Back

delayed_job + god 组合使用 ( using delayed_job with god )

发布时间: 2014-05-13 01:06:00

项目中用到了delayed_job 这个神器。  缺点是它有时候自己遇到 mysql_connection_error 就会关掉。  ( my project heavily used delayed_job, and we found it will stop if met some error or exceptions. and I want to restart it via web GUI http request ) 

于是我想到用god来控制它。  ( so it's time to use GOD (http://godrb.com ) 

god 的脚本针对两种进程: 一种是普通的进程, 一种是daemon进程。  delayed_job的脚本运行后,是daemon进程,所以它的god配置文件就略有不同( 注意其中的pid 属性)   ( there are 2 kind of processes in god:  normal , daemon . so the config files are different. below is the daemon config file, notice its "pid" segment )

# 使用方法:
# 1. 修改 RAILS_ROOT
# 2. 修改 start 命令和 log 日志的地点
# 3. 调试时,记得有4分钟的延迟时间。不要问为什么delay_job脚本启动了但是SQL中仍然有JOB记录。
# 4. 而且 delayed_job 有自己的 sleep_time, 默认是60秒。
RAILS_ROOT = '/sg552/workspace/m-api-cache-cleaner'
God.watch do |w| 
  w.name = 'cache_cleaner'
  w.dir = RAILS_ROOT
  w.start = "cd #{RAILS_ROOT} && RAILS_ENV=production bundle exec bin/delayed_job -n 5 start"
  w.stop = "cd #{RAILS_ROOT} && RAILS_ENV=production bundle exec bin/delayed_job stop"
  w.restart = "cd #{RAILS_ROOT} && RAILS_ENV=production bundle exec bin/delayed_job -n 5 restart"
  w.log = "#{RAILS_ROOT}/log/cache_cleaner_stdout.log"
  # 根据实际使用的效果,这个参数,god 是不会建立pid_file的
  # 因为 delayed_job -n 5 start 会生成自己的PID文件
  # 但是我们仍然要给它一个随便的值,这标志该任务是 “有自己的daemon“的任务
  w.pid_file = File.join(RAILS_ROOT, "log/delayed_job.total.pid")
  # 不需要为 delayed_job 指定这个参数。因为delayed_job有休眠时间。god会不断的唤醒它。。
  w.keepalive
end

一点儿经验: 由于pid文件没有正常生成,所以 god不会实时监控,并且 keepalive对应的进程(也就是说,我把5个delayed_job 干掉一个之后, god不会自动的重启它)      ( because the god "pid" file is omitted,  god won't keep an eye on the stopped process ) 

但是一个变通方法是:  (a workaround to keep alive delayed_job child-processes: ) 

$ god restart cache_cleaner

在god运行状态下,我们可以这样加载:  ( we could load config file in run-time) 

$ god stop cache_cleaner
$ god remove cache_cleaner
$ god load /opt/app/path/cache_cleaner.god
$ god start cache_cleaner
$ god status
cache_cleaner: start 

现在可以看到, 5个delayed_job 都已经跑起来了:  ( now we see all the 5 processes running) 

09:04 [[email protected]]$ ps -ef | grep delayed
root      3683 31684  0 08:54 pts/4    00:00:00 tail delayed_job.log -f
root      7842     1  4 09:04 ?        00:00:18 delayed_job.1                    
root      7843     1  4 09:04 ?        00:00:19 delayed_job.0                    
root      7872     1  5 09:04 ?        00:00:20 delayed_job.2                    
root      7940     1  5 09:05 ?        00:00:19 delayed_job.3                    
root      7956     1  5 09:05 ?        00:00:19 delayed_job.4      

Back