Skip to content

Instantly share code, notes, and snippets.

@rogercampos
Created September 6, 2012 21:56
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save rogercampos/3660663 to your computer and use it in GitHub Desktop.
Save rogercampos/3660663 to your computer and use it in GitHub Desktop.
Capistrano running clockwork as daemon
after "deploy:stop", "clockwork:stop"
after "deploy:start", "clockwork:start"
after "deploy:restart", "clockwork:restart"
namespace :clockwork do
desc "Stop clockwork"
task :stop, :roles => clockwork_roles, :on_error => :continue, :on_no_matching_servers => :continue do
run "if [ -d #{current_path} ] && [ -f #{pid_file} ]; then cd #{current_path} && kill -INT `cat #{pid
_file}` ; fi"
end
desc "Start clockwork"
task :start, :roles => clockwork_roles, :on_no_matching_servers => :continue do
run "daemon --inherit --name=clockwork --env='#{rails_env}' --output=#{log_file} --pidfile=#{pid_file
} -D #{current_path} -- bundle exec clockwork config/clockwork.rb"
end
desc "Restart clockwork"
task :restart, :roles => clockwork_roles, :on_no_matching_servers => :continue do
stop
start
end
def rails_env
fetch(:rails_env, false) ? "RAILS_ENV=#{fetch(:rails_env)}" : ''
end
def log_file
fetch(:clockwork_log_file, "#{current_path}/log/clockwork.log")
end
def pid_file
fetch(:clockwork_pid_file, "#{current_path}/tmp/pids/clockwork.pid")
end
end
@mfrederickson
Copy link

Why not use daemon for the stopping and add a status task also?

    task :stop, :roles => clockwork_roles, :on_no_matching_servers => :continue do
      run "daemon --inherit --name=clockwork --env='#{rails_env}' --output=#{log_file} --pidfile=#{pid_file} -D #{current_path} --stop || true"
    end

    desc "clockwork status"
    task :status, :roles => clockwork_roles, :on_no_matching_servers => :continue do
      run "daemon --inherit --name=clockwork --env='#{rails_env}' --output=#{log_file} --pidfile=#{pid_file} -D #{current_path} --verbose --running || true"
    end

I or || the command with true because I don't want cap to fail if the process is not running. Another change I made was I had my log_file use shared_path instead of current_path. Thanks for giving me something to start with. Of course, I had to sudo apt-get install daemon on my servers first.

@emerak
Copy link

emerak commented Nov 12, 2014

I have a few questions about this, do you think you can help me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment