1

我正在尝试为 Capistrano 3 编写发条食谱。在查看了Sidekiq 的食谱后,我想出了这个:

namespace :load do
  task :defaults do
    set :clockwork_default_hooks, -> { true }

    set :clockwork_pid,           -> { 'tmp/pids/clockwork.pid' }
    set :clockwork_log,           -> { "#{current_path}/log/clockwork.log" }

    set :clockwork_roles,         -> { :app }

    set :clockwork_config,        -> { 'clock.rb' }
  end
end

namespace :deploy do
  before :starting, :check_clockwork_hooks do
    invoke 'clockwork:add_default_hooks' if fetch(:clockwork_default_hooks)
  end
end

namespace :clockwork do
  def clockwork_pid_full_path
    if fetch(:clockwork_pid).start_with?('/')
      fetch(:clockwork_pid)
    else
      "#{current_path}/#{fetch(:clockwork_pid)}"
    end
  end

  task :add_default_hooks do
    after 'deploy:updated',   'clockwork:stop'
    after 'deploy:reverted',  'clockwork:stop'
    after 'deploy:published', 'clockwork:start'
  end

  desc 'Stop clockwork'
  task :stop do
    on roles fetch(:clockwork_roles) do
      if test "[ -f #{clockwork_pid_full_path} ]"
        within current_path do
          execute "kill -int $(cat #{clockwork_pid_full_path}) 2>/dev/null"
        end
      else
        execute "echo 'clockwork was not running'"
      end
    end
  end

  desc 'Start clockwork'
  task :start do
    on roles fetch(:clockwork_roles) do
      within current_path do
        with rails_env: fetch(:rails_env, 'production') do
          execute "export RAILS_ENV=$RAILS_ENV"
          execute :bundle, :exec, :clockwork, "#{fetch(:clockwork_config)} >> #{fetch(:clockwork_log)} 2>&1 &"
        end
        execute "ps -eo pid,command | grep clockwork | grep -v grep | awk '{print $1}' > #{clockwork_pid_full_path}"
      end
    end
  end

  desc 'Restart clockwork'
  task :restart do
    invoke 'clockwork:stop'
    invoke 'clockwork:start'
  end
end

但是,clockwork:start任务仅在我2>&1 &最后删除部分时才有效。如果我尝试在后台启动该过程,似乎什么也没有发生。

我究竟做错了什么?

4

2 回答 2

1

这就是我最终的结果。我根据我找到的Gist改编了它。

namespace :clockwork do
  desc "Stop clockwork"
  task :stop do
    on roles(:app) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :bundle, :exec, :clockworkd, "-c clock.rb --pid-dir=#{cw_pid_dir} --log-dir=#{cw_log_dir} --log stop"
        end
      end
    end
  end

  desc "Clockwork status"
  task :status do
    on roles(:app) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :bundle, :exec, :clockworkd, "-c clock.rb --pid-dir=#{cw_pid_dir} --log-dir=#{cw_log_dir} --log status"
        end
      end
    end
  end

  desc "Start clockwork"
  task :start do
    on roles(:app) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :bundle, :exec, :clockworkd, "-c clock.rb --pid-dir=#{cw_pid_dir} --log-dir=#{cw_log_dir} --log start"
        end
      end
    end
  end

  desc "Restart clockwork"
  task :restart do
    on roles(:app) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :bundle, :exec, :clockworkd, "-c clock.rb --pid-dir=#{cw_pid_dir} --log-dir=#{cw_log_dir} --log restart"
        end
      end
    end
  end

  def cw_log_dir
    "#{shared_path}/log"
  end

  def cw_pid_dir
    "#{shared_path}/tmp/pids"
  end

  def rails_env
    fetch(:rails_env, false) ? "RAILS_ENV=#{fetch(:rails_env)}" : ''
  end
end
于 2014-11-12T16:31:01.390 回答
1

试试这个宝石capistrano-clockwork

这是最简单的方法,对我来说效果很好。我尝试了几个“要点”选项,但没有成功。

您只需要更新此文件GemfileCapifile然后deploy.rb.

宝石文件:

gem 'daemons'
gem 'capistrano-clockwork', group: :development

帽文件:

require 'capistrano/clockwork'

部署.rb:

set :clockwork_file, "lib/name_of_your_clockwork_config.rb"

记得bundle install在部署之前执行;)

于 2015-11-25T19:42:48.457 回答