Background jobs with php and resque: part 3, installation

As said in part 2, we’ll use php-resque for our queue system. In this part, I’ll explain how to install all the tools needed to run php-resque, a port of Resque.

Resque (pronounced like “rescue”) is a Redis-backed library for creating background jobs, placing those jobs on multiple queues, and processing them later.

Obviously, we’ll also need to install Redis, and its php extension. The full list of tools to install :

Installation

Redis

Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.

It’s a very fast key-value database, since it’s stored in the RAM. You can also use it for caching, or storing sessions. Check out their download page to download and install the latest version. Installation instruction are on their website. Once installed, don’t forget to start the server.

PHP-Resque

php-resque is a php port of resque, and provides much the same features as the original version.

Download the latest version in zip, or clone the repo with

git clone git://github.com/chrisboulton/php-resque.git 

That’s pretty much all, it’s just a library, nothing to compile or install. Just put the folder somewhere in your project. If you’re using Composer, php-resque is available on packagist.

PHPredis

The phpredis extension provides an API for communicating with the Redis key-value store Again, installation instructions are in the README.

phpredis is not the only way for php to communicate with redis. There are many other library like redisent, rediska, predis and redisentwrap that can do the same thing, but phpredis is the fastest, since it’s a native php extension. (benchmark) Redisent is included in php-resque, and is the API used by php-resque to communicate with redis. You’ll have to edit some files in the library to get it working with phpredis.

Php-resque will just works fine if you do not want to install phpredis.

Php-Resque-Ex

Alternately, you can install php-resque-ex instead of php-resque. It’s my forked version of php-resque, with support of phpredis.

Installation instruction are the same : just download or clone git clone git://github.com/kamisama/php-resque-ex.git

An extra step is needed to download all the required libraries. If you’ve chosen to download the archive, first extract it.

We’ll then need to cd into the php-resque-ex folder:

cd ./php-resque-ex 

Then download composer and download the dependencies

curl -s https://getcomposer.org/installer | php
php composer.phar install

PHP PCNTL Extension

Process Control (PCNTL) implements the Unix style of process creation, program execution, signal handling and process termination It’s used to fork php process.

This extension only works on Unix platform. It does not work on Windows.

If it’s not already installed :

tar -zxvf php-x.x.x.tar.gz
  • Go into the ext/pcntl folder
cd php-x.x.x/ext/pcntl/
  • Configure, compile and install it
sudo phpize && ./configure && make install
  • Add extension=pcntl.so to your php.ini
  • Restart apache
/etc/init.d/apache2 restart # for linux
apachectl restart # for OS X

Next time …

In part 4, we’ll go into the heart of the subject : implementing the queue system and background jobs into your application.