Skip to content

Instantly share code, notes, and snippets.

@vjfrancois
Forked from rogercampos/clockwork.rb
Last active August 6, 2016 15:29
Show Gist options
  • Save vjfrancois/9951002 to your computer and use it in GitHub Desktop.
Save vjfrancois/9951002 to your computer and use it in GitHub Desktop.
Clockwork and delayed_job tasks file for capistrano 3
namespace :workers do
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 lib/clockwork.rb --pid-dir=#{cw_pid_dir} --log-dir=#{cw_log_dir} 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 lib/clockwork.rb --pid-dir=#{cw_pid_dir} --log-dir=#{cw_log_dir} 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 lib/clockwork.rb --pid-dir=#{cw_pid_dir} --log-dir=#{cw_log_dir} 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 lib/clockwork.rb --pid-dir=#{cw_pid_dir} --log-dir=#{cw_log_dir} 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
namespace :delayed_job do
def args
fetch(:delayed_job_args, "") #Useful for queuing on specific servers
end
def delayed_job_roles
fetch(:delayed_job_server_role, :app)
end
desc 'Stop the delayed_job process'
task :stop do
on roles(delayed_job_roles) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :bundle, :exec, :'script/delayed_job', :stop
end
end
end
end
desc 'Start the delayed_job process'
task :start do
on roles(delayed_job_roles) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :bundle, :exec, :'script/delayed_job', args, :start
end
end
end
end
desc 'Restart the delayed_job process'
task :restart do
on roles(delayed_job_roles) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :bundle, :exec, :'script/delayed_job', args, :restart
end
end
end
end
end
end
@fred
Copy link

fred commented Apr 22, 2014

thanks, working great with Capistrano 3.
do you mind I make a gem out of it? with proper credits of course.
cheers

@emerak
Copy link

emerak commented Nov 13, 2014

I have an error with this config, do you think you can take a look?

require_relative "../config/boot"
require_relative "../config/environment"
require 'clockwork'
module Clockwork
handler do |job|
BlackList.last_black_lists.each do |list|
updating_period = list.updating_period
if job == updating_period
BlackListWorker.perform_async(list.provider, BlackList::LIST_URL[list.provider], list.updating_type) if (list.updating_type == 'aut' && ['pro_onu', 'pro_ofac'].include?(list.provider))
list.updated = false if Time.now > ( list.created_at + eval(updating_period) )
list.save
end
end
end

every(1.week, '1.week', at: '00:30')
every(2.weeks, '2.weeks', at: '00:30')
every(1.month, '1.month', at: '00:30')
every(2.months,'2.months', at: '00:30')
every(3.months,'3.months', at: '00:30')
every(4.months,'4.months', at: '00:30')
every(6.months,'6.months', at: '00:30')
every(1.year, '1.year', at: '00:30')
end

And then I bundle exec I get this error

read': No such file or directory @ rb_sysopen - config/redis.yml (Errno::ENOENT)
from /home/alejandra/we-send-it-provider/config/application.rb:12:in <class:Application>' from /home/alejandra/we-send-it-provider/config/application.rb:10:inmodule:WeSendItProvider'
from /home/alejandra/we-send-it-provider/config/application.rb:9:in <top (required)>' from /home/alejandra/we-send-it-provider/config/environment.rb:2:inrequire'
from /home/alejandra/we-send-it-provider/config/environment.rb:2:in <top (required)>' from /home/alejandra/we-send-it-provider/lib/clockwork.rb:2:inrequire_relative'
from /home/alejandra/we-send-it-provider/lib/clockwork.rb:2:in <top (required)>' from /home/alejandra/.rvm/gems/ruby-2.1.1@wsprovider/gems/clockwork-1.0.0/bin/clockworkd:88:inrequire'
from /home/alejandra/.rvm/gems/ruby-2.1.1@wsprovider/gems/clockwork-1.0.0/bin/clockworkd:88:in block in <top (required)>' from /home/alejandra/.rvm/gems/ruby-2.1.1@wsprovider/gems/daemons-1.1.9/lib/daemons/application.rb:255:incall'
from /home/alejandra/.rvm/gems/ruby-2.1.1@wsprovider/gems/daemons-1.1.9/lib/daemons/application.rb:255:in block in start_proc' from /home/alejandra/.rvm/gems/ruby-2.1.1@wsprovider/gems/daemons-1.1.9/lib/daemons/daemonize.rb:82:incall'
from /home/alejandra/.rvm/gems/ruby-2.1.1@wsprovider/gems/daemons-1.1.9/lib/daemons/daemonize.rb:82:in call_as_daemon' from /home/alejandra/.rvm/gems/ruby-2.1.1@wsprovider/gems/daemons-1.1.9/lib/daemons/application.rb:259:instart_proc'
from /home/alejandra/.rvm/gems/ruby-2.1.1@wsprovider/gems/daemons-1.1.9/lib/daemons/application.rb:296:in start' from /home/alejandra/.rvm/gems/ruby-2.1.1@wsprovider/gems/daemons-1.1.9/lib/daemons/controller.rb:70:inrun'
from /home/alejandra/.rvm/gems/ruby-2.1.1@wsprovider/gems/daemons-1.1.9/lib/daemons.rb:197:in block in run_proc' from /home/alejandra/.rvm/gems/ruby-2.1.1@wsprovider/gems/daemons-1.1.9/lib/daemons/cmdline.rb:109:incall'
from /home/alejandra/.rvm/gems/ruby-2.1.1@wsprovider/gems/daemons-1.1.9/lib/daemons/cmdline.rb:109:in catch_exceptions' from /home/alejandra/.rvm/gems/ruby-2.1.1@wsprovider/gems/daemons-1.1.9/lib/daemons.rb:196:inrun_proc'
from /home/alejandra/.rvm/gems/ruby-2.1.1@wsprovider/gems/clockwork-1.0.0/bin/clockworkd:84:in <top (required)>' from /home/alejandra/.rvm/gems/ruby-2.1.1@wsprovider/bin/clockworkd:23:inload'
from /home/alejandra/.rvm/gems/ruby-2.1.1@wsprovider/bin/clockworkd:23:in <main>' from /home/alejandra/.rvm/gems/ruby-2.1.1@wsprovider/bin/ruby_executable_hooks:15:ineval'
from /home/alejandra/.rvm/gems/ruby-2.1.1@wsprovider/bin/ruby_executable_hooks:15:in `

'

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