dev-master
9999999-dev http://github.com/josegonzalez/cakephp-cake-djjobjob queues for cakephp using seatgeek's djjob — Edit
MIT
The Requires
cakephp queues
job queues for cakephp using seatgeek's djjob — Edit
Quick and easy job queues, based on delayed_job, (*2)
CakePackages is the type of site that would do well with background processes. Rather than try to build - and fail - a job/worker system, I set out to wrap an existing system., (*3)
I am aware of a few plugins for cakephp, notably the queue plugin by David Persson, and the cron-mailer plugin by Pierre-Emmanuel Fringant. However, I don't wish to run and maintain yet another service, and my cron tasks aren't email-based at all. The other thing was the lack of documentation for David's plugin, and I am hesitant to familiarize myself with so many tools at once., (*4)
At SeatGeek, we use a PHP port of delayed_job, which works very well in production. It's a neat little set of classes that abstracts away most of the pain of creating new jobs and workers. Unfortunately, the only system I've ever used it in was Symfony (you'll notice we include a sample Symfony task with the package)., (*5)
So here this is, Cake DJJob. Use it and abuse it to create Jobs/workers whenever necessary :), (*6)
You are required to install both djjob
and cake_djjob
. Note that you'll need a jobs
table just like the one included with djjob
. A cake_schema
file is forthcoming., (*7)
[Manual], (*8)
Djjob/Vendor
[GIT Submodule], (*9)
In your app directory type:, (*10)
git submodule add git://github.com/seatgeek/djjob.git Plugin/Djjob/Vendor git submodule init git submodule update
[GIT Clone], (*11)
In your plugin directory type, (*12)
git clone git://github.com/seatgeek/djjob.git Djjob/Vendor
h3. Cake DJJob Installation, (*13)
[Manual], (*14)
CakeDjjob
[GIT Submodule], (*15)
In your app directory type:, (*16)
git submodule add git://github.com/josegonzalez/cake_djjob.git Plugin/CakeDjjob git submodule init git submodule update
[GIT Clone], (*17)
In your Plugin directory type, (*18)
git clone git://github.com/josegonzalez/cake_djjob.git CakeDjjob
You can create new jobs in the Lib/Job
folder of your application or of any plugin. When creating plugin jobs, the plugin name must be in the job class name, but not the job class file. For example, the CakeDjjob
plugin contains a CakeDjjob_TestJob
in Lib/Job/TestJob.php
. All plugins must be separated from the job classname using an underscore, and there can only be one underscore in a classname. This is so you can use jobs from plugins as well as from the application., (*19)
Jobs respond to a perform()
method, as in djjob. In CakeDjjob
, all jobs should extend the CakeJob
class. This is not required, but it allows for increased base functionality in your jobs. For example, an application's ForgotPasswordJob
might be composed as follows:, (*20)
email = $email; } function perform() { $this->out('Some debug output'); // Using models $this->loadModel('User'); $user = $this->User->findByEmail($this->email); if (empty($user)) { return; } // Complex email stuff here... $this->loadComponent('Email'); $this->Email->_set(array( 'to' => $this->email, 'from' => 'noreply@example.com', 'subject' => 'Forgot your password?', 'replyTo' => 'noreply@example.com', 'template' => null, 'sendAs' => 'both', )); $this->Email->send("Some text to indicate how to reset password..."); } } ?>
Extending the @CakeJob@ also allows the usage of the following methods:, (*21)
By extending @CakeJob@, you get an instant base off of which writing jobs will be quick and easy., (*22)
You can attach the @CakeDjjob@ Component to your controller like so:, (*23)
User->forgot($this->data)) != false) { $this->CakeDjjob->enqueue(new ForgotPasswordJob($email)); $this->Session->setFlash('You will shortly be sent a password reset email :)'); } else { $this->Session->setFlash('There is no such email in our system. Please try again.'); } } } ?>
It is also possible to queue jobs from a Model by attaching the included @CakeDjjob@ behavior:, (*24)
enqueue(new ForgotPasswordJob($email)); } public function bulkAction($emails = array(), $config = array()) { $jobs = array(); foreach ($emails as $email) { $jobs[] = new BulkPasswordResetJob($email, $config); } return $this->bulkEnqueue($jobs); } } ?>
enqueue($this->CakeDjjob->load('ForgotPasswordJob', $email)); } public function bulkAction($emails = array(), $config = array()) { $jobs = array(); foreach ($emails as $email) $jobs[] = $this->CakeDjjob->load('BulkPasswordResetJob', $email, $config); } return $this->bulkEnqueue($jobs); } } ?>
Included with this plugin is a task to run jobs., (*25)
cake worker run
The above statement will run a single worker on the default queue forever. It can be added to your cron or by some other process manager such as God. The following params are available for the worker:, (*26)
It's possible to clean a particular job queue, by either resetting locked jobs or deleting failed jobs:, (*27)
cake worker cleanup
This task can take the connection
, type
, queue
, and debug
arguments, as well as a few custom ones:, (*28)
If you need to get the status of a particular job queue, you can call the status method on the task:, (*29)
cake worker status
This task can take the connection
, type
, queue
, and debug
arguments as well., (*30)
Copyright (c) 2011 Jose Diaz-Gonzalez, (*31)
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:, (*32)
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software., (*33)
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., (*34)
job queues for cakephp using seatgeek's djjob — Edit
MIT
cakephp queues