安装

1
2
3
4
5
6
7
8
## 安装 Composer
curl -s http://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

## 安装 php-resque
git clone https://github.com/chrisboulton/php-resque.git
cd php-resque
composer install

Mac OS 可能会碰到这个问题

1
2
Problem 1
- The requested PHP extension ext-pcntl * is missing from your system. Install or enable PHP's pcntl extension.

解决方案: http://moffe42.blogspot.com/2012/06/installing-pcntl-for-php-on-osx-lion.html

安装 pcntl 可能还会碰到问题

解决方案: http://coolestguidesontheplanet.com/what-is-sip-in-osx-10-11-el-capitan/

使用

参照 Demo,核心文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// queue.php

if(empty($argv[1]) || empty($argv[2])) {
die('Specify the name of a job to add. e.g, php queue.php WORK_A PHP_Job');
}

require __DIR__ . '/init.php';
date_default_timezone_set('Asia/Chongqing');
Resque::setBackend('127.0.0.1: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($argv[1], $argv[2], $args, true);
echo "Queued job ".$jobId."\n\n";
1
2
3
4
5
6
7
8
9
10
11
// job.php

class PHP_Job
{
public function perform()
{
fwrite(STDOUT, 'Start job! -> ');
// Do something.
fwrite(STDOUT, 'Job ended!' . PHP_EOL);
}
}
1
2
3
4
5
## 启动 resque
## BLOCKING=TRUE INTERVAL=0 利用Redis blpop的特性
## QUEUE_A的优先级高于QUEUE_B
## 指定 Redis 地址
QUEUE=QUEUE_A,QUEUE_B REDIS_BACKEND=127.0.0.1:6380 BLOCKING=TRUE INTERVAL=0 php resque.php

安装 resque ,使用 resque-web 查看状态

1
2
3
4
5
6
7
8
## 安装 resque
sudo gem install resque

## 运行 resque-web
resque-web

## 指定端口号、redis 端口号
resque-web -p 8282 -r localhost:6380

后台运行

1
nohup bash -c "QUEUE=QUEUE_A,QUEUE_B REDIS_BACKEND=localhost:6380 BLOCKING=TRUE INTERVAL=0 php resque.php" >/dev/null 2>&1 &

查看运行状态和PID

1
ps -ef | grep resque.php

进阶

启动脚本 保存为 php-resque

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#! /bin/sh

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

PHP_RESQUE_DIR=/var/www/tasks/php-resque
RUNDIR=$PHP_RESQUE_DIR/MyResque

export APP_INCLUDE=$RUNDIR/jobs.php
export QUEUE=QUEUE_A,QUEUE_B
export COUNT=1
# export VVERBOSE=1 # for debugging
export REDIS_BACKEND=localhost:6380
export BLOCKING=TRUE
export INTERVAL=0
export PIDFILE=$RUNDIR/php-resque.pid

get_pid() {
if [ -f $PIDFILE ]; then
cat $PIDFILE
fi
}

start() {
local PID=$(get_pid)
if [ ! -z $PID ]; then
echo "php-resque($PID) is running."
echo "You should stop it before you start."
return
fi

mkdir -p $RUNDIR
touch $PIDFILE

echo "Starting php-resque..."
nohup php $PHP_RESQUE_DIR/bin/resque >/dev/null 2>&1 &
}

stop() {
local PID=$(get_pid)
if [ -z $PID ]; then
echo "php-resque is not running."
return
fi

echo "Stopping php-resque..."
get_pid | xargs kill -9
rm -f $PIDFILE
}

status() {
local PID=$(get_pid)
if [ ! -z $PID ]; then
echo "php-resque($PID) is running."
else
echo "php-resque is not running."
fi
}

case "$1" in
start)
start
;;

stop)
stop
;;

restart)
stop
start
;;

status)
status
;;

*)
echo -n "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac

exit 0
1
sudo ln -s /var/www/tasks/php-resque/MyResque/php-resque /usr/local/bin/php-resque

参考链接: http://blog.hsatac.net/2012/01/php-resque-introduction/
http://avnpc.com/pages/run-background-task-by-php-resque