JQJobs is a job queue infrastructure for PHP.
Features
Roadmap
The job system has only a few core parts:
Additional Utilities:
The JQStore manages the queue and persistence of the JQManagedJob's.
JQStore is an interface, but the job system ships with several concrete implementations. The system is architected in this manner to allow the job store to be migrated to different backing stores (memcache, db, Amazon SQS, etc). JQStore implementations are very simple.
Jobs that complete successfully are removed from the queue immediately. Jobs that fail are retried until maxAttempts is reached, and then they are marked FAILED and left in the queue. It's up to the application to cleanup failed entries.
If the application requires an audit log or archive of job history, it should implement this in run()/cleanup() for each job, or in a custom JQStore subclass.
The minimal amount of work needed to use a JQJobs is 1) create at least one job; 2) create a queuestore; 3) add jobs; 4) start a worker.
1) Create a job
class SampleJob implements JQJob
{
function __construct($info) { $this->info = $info; }
function run() { print $this->description() . "\n"; } // no-op
function cleanup() { print "cleanup() {$this->description()}\n"; }
function statusDidChange(JQManagedJob $mJob, $oldStatus, $message) { print "SampleJob [Job {$mJob->getJobId()}] {$oldStatus} ==> {$mJob->getStatus()} {$message}\n"; }
function description() { return "Sample job {$this->info}"; }
}
2) Create a queuestore
$q = new JQStore_Array();
// alternatively; create a db-based queue with Propel:
$con = Propel::getConnection(JQStoreManagedJobPeer::DATABASE_NAME);
$q = new JQStore_Propel('JQStoreManagedJob', $con);
3) Add jobs
foreach (range(1,10) as $i) {
$q->enqueue(new SampleJob($i));
}
4) Start a worker to run the jobs.
declare(ticks = 1); // to have JQJobs be able to gracefully handle SIGKILL (or other *immediate termination* signals)
// the declare(ticks=1) must be in global scope.
$w = new JQWorker($q);
$w->start();
5) If you want hung jobs detection and you aren't using JQAutoscaler, you will need to schedule a task to run JQStore::detectHungJobs().
=======================
JQDelayedJob Example
JQDelayedJob::doLater(new MyJob('data'));
JQDelayedJob::doLater(function() { print "Hello, World. I am running from a delayed job after the script exits!"; });
INSTALLATION
pear install apinstein.pearfarm.org/jqjobs
See http://apinstein.pearfarm.org/apinstein/jqjobs
SOURCE
https://github.com/apinstein/jqjobs
Currently the only db-backed JQStore implememtation is for Propel ORM. All migrations needed for JQJobs/Propel to function are in the migrations/ folder which is expected to be run with mp (github.com/apinstein/mp). This could easily be adapter into a Propel plugin, but hasn't yet.
In any case, just ensure that if you are installing/upgrading your JQJobs that you copy and re-sequence the migrations as needed.