Search code examples
ruby-on-railsrubyherokuruby-on-rails-3.2clockwork

Rails clockwork not running code


I'm using the gem 'clockwork' in a Rails 3.2 app. The app is running on Heroku. The clock job runs at 1am - but it's not executing the code.

Here is the clock.rb code:

  Clockwork.every(1.day, 'DailyJob', :at => '01:00'){
    StatsMailer.stats_email.deliver
    Forecast.where(:run_date => Date.today).each  do |forecast|
      woschedules_run_jobplans_path(:id => forecast.woschedule_id)
    end
  }

The log shows:

Sep 04 00:00:05 ndeavor-staging app/clock.1: #<NoMethodError: undefined method `woschedules_run_jobplans_path' for Clockwork:Module>

If I rake routes, I get:

woschedules_run_jobplans GET   /woschedules/run_jobplans(.:format) woschedules#run_jobplans

Solution

  • The error is in that the route is non-existant. Since the route DOES exist, this problem usually means that the Rails routes have not been loaded into the current scope.

    You may need to include Rails' route helpers in order for it to function properly: Rails.application.routes.url_helpers

    Clockwork.every(1.day, 'DailyJob', :at => '01:00'){
      StatsMailer.stats_email.deliver
      Forecast.where(:run_date => Date.today).each  do |forecast|
        # Prepend your route with the following:
        Rails.application.routes.url_helpers.woschedules_run_jobplans_path(:id => forecast.woschedule_id)
      end
    }
    

    Alternatively, to dry it up a bit you can simply include all of the route helpers at the beginning of the file using:

    # Beginning of file
    include Rails.application.routes.url_helpers
    
    # ...