技术文PHP实战程序员

php-resque :基于Redis的后台任务系统

2016-10-22  本文已影响2906人  codefine

为什么使用php-resque?

php-resque 是轻量级后台任务系统,基于Redis,功能设计简单,配置灵活。相比MQ系统大而全的MQ系统,这个显得小而美。

php-resque 角色划分

使用

install

composer config -g repo.packagist composer https://packagist.phpcomposer.com
composer require  "chrisboulton/php-resque 1.2"

编写Job

DemoJob.php

<?php
class DemoJob
{
    public function perform()
    {
        // Work work work
        //echo $this->args['name'];
    }
}

入队列操作

<?php

Resque::setBackend('localhost:6379');
$args = array(
      'name' => 'hanmeimei',
    );
Resque::enqueue('default', DemoJob::class, $args);

Worker代码

resque-worker.php

<?php
$redis_dsn = '127.0.0.1:6379';
putenv("REDIS_BACKEND=$redis_dsn");
// 引入队列的入口程序
$resque = realpath(dirname(__FILE__) . '/vendor/chrisboulton/php-resque/resque.php');
require_once $resque;

启动worker

php-resque 的环境变量有:

示例

QUEUE=counter php resque-worker.php

至此,php-resque的安装和使用已经完毕。

后面的章节是工具插件, 仅供参考。


界面 resque-web

监控 PHP-Resque 的运行状况

安装

gem install resque-web -v 0.0.8

运行

resque-web -p 40000

监控 supervisor

启动服务

/usr/bin/python /usr/bin/supervisord -c /etc/supervisor/supervisord.conf

监控项目配置
/etc/supervisor/conf.d/lumen_resque.conf

[program:worker_lumen_resque]
directory=/home/wwwroot/mysite
command=php resque-worker.php
environment=QUEUE='default'

优点:

上一篇 下一篇

猜你喜欢

热点阅读