Skip to content
A PHP job queue backed by Mongo
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



MongoQueue is a PHP queue that allows for moving tasks and jobs into an asynchronous process for completion in the background. The queue is managed by Mongo

MongoQueue is an extraction from online classifieds site Oodle. Oodle uses MongoQueue to background common tasks in order to keep page response times low.


  • mongod 1.5.4+
  • PHP 5.3+
  • Mongo PECL (PHP mongo driver)


Extract the source files into a directory in your PHP library path. Because the queue is backed by Mongo, there is no need to migrate or set up a table for MongoQueue. It will use the mongo_queue collection in a database of your choice.


Before any MongoQueue actions are taken you’ll need to initialize the MongoQueue class with the proper settings:

 $mongo = new Mongo('mongodb://host:port',  array('connect' => false));
 MongoQueue::$connection = $mongo;
 MongoQueue::$database = 'my_database';

Jobs are PHP objects that extend from MongoJob. Object states are not serialized so jobs must be defined as public static function.

 class QueueTracer
   extends MongoJob
   public static $context;
   public static function trace()
     echo "Tracer hit\n";
 MongoQueue::push('QueueTracer', 'trace', array(), time() + 500); # run QueueTracer 500 seconds later

You can also take advantage of @MongoJob@’s built in later() method which automatically delays a method for asynchronous running:


Running the jobs

Jobs are run via MongoQueue::run(). Before running job, you’ll need to set two extra MongoQueue initializers: environment and, optionally, context. MongoQueue currently does not include a daemon runner, but here is an example runner:

 $mongo = new Mongo('mongodb://host:port', array('connect' => false));
 MongoQueue::$connection = $mongo;
 MongoQueue::$database = 'my_database';
 MongoQueue::$environment = 'classes/jobs';
 MongoQueue::$context = array('context' => array('foo', 'bar'));

 if (MongoQueue::run())
     echo("Found a job to run");
     echo("Nothing to run");

$environment is the path under which jobs are automatically loaded by the runner. The convention is that if your job class is called FooBar then the file that is loaded is $environment/FooBar.php

Any keys set in $context are available as static variables inside the Job class. (e.g. ‘context’ in the QueueTracer example)


All jobs are stored as Mongo documents in the mongo_queue collection. MongoQueue does not come with any built in administrative scripts, but you can view the queue via your mongo console. locked_at will tell you whether or not a job is currently being processed.

Something went wrong with that request. Please try again.