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

资源魔 40 0

为何应用php-resque?

php-resque 是轻量级后盾义务零碎,基于Redis,性能设计简略,设置装备摆设灵敏。相比MQ零碎年夜而全的MQ零碎,这个显患上小而美。

php-resque 脚色划分

  • Job 界说义务,是担任详细的营业逻辑。
  • Queue 行列步队,担任Job存/取
  • Worker 从Queue中取Job来执行。 普通为PHP CLI模式下,后盾守护形式运转。

应用

install

  • 假如下载慢, 能够设置装备摆设 composer 国际镜像
composer config -g repo.packagist composer https://packagist.phpcomposer.com
  • 装置php-resque

旧版

Composer:This package is abandoned and no longer maintained. The author suggests using the resque/php-resque package instead.

composer require  "chrisboulton/php-resque 1.2"

更新为新的扩大包:resque/php-resque

composer require resque/php-resque

编写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 – 这个是须要的,会决议 worker 要执行甚么义务,首要的正在前,例如 QUEUE=notify,mail,log 。也能够设定為 QUEUE=* 示意执行一切义务。

  • APP_INCLUDE – 可选,加载文件用的。能够设成 APP_INCLUDE=require.php ,正在 require.php 中引入一切 Job 的 Class便可。

  • COUNT – 设定 worker 数目,预设是1 COUNT=5 。

  • REDIS_BACKEND – 设定 Redis 的 ip, port。假如没设定,预设是连 localhost:6379 。

  • LOGGING, VERBOSE – 设定 log, VERBOSE=1 便可。

  • VVERBOSE – 比拟具体的 log, VVERBOSE=1 debug 的时分能够开进去看。

  • INTERVAL – worker 反省 queue 的距离,预设是五秒 INTERVAL=5 。

  • PIDFILE – 假如你是开单 worker,能够指定 PIDFILE 把 pid 写入,例如 PIDFILE=/var/run/resque.pid 。

  • BACKGROUND 能够把 resque 丢到布景执行。或许应用 php resque.php &就能够了。

示例

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
co妹妹and=php resque-worker.php
environment=QUEUE='default'

优点:

  • 能够设置装备摆设 顺序异样加入后主动重启
  • 制订顺序运转用户
  • 能够设置过程数
  • 主动重启
  • supervisord启动后,主动启动剧本
  • 分组治理

更多PHP相干技巧文章,请拜访PHP教程栏目进行学习!

以上就是php-resque :基于Redis的后盾义务零碎的具体内容,更多请存眷资源魔其它相干文章!

标签: php开发教程 php开发资料 php开发自学 Redis php-resque 后台系统

抱歉,评论功能暂时关闭!