php-resque实战

环境安装

安装REDIS

brew install redis
brew install homebrew/php/php71-redis

安装PHP-RESQUE

{    
    "require": {        
        "chrisboulton/php-resque": "1.2.x"
    }
}

composer install


代码封装

推送任务

require_once 'vendor/autoload.php';
date_default_timezone_set('GMT');
Resque::setBackend('127.0.0.1:6379');

$args = array(
   'time' => time(),
   'array' => array(
       'name' => 'Task_1',
       'todo' => 'add'
   ),
);

$jobId = Resque::enqueue('Task', 'Task', $args, true);
echo "Queued job ".$jobId."\n\n";

任务类

require_once 'vendor/autoload.php';
use redis\redis;

class Task
{
   public function perform()
   {
       $redis = new Redis('127.0.0.1','',6379);
       $redis-> set('Task_1', json_encode($this->args));
   }
}

服务类


include 'job/Task.php';

$queues = explode(',', 'Task');
$worker = new \Resque_Worker($queues);

fwrite(STDOUT, '*** Starting worker '.$worker."\n");
$ret = (string)$worker->work(1);
fwrite(STDOUT, $ret);


使用

添加任务

wuzhens-MacBook-Pro:bdmain wuzheng40$ php Starttask.php 
Queued job dc84f77630a825dbac801699c6c8f82a
Queued job 38a3874f1739cf614170769e7b38fc53

启动服务

wuzhens-MacBook-Pro:bdmain wuzheng40$ QUEUE=* php Server.php 
*** Starting worker wuzhens-MacBook-Pro.local:72678:Task

REDIS

127.0.0.1:6379> keys *
1) "resque:workers"
2) "Task_1"
3) "resque:queues"
4) "resque:worker:wuzhens-MacBook-Pro.local:72678:Task:started"
5) "resque:stat:processed"
6) "resque:stat:processed:wuzhens-MacBook-Pro.local:72678:Task"
7) "Task_2"
8) "resque:job:38a3874f1739cf614170769e7b38fc53:status"
9) "resque:job:dc84f77630a825dbac801699c6c8f82a:status"
127.0.0.1:6379> get Task_1
"{\"time\":1513307784,\"array\":{\"name\":\"Task_1\",\"todo\":\"add\"}}"
127.0.0.1:6379> get Task_2
"{\"time\":1513307784,\"array\":{\"name\":\"Task_2\",\"todo\":\"lock\"}}"


GITHUB

https://github.com/chrisboulton/php-resque