部署 capistrano nohup 启动任务的问题

yakjuly · 2012年01月04日 · 最后由 yakjuly 回复于 2012年02月04日 · 4057 次阅读

用 capistrano 启动 delayed_job 任务的时候,无法起作用,可能和 ssh 登陆的 交互模式有关。网上搜了下,有说 执行 run 的方法中加 :pty => true 的。我试了 都没有作用。

task :restart_delayed_job do
  #原来是想通过执行文件来重新启动的,没有效果
  # run("cd #{release_path} && chmod +x script/restart_delayed_job", :pty => true)
  # run("cd #{release_path} && ./script/restart_delayed_job", :pty => true)

  用了 pty 也没用
  run("cd #{release_path} ; nohup rake jobs:work > log/delayed_job.log 2>&1 &", :pty => true)
end

你们的这种部署是怎么写的呢?

@yorzi 这个够全,不过作者没把 script/delayed_job 脚本放上去。 找了一个 ruby 的脚本,用 daemon_spawn 做后台任务的。和 ruby-china 的启动 resque 一样。

#!/usr/bin/env ruby
# -*- ruby -*-
require 'rubygems'
require 'daemon_spawn'

RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))

class DelayedJobWorker < DaemonSpawn::Base
  def start(args)
    ENV['RAILS_ENV'] ||= args.first || 'development'
    Dir.chdir RAILS_ROOT
    require File.join('config', 'environment')

    Delayed::Worker.new.start
  end

  def stop
    system("kill `cat #{RAILS_ROOT}/tmp/pids/delayed_job.pid`")
  end
end

DelayedJobWorker.spawn!(:log_file => File.join(RAILS_ROOT, "log", "delayed_job.log"),
                        :pid_file => File.join(RAILS_ROOT, 'tmp', 'pids', 'delayed_job.pid'),
                        :sync_log => true,
                        :working_dir => RAILS_ROOT)

deploy.rb

# add this to config/deploy.rb
namespace :delayed_job do
  desc "Start delayed_job process"
  task :start, :roles => :app do
    run "cd #{current_path}; script/delayed_job start #{rails_env}"
  end

  desc "Stop delayed_job process"
  task :stop, :roles => :app do
    run "cd #{current_path}; script/delayed_job stop #{rails_env}"
  end

  desc "Restart delayed_job process"
  task :restart, :roles => :app do
    run "cd #{current_path}; script/delayed_job restart #{rails_env}"
  end
end

after "deploy:start", "delayed_job:start"
after "deploy:stop", "delayed_job:stop"
after "deploy:restart", "delayed_job:restart"
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册