Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce uniqueness of job names #27

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/clockwork/database_events/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 15 additions & 2 deletions lib/clockwork/manager.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/database_events/synchronizer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
27 changes: 25 additions & 2 deletions test/manager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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
Expand Down