Search code examples
ruby-on-railsrubyrubygemsruby-on-rails-5clockwork

Rails Clockwork events getting called twice locally


When I run my clockwork file, events are registered twice. Am I doing something wrong?

CLI I'm running locally to test: clockwork app/lib/clockwork/clock.rb

clock.rb:

require 'clockwork'
require 'active_support/time'
require './config/boot'
require './config/environment'

module Clockwork
  handler do |job, time|
    puts "Running #{job} at #{time}"
    puts
  end

  every(1.hour, 'test') do 
    puts 'RUNNING TEST'
  end
end

The output:

I, INFO -- : Starting clock for 2 events: [ test test ]
I, INFO -- : Triggering 'test'
BLOCK RUNNING TEST
I, INFO -- : Triggering 'test'
BLOCK RUNNING TEST

Solution

  • After a good amount of debugging I found it was because in my development.rb file I had:

    config.eager_load = true

    Which, combined with the

    require './config/boot'
    require './config/environment'
    

    Ran the clock file twice. I believe this is because I'm preloading the app (eager_load), and then loading again with the require statements?

    Anyways, the eager_load is ignored when clockwork is ran from my procfile (tested with $ heroku local), so it works fine.

    Hope this helps anyone as I was stuck on this for a while!