Yii 1.1: yii-resque

php-resque wrapper for Yii, build as component and command console
10 followers

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.php and 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

Project Resource

Total 9 comments

#16531 report it
M0ka at 2014/03/03 03:54am
Thanks

Thank you for your notice, your solution is better for performance.

#16530 report it
TheBee at 2014/03/03 03:48am
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.

#16529 report it
M0ka at 2014/03/03 03:39am
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

#16526 report it
TheBee at 2014/03/03 12:55am
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
#16521 report it
M0ka at 2014/03/02 02:31am
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 :)

#16517 report it
TheBee at 2014/03/01 03:30pm
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.

#16513 report it
M0ka at 2014/03/01 07:09am
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?

#16512 report it
TheBee at 2014/03/01 05:12am
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
#16508 report it
M0ka at 2014/02/28 10:01pm
This is confusing

I am trying to add all what you posted literally but still cant make it work by this command

yiic rresque start

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?

Leave a comment

Please to leave your comment.

Create extension
Downloads
No downloadable files yet