简体   繁体   English

使用Capistrano开始后台任务

[英]Starting background tasks with Capistrano

For my RubyOnRails-App I have to start a background job at the end of Capistrano deployment. 对于我的RubyOnRails-App,我必须在Capistrano部署结束时开始后台工作。 For this, I tried the following in deploy.rb: 为此,我在deploy.rb中尝试了以下内容:

run "nohup #{current_path}/script/runner -e production 'Scheduler.start' &", :pty => true

Sometimes this works, but most of the time it does not start the process (= not listed in ps -aux). 有时它可以工作,但大多数情况下它不会启动过程(=未在ps -aux中列出)。 And there are no error messages. 并且没有错误消息。 And there is no nohup.out, not in the home directory and not in the rails app directory. 没有nohup.out,不在主目录中,也不在rails app目录中。

I tried using trap('SIGHUP', 'IGNORE') in scheduler.rb instead of nohup, but the result is the same. 我尝试在scheduler.rb而不是nohup中使用trap('SIGHUP','IGNORE'),但结果是一样的。

The only way to get it work is removing the ":pty => true" and do a manual Ctrl-C at the end of "cap deploy". 让它工作的唯一方法是删除“:pty => true”并在“cap deploy”结束时执行手动Ctrl-C。 But I don't like this... 但我不喜欢这个......

Are there any other chances to invoke this Scheduler.start? 还有其他机会调用此Scheduler.start吗? Or to get some more error messages? 或者获取更多错误消息?

I'm using Rails 2.3.2, Capistrano 2.5.8, Ubuntu Hardy on the Server 我在服务器上使用Rails 2.3.2,Capistrano 2.5.8,Ubuntu Hardy

With :pty => true, user shell start-up scripts (eg bashrc, etc.) are (usually) not loaded. 使用:pty => true,(通常)不加载用户shell启动脚本(例如bashrc等)。 My ruby program exited right after launching because of the lack of dependent environment variables. 由于缺乏依赖的环境变量,我的ruby程序在启动后立即退出。

Without :pty => true, as you described in the question, capistrano hangs there waiting for the process to exit. 如果没有:pty => true,正如你在问题中描述的那样,capistrano会在那里等待进程退出。 You'll need to redirect both stdout and stderr to make it return immediately. 您需要重定向stdout和stderr以使其立即返回。

run 'nohup ruby -e "sleep 5" &' # hangs for 5 seconds
run 'nohup ruby -e "sleep 5" > /dev/null &' # hangs for 5 seconds
run 'nohup ruby -e "sleep 5" > /dev/null 2>&1 &' # returns immediately. good.

If your background task still doesn't run. 如果您的后台任务仍未运行。 Try redirecting stdout and stderr to a log file so that you can investigate the output. 尝试将stdout和stderr重定向到日志文件,以便您可以调查输出。

I'd like to share my solution which also works when executing multiple commands. 我想分享我的解决方案,该解决方案在执行多个命令时也有效。 I tried many other variants found online, including the "sleep N" hack. 我在网上尝试了许多其他变种,包括“睡眠N”黑客。

run("nohup sh -c 'cd #{release_path} && bundle exec rake task_namespace:task_name RAILS_ENV=production > ~/shared/log/<rakelog>.log &' > /dev/null 2>&1", :pty => true)

This is a dup response launching background process in capistrano task but want to make sure others and myself can google for this solution. 这是在capistrano任务中启动后台进程的重复响应但是想确保其他人和我自己可以谷歌寻求此解决方案。

Do you want your Scheduler job to run continually in the background and get restarted when you run Capistrano? 您是否希望Scheduler作业在后台持续运行并在运行Capistrano时重新启动?

If so, then for that I use runit http://smarden.sunsite.dk/runit/ and DelayedJob http://github.com/Shopify/delayed_job/tree/master 如果是这样,那么我使用runit http://smarden.sunsite.dk/runit/和DelayedJob http://github.com/Shopify/delayed_job/tree/master

  1. Install runit in the mode of not replacing init 以不替换init的方式安装runit
  2. Add your background job as a runit service and add the log monitor for it from runit. 将后台作业添加为runit服务,并从runit添加日志监视器。
  3. Have Capistrano call sudo sv kill job_name to kill and restart the job. 让Capistrano调用sudo sv kill job_name来杀死并重新启动作业。

My backround job is an instance of the Rails plugin DelayedJob which handles background Rails tasks. 我的backround作业是Rails插件DelayedJob的一个实例,它处理后台Rails任务。 I kill it with every Capistrano deploy so it will restart with the updated code base. 我用每个Capistrano部署杀了它,所以它将使用更新的代码库重新启动。

This has proved to be very reliable. 事实证明这非常可靠。

HTH, HTH,

Larry 拉里

If this task scheduler has a -d switch it will work. 如果此任务调度程序具有-d开关,则它将起作用。 For example passenger standalone has a -d option to start it as a demonized process. 例如,passenger standalone有一个-d选项可以将其作为妖魔化进程启动。

namespace :passenger_standalone do
  task :start do
    run "cd #{current_path} && passenger start -e #{rails_env} -d"
  end
  task :stop do
    run "cd #{current_path} && RAILS_ENV=#{rails_env} passenger stop"
  end
  task :restart do
    stop
    start
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM