You are on page 1of 7

PHP Resque

This document is created to give you an insight on the PHP resque library.
The resque library is created for background jobs.
Background jobs are the jobs which are executed outside of the main
workflow.
We use php resque to create the ideal workflow.

An example of an ideal workflow.

As you can see in the picture above the main task is to update the database
and inform the user as quick as possible.
The workflow consist of 3 parts.
1. Update the database.
2. Send tasks to the queue(to do list).
3. Execute the background jobs.
It is obvious that if the 1st task fails, the 2nd and 3rd won’t be executed.

PHP resque is our queue system(execute jobs system).


This system uses Redis database system to function, you will read about this
later.

Getting started
To use this library we will have to get the library. You can download the
library from Github just Google the library.

Both the link and the title contain the name: Chris Boulton, he is the author
of the library.
Clone the library or download it as a zip file. After downloading the library
just add the library to your project folder.

We will include the autoload.php file to use the classes which autoload.php
includes. The class which autoload.php includes is generated by the
composer. We include this file in the same file we want to add the task(s) to
the queue. In our case we have a separate file where we add
tasks(task.php), so we will include the autoload in task.php

For the queue system


to work you don’t have to include the autoload.php.

We also need to install Redis. Redis is a key value data storage system,
which saves data in the computer’s RAM. This makes accessing data
extremely fast. Redis is already installed on our server, but to use it we
must start it. Type in the command line redis-server, this command will start
the redis server and show some information.

There are more commands for managing the redis-sever, for a


quickstart go to: https://redis.io/topics/quickstart

The Queue

Let’s imagine we have simple form, like this:

When the user clicks on the submit button we want some tasks to be
performed, the main task will be adding the data to the database. The
background jobs will be emptying the cache and sending a conformation
mail to the user.

If the main job is executed correctly we will add tasks to the queue, but
how? The Resque::enqueue() function adds tasks to the queue. This function
takes 3 parameters.

1. The first argument is a task name, this can be any random name.

2. The second argument is the name of the executing class, this can also
be any random name but remember that the class name should be the
same. If the class name isn’t the same as you have passed in the
arguments the worker script won’t be able to execute the job. I will
explain about the worker script later.

3. The third argument is just a variable we are passing to the function,


this variable can be used in the class, I will later show an example.

The Classes

We have set a task and passed some arguments, but we haven’t created any
functions. Without function we can’t execute tasks, so we need functions.
We have to create classes! Classes which contain functions, a class example:
Look at the picture above, this piece of code is a class with methods in it.
The name of the class is the same as the second parameter of the
Resque::enqueue() function, this is mandatory. The worker script needs to
know which class’ methods to execute. The class is built with a certain
structure, the structure are the methods. The 1st method is the setup, this
one is the pre-execute method. It could be used for creating a database
connection for example. The 2nd method is the main function which should
execute the main job. The 3rd method is the after execution method, it could
be used to close a database connection for example.
All 3 method must be in the class otherwise the class will be invalid and the
worker script won’t execute it.
As you can see we use the $args variable in the mail function, this is an
example of how you could use the 3rd argument of the Resque::enqueue
method.

The Worker
Now we have set a task and we have a class which belongs to the task, but
who is going execute this task and the class’ methods?

We need a worker, a worker is script which executes the task.


Let’s take a look at the worker script:

The picture above is the worker, it contains to require statements, the 1 st


one is a job.php, job.php is the script which contains the class and methods.
The 2nd require statement includes resque.php, this file is within the
php-resque library.

We cannot run this worker script through the browser, it doesn’t work like
that. We have to run this file through the command line.
Navigate to your worker file and run it.

My worker file is named run_worker.php, but when I try to run it gives a


message back, as you can see in the 2nd line of the picture.
It wants me to set an environmental variable containing the queues, these
queues are the tasks I want the worker to execute.
This is the proper way to execute a worker script.

The command above says: Hi worker start looking for the task send_mail in
the queue. The VVERBOSE=1 gives us more detail about the workers
process. When the command is run we will get the following output:

The worker is started and is looking for the same task after 5 seconds, a
single worker script can execute multiple tasks as long as the associated
class with methods is included. You can change the settings of the
worker(s), you can tell the worker to pull al queues or to sleep for 10 instead
of 5 seconds.

You might also like