1:前提
系统:CentOS
PHP版本:PHP7 安装目录:/usr/local/php
php-resque 安装目录:/home/software/php-resque
worker 存放目录:/usr/local/nginx/resque-worker
2:job /home/software/php-resque/demo/queue.php
if(empty($argv[1])) { die('Specify the name of a job to add. e.g, php queue.php PHP_Job'); } require __DIR__ . '/init.php'; date_default_timezone_set('PRC'); Resque::setBackend('localhost:6379'); // You can also use a DSN-style format: //Resque::setBackend('redis://user:pass@127.0.0.1:6379'); //Resque::setBackend('redis://user:pass@a.host.name:3432/2'); $args = array( 'time' => time(), 'array' => array( 'test' => 'test', ), ); $jobId = Resque::enqueue('default', $argv[1], $args, true); echo "Queued job ".$jobId." ";
3:workers 目录:/usr/local/nginx/resque-worker
sendmail.php
<?php class SendMail{ public function perform(){ file_put_contents(__DIR__.'/sendmail.txt',date('Y-m-d H:i:s').PHP_EOL,FILE_APPEND); } }
4:添加常驻进程
#!/bin/bash # ### BEGIN INIT INFO # Provides: php-resque # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Start and stop the php-resque service daemon # Description: Controls the main php-resque service daemon "php-resque" ### END INIT INFO # export APP_INCLUDE=/usr/local/nginx/resque-worker/require.php export QUEUE=default export COUNT=1 export VVERBOSE=1 # for debugging export REDIS_BACKEND=localhost:6379 #. /etc/rc.d/init.d/functions start() { su -s /bin/bash www-data -c "/usr/local/php/bin/php /home/software/php-resque/bin/resque.php > /var/log/php-resque.log 2>&1" & #su -s /bin/bash www-data -c "/usr/local/php/bin/php /home/software/php-resque/demo/resque.php > /var/log/php-resque.log 2>&1" & } stop() { ps -ef | grep resque | grep -v grep | grep -v resque-web | awk '{print $2}' | xargs kill -15 } kill() { ps -ef | grep resque | grep -v grep | grep -v resque-web | awk '{print $2}' | xargs kill -9 } case "$1" in start) number=$(ps aux | grep php-resque/bin/resque.php | grep -v grep | wc -l) if [ $number -gt 0 ] then echo "php-resque is running. ($number workers)" echo "You may wanna stop them before you start." else start fi ;; stop) stop ;; kill) kill ;; status) number=$(ps aux | grep php-resque/bin/resque.php | grep -v grep | wc -l) if [ $number -gt 0 ] then echo "php-resque is running. ($number workers)" else echo "php-resque is not running." fi ;; *) echo -n "Usage: $0 {start|stop|status}" esac
5:移到php项目中
cp -R /home/software/php-resque /usr/local/nginx/www/protected/vendor/php-resque
6:在控制器里调用 vim /usr/local/nginx/www/protected/conterollers/ResqueController.php
/** * Description of ResqueController * * @author dxh20012012001@sina.com */ class ResqueController extends Controller { //put your code here public function actionIndex() { Yii::import('application.vendor.php-resque.vendor.autoload', true); $resque = new Resque(); $resque->setBackend('127.0.0.1:6381'); $job = $resque->enqueue('default', 'SendMail'); echo '<pre>'; print_r($job); echo '</pre>'; die('FILE:' . __FILE__ . '; LINE:' . __LINE__); } }