Queue
NAME
MongoDB::Queue - Fast persistent queueing system backed by MongoDB::Fast
SYNOPSIS
use MongoDB::Queue;
my $q = MongoDB::Queue.connect(host => 'localhost', db => 'myapp');
# Single-job producer
my $id = $q.enqueue({ task => 'send-email', to => '[email protected]' });
my $id = $q.enqueue({ task => 'resize-image' }, priority => 10, delay => 5);
# Bulk producer โ one MongoDB round trip for N jobs
my @ids = $q.enqueue-many(@payloads, priority => 5);
# Single-job consumer
while my $job = $q.dequeue {
process($job<payload>);
$q.ack($job<_id>);
CATCH { default { $q.nack($job<_id>) } }
}
# Bulk consumer โ one find + N claims, then one round trip to ack all
while my @jobs = $q.dequeue-many(20) {
process($_<payload>) for @jobs;
$q.ack-many(@jobs.map(*<_id>));
}
# Or use the built-in event loop
$q.listen(-> $job { process($job<payload>) });
DESCRIPTION
Each job document stored in MongoDB has the following fields:
_id Str โ unique job ID (random hex)
payload Hash โ arbitrary job data supplied by caller
status Str โ pending | processing | done | failed
priority Int โ higher value = dequeued first (default 0)
available_at Int โ epoch seconds; supports delayed delivery
available_at_dt DateTime โ UTC datetime, e.g. 2025-01-01T12:00:00Z
created_at Int โ epoch seconds
created_at_dt DateTime โ UTC datetime
locked_at Int โ epoch seconds when last claimed (Nil if pending)
locked_at_dt DateTime โ UTC datetime (Nil if pending)
locked_by Str โ worker ID that holds the lock (Nil if pending)
attempts Int โ how many times this job has been dequeued
max_attempts Int โ fail permanently after this many attempts
done_at Int โ epoch seconds when acked/failed (Nil otherwise)
done_at_dt DateTime โ UTC datetime (Nil otherwise)