Rykian / clockwork

A scheduler process to replace cron.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Clockwork::Event conflicts with Event Model

wmantegna opened this issue · comments

I have a rails model named Event, which is conflicting with Clockwork::Event.

If I put my Model within a module (ie Restaurant::Event), then this works properly. I could also rename the model (I've tested this code using other models without error). Another potential solution is to rename the model, but I think it would be easier to figure out how to solve this issue at it's source. Any help on this would be greatly appreciated. Thanks!

~/app/models/event.rb:

class Event < ActiveRecord::Base
  ...
end

~/gemfile:

gem 'clockwork', '~> 2', git: 'https://github.com/Rykian/clockwork'

~/clock.rb:

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

include Clockwork

handler do |job|
  puts "Job: #{job}"
end

every(2.seconds, 'print first event') do
  puts Event.first.to_json unless Event.first.nil?
end

Terminal:

$ clockwork clock.rb
I, [2017-01-22T13:11:16.626278 #8084]  INFO -- : Starting clock for 1 events: [ print first event ]
I, [2017-01-22T13:11:16.626868 #8084]  INFO -- : Triggering 'print first event'
E, [2017-01-22T13:11:16.627195 #8084] ERROR -- : undefined method `first' for Clockwork::Event:Class (NoMethodError)
...

Hi @wmantegna, maybe try using the root namespace? (::Event)

You might also need to put the things inside module Clockwork instead of doing include Clockwork (Like the Quickstart example in the README).

That solved it! Thanks for your help!

For transparency sake, here is my updated clock.rb:

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

module Clockwork

  handler do |job|
    puts "Job: #{job}"
  end

  every(2.seconds, 'print first event') do
    puts ::Event.first.to_json unless ::Event.first.nil?
  end
end