stojg / silverstripe-resque
封装了resque代码,以便在Redis的帮助下实现后台任务的调度
Requires
- resque/php-resque: ~1.3
README
此模块封装了php-resque,以便能够通过Redis实现定时后台任务
要求
- SilverStripe 及其要求
- Redis服务器
- (可选) 工作监控器,例如god或monit,用于启动和监控工作进程。
- 此模块需要安装PHP pcntl模块
仍在开发中
原则是,'前端'通过Resque类添加任务,这是一个非常快的操作。
$args = array(
'message' => 'from '.$_SERVER['HTTP_HOST'],
'time' => date('Y-m-d H:i:s')
);
$token = Resque::enqueue("dev:ping", "SSResquePingJob", $args);
这将通过tcp发送到redis服务器,该服务器将保留这些信息,直到工作进程检索它们。
通过在cli中启动一个或多个工作进程,它们将从redis的'队列'中拉取任务,即:dev:ping
。然后它将找到一个PHP类来运行任务,SSResquePingJob
用$args
填充它,然后运行SSResquePingJob->perform()
。
public function perform() {
echo 'Ping: '.$this->args['time'].' '.$this->args['message'].PHP_EOL;
}
任何异常或错误都会将任务标记为失败,并且可以重新排队。
安装
安装redis
通过homebrew
$ brew install redis
或者通过编译它,有关更多信息,请参阅Redis快速入门。
$ sudo mkdir -p /usr/local/src
$ cd /usr/local/src
$ sudo wget http://download.redis.io/redis-stable.tar.gz
$ sudo tar xvzf redis-stable.tar.gz
$ cd redis-stable
$ sudo make
$ sudo make install
您可能已经在您喜欢的Linux发行版中找到了合适的软件包(未经测试)。
Silverstripe Resque
$ git clone git@github.com:stojg/silverstripe-resque.git resque
$ cd resque
$ composer update
使用
启动redis服务器
$ redis-server
在另一个终端窗口中,启动所有队列的一个工作进程
$ ./framework/sake dev/resque/run queue=*
在另一个终端窗口中,尝试创建一个ping任务
$ ./framework/sake dev/resque/ping
在工作进程终端中,您现在应该看到类似的内容
Ping: 2012-11-15 10:51:29 from hostname
Web界面
sudo gem install resque --no-ri -no-rdoc
进程监控器
如何在ubuntu / debian上安装god: 如何使用God监控Resque并在Ubuntu上使用Capistrano完成所有操作
示例配置
num_workers = 2
num_workers.times do |num| God.watch do |w| w.dir = "/var/www/site" w.name = "site-worker-#{num}" w.group = 'site-worker' w.interval = 30.seconds w.uid = 'www-data' w.gid = 'www-data' w.start = "/usr/bin/php ./framework/cli-script.php dev/resque/run queue=ping"
# restart if memory gets too high
w.transition(:up, :restart) do |on|
on.condition(:memory_usage) do |c|
c.above = 64.megabytes
c.times = 2
end
end
# determine the state on startup
w.transition(:init, { true => :up, false => :start }) do |on|
on.condition(:process_running) do |c|
c.running = true
end
end
# determine when process has finished starting
w.transition([:start, :restart], :up) do |on|
on.condition(:process_running) do |c|
c.running = true
c.interval = 5.seconds
end
# failsafe
on.condition(:tries) do |c|
c.times = 5
c.transition = :start
c.interval = 5.seconds
end
end
# start if process is not running
w.transition(:up, :start) do |on|
on.condition(:process_running) do |c|
c.running = false
end
end
end
end
致谢
以下是我站在巨人的肩膀上的巨人
- Chris Boulton 为PHP resque
- defunkt 原始的ruby resque
- redis 为提供出色的、稳定的和快速的关键值存储
- SilverStripe 我在这里被付薪做这类事情的地方。