PHP Workers with Redis & Solo
I’ve come across an awesome combination of tools for managing PHP Workers, and thought I’d share.
Why Workers?
Sometimes there are situations when you want to parallel process things. Other times you might have a list of tasks to accomplish, and you don’t want to make the user wait after pressing a button. This is where “Workers” can come in. They are independent scripts that run along side of your application, performing tasks, or “jobs.”
An example is with Dating DNA and our score system. We generate scores between users to show how compatible they are with each other. When a user signs up, or makes a significant change to their profile questionnaire, we need to run a job to query our database, build a list of potential users, and generate scores. This takes 10-20 seconds, and while it is pretty fast, we don’t want to make the user wait for that. So we queue up a job for the user, divide up the work among several workers, and process the work.
General Concept
For this post, we’ll use the example of generating reports. Lets say on your internal website there is a button that you can click and it will email the user a report, and the report takes 2-3 minutes to generate. When the button is clicked, your code will insert the job into the queue. Meanwhile, workers are monitoring the queue. A worker script will pull the job off the queue, process the report, and send the email when its done.
For the queue management, we’ll use Redis. To let PHP read and write data to Redis, we’ll use the PHP Library predis. In our examples we’ll use PHP 5.3, however predis has a PHP 5.2 backport if you are not running 5.3.
Adding Jobs
To add jobs, we’ll need to connect to our Redis server:
/*
* Connecting to Redis
*/
const REDIS_HOST = '127.0.0.1';
const REDIS_PORT = 6379;
$predis = new Predis\Client(array(
'scheme' => 'tcp',
'host' => REDIS_HOST,
'port' => REDIS_PORT,
));
We’ll assume in all of our examples that we’ve done the following above & connected to Redis.






