简体   繁体   中英

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM