Yii resque is a component for Yii to queue your background jobs, this component based on php-resque and php-resque-scheduler with some enhancement for support phpredis, I'm also added log handler using Monolog, already tested with ResqueBoard.
- php pcntl extension.
- Redis.io
- phpredis extension for better performance, otherwise it'll automatically use Credis as fallback.
- Yii Framework >1.1.x
- Copy files to each folder
- Add to your
config/main.php
and yourconfig/console.php
...
'components'=>array(
...
'resque'=>array(
'class' => 'application.components.yii-resque.RResque',
'server' => 'localhost', // Redis server address
'port' => '6379', // Redis server port
'database' => 0, // Redis database number
'password' => '', // Redis password auth, set to '' or null when no auth needed
'includeFiles' => array() // Absolute path of files that will be included when initiate queue
),
...
)
...
-
Change path for Yii framework folder in
components/yii-resque/bin/resque
-
You may want to add these additional lines to your
console.php
for fixing auto loading and enabling the usage of models and helpers inside your workers :
...
'import' => array(
'application.models.*',
'application.helpers.*',
'application.components.*',
),
For performance reason you can just load required class by adding Yii::import()
on your setUp
function inside worker class, e.g :
public function setUp()
{
Yii::import('application.models.*');
}
You can put this line where ever you want to add jobs to queue
Yii::app()->resque->createJob('queue_name', 'Worker_ClassWorker', $args = array());
Put your workers inside Worker folder and name the class with Worker_
as prefix, e.g you want to create worker with name SendEmail then you can create file inside Worker folder and name it SendEmail.php, class inside this file must be Worker_SendEmail
You can run job at specific time
$time = 1332067214;
Yii::app()->resque->enqueueJobAt($time, 'queue_name', 'Worker_ClassWorker', $args = array());
or run job after n second
$in = 3600;
$args = array('id' => $user->id);
Yii::app()->resque->enqueueIn($in, 'email', 'Worker_ClassWorker', $args);
This is some trick that sometime useful if you want to do some recurring job like sending weekly newsletter, I just made some modification in my worker tearDown
event
public function tearDown()
{
$interval = 3600; # This job will repeat every hour
# Add next job queue based on interval
Yii::app()->resque->enqueueJobIn($interval, 'queue_name', 'Worker_Newsletter', $args = array());
}
So everytime job has done completely the worker will send queue for same job.
This will return all job in queue (EXCLUDE all active job)
Yii::app()->resque->getQueues();
Run this command from your console/terminal :
Start queue
yiic rresque start
or
yiic rresque start --queue=queue_name --interval=5 --verbose=0
Start delayed or scheduled queue
yiic rresque startrecurring
Stop queue
yiic rresque stop
Stop queue with QUIT signal
yiic rresque stop --quit=true
This is available options for starting worker using yiic
command :
- Set queue name
--queue=[queue_name]
This option default to *
means all queue.
- Set interval time
--interval=[time in second]
Set your interval time for checking new job.
- Run in verbose mode
--verbose=[1 or 0]
Set to 1
if you want to see more information in log file.
- Number of worker
--count=[integer]
- Log handler name and log handler target
--loghandler=[string] --logtarget=[string]
You can see available log handler at Monolog-Init.
Copyright (c) 2013 Rolies106
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.
- rolies106
- chrisschrijver
- summic
- anasAsh
- dbanck
- herroffizier
- davidevernizzi
- You