這篇文章給大家分享的是有關php-resque如何搭建基于Redis的后臺任務系統的內容。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。
php-resque 是輕量級后臺任務系統,基于Redis,功能設計簡單,配置靈活。相比MQ系統大而全的MQ系統,這個顯得小而美。
PHP CLI模式下,后臺守護方式運行。composer config -g repo.packagist composer https://packagist.phpcomposer.com
舊版
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
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);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;php-resque 的環境變量有:
QUEUE – 這個是必要的,會決定 worker 要執行什么任務,重要的在前,例如 QUEUE=notify,mail,log 。也可以設定為 QUEUE=* 表示執行所有任務。
APP_INCLUDE – 可選,加載文件用的??梢栽O成 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'
優點:
感謝各位的閱讀!關于php-resque如何搭建基于Redis的后臺任務系統就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。