Background jobs with Phalcon and php-resque

September 2nd, 2014 | Posted by mehuldoshi in Background Jobs | phalcon | php

In this article I have attempted to cover setting up background jobs in Phalcon using a php-resque library. I assume you have a basic LAMP/LEMP setup ready, additionally following libraries would be required.

Phalcon framework: This is one of the upcoming framework implemented as C extension.
Redis server
: used by php-resque as its backend
php-resque library: which will do the magic of background processing

If you would like to learn about what are background jobs and resque, I would recommend you to check out the article series at http://kamisama.me/2012/10/09/background-jobs-with-php-and-resque-part-1-introduction/
You can also check out different options related to Resque in the series of articles.

Installation

Install Phalcon

Please refer the instruction here for installation of Phalcon.

Install Redis

Please follow the instructions here for installation of Redis server.

Install project

You can clone or download the project from phlacon-resque and put this under your web root folder.

Installation of php-resque

Once you have downloaded the project simply run the composer using following command

Setup

Start Redis server

Setup Queue of Resque

Ok so now we have redis up and running and worker is started lets create a background job, open the browser and hit the home page, http://localhost/phalcon-resque/public/
Clicking on “Set Background Job!” will put a job in the queue, for demo purpose the background job just does a small work of storing some data in a text file. We are passing random values from frontend which will be stored in  a text file.

If everything went well you will see a text “Value: <someid>” within logs/res.log file.

Here is quick overview of some of the important codes,

index.php

make sure you include the autoload file as show above, this will include the php-resque files within the project.

IndexController.php

Important function here is the Resque::enque which takes 4 parameters

1st   :  (string) name of Queue, this should be same as the one which was used while starting the worker.
2nd :  (string) ClassName of the background job, this class should have a method called perform.
3rd  :  (array) argument array which can be passed to background class
4th  :  (boolean) if true will return job id which can be used for tracking purpose.

Once the job is created it will return the job id which can be used for tracking the status of job.

I have created a function “getJobStatus()” which will use the Resque_Job_Status class and return the status of the job.

TestBackgroundJob.php

above function is self explanatory only important thing to notice here is how to access the arguments passed.

bg.php

This will make sure the Backgrond jobs are accessible by workers without need to run application

*Note: Whenever you make any changes within your code make sure you restart the php-resque worker, otherwise the new code changes would not be reflected.

I hope this will be useful for some one to get started with setting up php-resque with Phalcon. In case you face any problem or have any questions please use the comment section below.

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">