为何运用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 command=php resque-worker.php environment=QUEUE='default'
长处:
- 能够设置 顺序非常退出后自动重启
- 制订顺序运转用户
- 能够设置历程数
- 自动重启
- supervisord启动后,自动启动剧本
- 分组治理
更多PHP相干技术文章,请接见PHP教程栏目举行进修!
以上就是php-resque :基于Redis的背景使命体系的细致内容,更多请关注ki4网别的相干文章!