Question

I'm using Sidekiq and Clockwork for background jobs and scheduling. My lib/clock.rb has a next code for executing some rake task:

require 'clockwork'
require File.expand_path('../../config/boot',        __FILE__)
require File.expand_path('../../config/environment', __FILE__)
require 'sidekiq'

include Clockwork
include Sidekiq

module Clockwork    
  every(1.day, 'rake places:get_from_api', at: '22:45') do
    Sidekiq.logger.info 'Starting rake places:get_from_api'
    execute_rake('places.rake', 'places:get_from_api') if Rails.env.production?
  end
end

def execute_rake(file,task)
  require 'rake'
  rake = Rake::Application.new
  Rake.application = rake
  Rake::Task.define_task(:environment)
  load "#{Rails.root}/lib/tasks/#{file}"
  rake[task].invoke
end

I deploy the application on EngineYard and have a after_restart.rb hook:

if environment == 'production'
  cw_pid_file = "#{shared_path}/pids/clockwork.pid"
  # stop clockwork
  run "if [ -d #{current_path} ] && [ -f #{cw_pid_file} ]; then cd #{current_path} && kill -int $(cat #{cw_pid_file}) 2>/dev/null; else echo 'clockwork was not running' ; fi"
  ## start clockwork
  run "cd #{current_path} && bundle exec clockworkd -c #{current_path}/lib/clock.rb --pid-dir #{shared_path}/pids --log --log-dir #{shared_path}/log restart >> log/clockwork.log 2>&1 &"
  run "ps -eo pid,command | grep clockwork | grep -v grep | awk '{print $1}' > #{cw_pid_file}"
end

After deploying I see an exception in clockworkd.clock.output log.

How to fixed it?

Was it helpful?

Solution

Engine Yard staff here.

Looking at your error, you are trying to call an each statement on Nil somewhere. Probably in your custom rake task. We can't really debug it without seeing more of your code.

Also I can recommend plain Cron tasks as a better way to run regular tasks on Engine Yard instead of using clockwork.

  1. At the bottom of your environment click on Crontabs (picture http://d.pr/i/3Yeo )
  2. Add a job by giving a job name, a command e.g. for running a rake task: cd /data/APP_NAME/current && RAILS_ENV=production /usr/local/bin/bundle exec rake task_name
  3. Give it a user and time interval and press create.
  4. Back on your environment press Apply.

If you would like a more ruby method of setting up cron tasks in rails, there is a nice gem called Whenever which allows you to write cron tasks in ruby but they get written to the crontab. A Google for whenever on Engine Yard returns some posts on how other people have went about doing it but for doing something as simple as you showed, my above instructions would probably be the easiest.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top