Yii Resque ¶
Yii resque is a component for Yii to queue your background jobs, this component based on php-resque with some enhancement for support phpredis.
Requirement ¶
- php pnctl extension.
- Redis.io
- phpredis extension for better performance, otherwise it'll automatically using Credis as fallback.
- Yii Framework >1.1.x
Configuration ¶
- Copy files to each folder
- Add to your
`
config.phpand your
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
),
...
)
...
- Change path for Yii framework folder in
`
components/yii-resque/bin/resque`
How to ¶
Create job and Workers ¶
You can put this line where ever you want to register the 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`
Start and Stop workers ¶
Run this command from your console/terminal :
yiic rresque start
or
yiic rresque stop
This is confusing
I am trying to add all what you posted literally but still cant make it work by this command
first it said no yiic command known, so i tried to make it /var/www/yii/framework/yiic but still doesn't work. cant start rresque at all, could you please help?
Run in protected folder
You should run it from protected folder, so it'll be look like this :
/var/www/your-project/protected/yiic rresque start
Thanks
Thanks for your reply.. when I did what you've said, I got it work, now I had this on my log file
This on resque log
[2014-03-01T11:08:10+00:00] got {"queue":"default","id":"414c8b2770dc95584a8a5f86ca2195e1","class":"Worker_Email","args":[[]]} [2014-03-01T11:08:10+00:00] Processing ID:414c8b2770dc95584a8a5f86ca2195e1 in default <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Internal Server Error</title> <style type="text/css"> /*<![CDATA[*/ body {font-family:"Verdana";font-weight:normal;color:black;background-color:white;} h1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red } h2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon } h3 {font-family:"Verdana";font-weight:bold;font-size:11pt} p {font-family:"Verdana";font-weight:normal;color:black;font-size:9pt;margin-top: -5px} .version {color: gray;font-size:8pt;border-top:1px solid #aaaaaa;} /*]]>*/ </style> </head> <body> <h1>Internal Server Error</h1> <h2>Invalid argument supplied for foreach()</h2> <p> An internal error occurred while the Web server was processing your request. Please contact the webmaster to report this problem. </p> <p> Thank you. </p> <div class="version"> 2014-03-01 11:08:10 </div> </body> </html>
and this on application
2014/03/01 11:08:10 [error] [php] Invalid argument supplied for foreach() (/var/www/yii/framework/YiiBase.php:411) Stack trace: #0 protected/components/yii-resque/lib/Resque/Job.php(155): class_exists() #1 protected/components/yii-resque/lib/Resque/Job.php(187): Resque_Job->getInstance() #2 protected/components/yii-resque/lib/Resque/Worker.php(245): Resque_Job->perform() #3 protected/components/yii-resque/lib/Resque/Worker.php(208): Resque_Worker->perform() #4 protected/components/yii-resque/bin/resque(131): Resque_Worker->work() #5 protected/components/yii-resque/bin/resque(103): startWorker()
Can you please help me with it?
re-check
Not sure about this, just for re-check, are you sure have file "Email.php" inside Worker folder? and this file is contain "Worker_Email" class.
Hello
Yes I have Email.php and the class name Worker_Email, and inside this class, I made 3 functions, setup(), perform() and tearDown(), and I have put my code that I want it to be excuted into the perform() function, which is an update to a mongoDB, and I have added the config of the mongoDB into config/console.php
Is there anything missing of my scenario? Thank you so much in advance :)
Resque Log
is there any error thrown when you start the resque or sending a job on your resque log file? log file location is :
protected/runtime/yii_resque_log.log
I figured it out
Thank you so much
I figured it out by this solution, I added these codes to the config/console.php
'import' => array( 'application.models.*', 'application.components.*', 'application.extensions.directmongosuite.components.*', 'application.extensions.helpers.*', ),
and for any database queries, I had to add the configurations to the config/console.php too
Configuration
As you mention it, configuration that you use in main.php will never loaded on your resque worker since it using console.php instead, so you need to import your require class on console.php or using
Yii::import('application.models.*');
on setUp function inside your worker class to avoid importing all classes on every jobs.
Thanks
Thank you for your notice, your solution is better for performance.
Server dies after some time... ?
Got it working and yii-resque server is supposed to be active but if I leave it for months as I just did (app hasn't launched yet, working on other sections), then it "dies" and the worker gives me an exit code of 255. Any ideas how to prevent this and keep it "alive" ? I'm not sure what the "timeout" duration is. A server should be reliable and keep running and not just cop out when it feels like it.
Thanks !
Also posted here : http://www.yiiframework.com/forum/index.php/topic/69068-yii-resque-extension-issue/
How to keep worker alive
@jinren The best trick to keep the worker alive is using Process control system like supervisor in ubuntu, it'll automatically revive the worker if it stop suddenly and also if there is an error it'll put some logs so you can trace the bug easily.
Thanks
OK, will look into it.
Process dying ?
@TheBee I'm looking into supervisord now. Just curious but why would yii-resque die if it's not even being used ? Is there any way to address that ? Thank you :-)
Supervisord won't work with this
Supervisord won't work with this because your process is a daemonizing process : [http://supervisord.org/subprocess.html].
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.