Skip to content
Browse files

Add a new at() method for MongoJob. Add documentation to MongoQueue.

  • Loading branch information...
1 parent 20874d3 commit bae8bfeda18ed6d3a35c57ef9d0d0f756788f91a @lunaru committed
Showing with 104 additions and 5 deletions.
  1. +18 −0 LICENSE
  2. 0 README
  3. +75 −0 README.textile
  4. +4 −4 lib/MongoFunctor.php
  5. +7 −1 lib/MongoJob.php
View
18 LICENSE
@@ -0,0 +1,18 @@
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
View
0 README
No changes.
View
75 README.textile
@@ -0,0 +1,75 @@
+h1. MongoQueue
+
+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":http://pecl.php.net/package/mongo
+
+MongoQueue is an extraction from online classifieds site "Oodle":http://www.oodle.com. Oodle uses MongoQueue to background common tasks in order to keep page response times low.
+
+h2. Requirements
+
+* PHP 5.3+
+* Mongo PECL
+
+h2. Installation
+
+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.
+
+h3. Usage
+
+Before any MongoQueue actions are taken you'll need to initialize the MongoQueue class with the proper settings:
+
+pre..
+$mongo = new Mongo('mongodb://host:port', array('connect' => false));
+MongoQueue::$connection = $mongo;
+MongoQueue::$database = 'my_database';
+
+p.
+Jobs are PHP objects that extend from @MongoJob@. Object states are not serialized so jobs must be defined as @public static function@.
+
+pre..
+require_once('MongoQueue/lib/MongoJob.php');
+
+class QueueTracer
+ extends MongoJob
+{
+ public static $context;
+
+ public static function trace()
+ {
+ log_msg('INF', "Tracer hit");
+ }
+}
+
+MongoQueue::push('QueueTracer', 'trace', array(), time() + 500); # run QueueTracer 500 seconds later
+
+p. You can also take advantage of @MongoJob@'s built in @later()@ method which automatically delays a method for asynchronous running:
+
+pre..
+QueueTracer::later(500)->trace()
+
+
+h3. 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:
+
+pre..
+$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");
+else
+ echo("Nothing to run");
+
+p.
+$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)
+
+h3. Administration
+
+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.
+
+~
View
8 lib/MongoFunctor.php
@@ -4,17 +4,17 @@
class MongoFunctor
{
- protected $delay = 0;
+ protected $when;
protected $className = null;
- public function __construct($className, $delay)
+ public function __construct($className, $when)
{
$this->className = $className;
- $this->delay = $delay;
+ $this->when = $when;
}
public function __call($method, $args)
{
- MongoQueue::push($this->className, $method, $args, time() + $this->delay);
+ MongoQueue::push($this->className, $method, $args, $this->when);
}
}
View
8 lib/MongoJob.php
@@ -7,7 +7,13 @@
{
public static function later($delay = 0)
{
+ return $this->at(time() + $delay);
+ }
+
+ public static function at($time = null)
+ {
+ if (!$time) $time = time();
$className = get_called_class();
- return new MongoFunctor($className, $delay);
+ return new MongoFunctor($className, $time);
}
}

0 comments on commit bae8bfe

Please sign in to comment.
Something went wrong with that request. Please try again.