PHPResque for message or job queues

posted in: Design, Linux, PHP, Standards | 0

I am not going to compare different message queues or try and convince you to delegate long running tasks to a queue.

Instead I am simply going to show you how to get started with PHPResque.

For the repo I am using: https://github.com/thinkadoo/php-resque-ex
For documentation: https://github.com/chrisboulton/php-resque

job.php 

<?php
/* /src/worker/Job.php */
class PHP_Job
{
    public function perform()
    {
    	$jobID = $this->job->payload['id'];
    	$workerClass = $this->job->payload['class'];
    	$arguments = json_encode($this->args);
		$time = $this->args['array']['time'];
        fwrite(STDOUT, "$jobID : $workerClass -> $arguments\n");
    }
}

queue.php

<?php
/* /demo/queue.php */
if(empty($argv[1])) {
	die('Specify the name of a job to add. e.g, php queue.php PHP_Job');
}

require '../lib/Resque.php';
date_default_timezone_set('GMT');
Resque::setBackend('127.0.0.1:6379');

for ($i = 1; $i <= 1000; $i++) {
		$args = array(
			'counter' => $i,
			'array' => array(
				'time' => time(),
			),
		);
    $jobId = Resque::enqueue('default', $argv[1], $args, true);
    echo "Queued job ".$jobId."\n\n";
}

 

Queue the Jobs: from the ‘demo’ folder

$ php queue.php PHP_Job

Queued job 2a43fed22312a98527384a2d8b3203a1
Queued job a5163c652ada7f2b3a57c9ff4bbab839

Queued job 473f7a8a0012b0336ebbc51f65b27fd9
Queued job 2cf8765e28214959bd6ef5a36c5d48a2

Delegate the Workers: from the ‘root’ folder

$ QUEUE=default INTERVAL=1 COUNT=1 APP_INCLUDE=src/worker/Job.php php ./bin/resque

2a43fed22312a98527384a2d8b3203a1 : PHP_Job -> {“counter”:1,”array”:{“time”:1397250633}}
a5163c652ada7f2b3a57c9ff4bbab839 : PHP_Job -> {“counter”:2,”array”:{“time”:1397250633}}

473f7a8a0012b0336ebbc51f65b27fd9 : PHP_Job -> {“counter”:999,”array”:{“time”:1397250633}}
2cf8765e28214959bd6ef5a36c5d48a2 : PHP_Job -> {“counter”:1000,”array”:{“time”:1397250633}}
For faster workers set “INTERVAL=1” smaller e.g. INTERVAL=.01
For more workers set “COUNT=1” larger e.g. COUNT=10
This helped me : http://kamisama.me/2012/10/12/background-jobs-with-php-and-resque-part-4-managing-worker/

 

Structure of a message object:

 

/**
 class Resque_Job#5 (4) {
  public $queue =>
  string(7) "default"
  public $worker =>
  class Resque_Worker#3 (10) {
    public $logOutput =>
    resource(2) of type (stream)
    public $logLevel =>
    int(0)
    protected $queues =>
    array(1) {
      [0] =>
      string(1) "*"
    }
    protected $hostname =>
    string(13) "Air-128.local"
    protected $shutdown =>
    bool(false)
    protected $paused =>
    bool(false)
    protected $id =>
    string(20) "Air-128.local:4856:*"
    protected $currentJob =>
          ...

    protected $child =>
    int(0)
    protected $logger =>
    NULL
  }
  public $payload =>
  array(3) {
    'class' =>
    string(7) "PHP_Job"
    'args' =>
    array(1) {
      [0] =>
      array(2) {
        'counter' =>
        int(778)
        'array' =>
        array(1) {
          'time' =>
          int(1397248458)
        }
      }
    }
    'id' =>
    string(32) "165e2b707e128e20d5abaa75c951b9b4"
  }
  private $instance =>
  class PHP_Job#6 (3) {
    public $job =>
          ...

    public $args =>
    array(2) {
      'counter' =>
      int(778)
      'array' =>
      array(1) {
        'time' =>
        int(1397248458)
      }
    }
    public $queue =>
    string(7) "default"
  }
}
*/

Leave a Reply