diff --git a/lib/clockwork/database_events/manager.rb b/lib/clockwork/database_events/manager.rb index 3231d6f..6141255 100644 --- a/lib/clockwork/database_events/manager.rb +++ b/lib/clockwork/database_events/manager.rb @@ -8,7 +8,7 @@ def unregister(event) @events.delete(event) end - def register(period, job, block, options) + def register(period, job, block, options, _skip_duplicate_check = false) @events << if options[:from_database] synchronizer = options.fetch(:synchronizer) model_attributes = options.fetch(:model_attributes) diff --git a/lib/clockwork/manager.rb b/lib/clockwork/manager.rb index 55ec497..65584a3 100644 --- a/lib/clockwork/manager.rb +++ b/lib/clockwork/manager.rb @@ -1,11 +1,14 @@ module Clockwork class Manager class NoHandlerDefined < RuntimeError; end + class DuplicateEventNames < RuntimeError; end attr_reader :config def initialize @events = [] + @event_names = [] + @incrementer = 0 @callbacks = {} @config = default_configuration @handler = nil @@ -50,6 +53,12 @@ def every(period, job='unnamed', options={}, &block) options = job job = "unnamed" end + + if job == "unnamed" + job += "_#{@incrementer}" + @incrementer += 1 + end + if options[:at].respond_to?(:each) every_with_multiple_times(period, job, options, &block) else @@ -163,8 +172,12 @@ def events_to_run(t) @events.select{ |event| event.run_now?(t) } end - def register(period, job, block, options) + def register(period, job, block, options, skip_duplicate_check = false) event = Event.new(self, period, job, block || handler, options) + if @event_names.include?(job) + raise(DuplicateEventNames, "#{job} is identical to another event") unless skip_duplicate_check + end + @event_names << job @events << event event end @@ -173,7 +186,7 @@ def every_with_multiple_times(period, job, options={}, &block) each_options = options.clone options[:at].each do |at| each_options[:at] = at - register(period, job, block, each_options) + register(period, job, block, each_options, true) end end end diff --git a/test/database_events/synchronizer_test.rb b/test/database_events/synchronizer_test.rb index 210c4d1..3f9ba80 100644 --- a/test/database_events/synchronizer_test.rb +++ b/test/database_events/synchronizer_test.rb @@ -164,7 +164,7 @@ def log(msg); end # silence log output end describe "when #name is defined" do - it 'runs daily event with at from databse only once' do + it 'runs daily event with at from database only once' do DatabaseEventModel.create(:frequency => 1.day, :at => next_minute(@now).strftime('%H:%M')) setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run) diff --git a/test/manager_test.rb b/test/manager_test.rb index c102c43..7eef6ac 100644 --- a/test/manager_test.rb +++ b/test/manager_test.rb @@ -80,6 +80,13 @@ def assert_wont_run(t) end end + it "aborts when there are multiple jobs with the same name" do + assert_raises(Clockwork::Manager::DuplicateEventNames) do + @manager.every(1.minute, "jobx") + @manager.every(2.minutes, "jobx") + end + end + it "general handler" do $set_me = 0 @manager.handler { $set_me = 1 } @@ -155,12 +162,28 @@ def assert_wont_run(t) it "should accept unnamed job" do event = @manager.every(1.minute) - assert_equal 'unnamed', event.job + assert_equal 'unnamed_0', event.job end it "should accept options without job name" do event = @manager.every(1.minute, {}) - assert_equal 'unnamed', event.job + assert_equal 'unnamed_0', event.job + end + + it "should accept multiple unnamed jobs" do + event = @manager.every(1.minute) + event2 = @manager.every(2.minutes) + + assert_equal "unnamed_0", event.job + assert_equal "unnamed_1", event2.job + end + + it "should accept multiple options without job name" do + event = @manager.every(1.minute, {}) + event2 = @manager.every(2.minute, {}) + + assert_equal "unnamed_0", event.job + assert_equal "unnamed_1", event2.job end describe ':at option' do