Skip to content

Commit

Permalink
Fix bug when job key is a symbol and sidekiq runs twice
Browse files Browse the repository at this point in the history
Fixes #102
  • Loading branch information
matu1104 authored and snmgian committed May 11, 2016
1 parent 27c54fa commit 8043b72
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/sidekiq-scheduler/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module Schedule
# param, otherwise params is passed in as the only parameter to perform.
def schedule=(schedule_hash)
schedule_hash = prepare_schedule(schedule_hash)
to_remove = (get_all_schedules || {}).keys - schedule_hash.keys
to_remove = (get_all_schedules || {}).keys - schedule_hash.keys.map(&:to_s)

schedule_hash.each do |name, job_spec|
set_schedule(name, job_spec)
Expand Down
23 changes: 18 additions & 5 deletions spec/sidekiq-scheduler/schedule_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
describe SidekiqScheduler::Schedule do
before { Sidekiq.redis(&:flushall) }

def build_cron_hash
{
Expand All @@ -13,7 +14,9 @@ def only_cron_and_args
end

def job_from_redis(job_id)
MultiJson.decode(job_from_redis_without_decoding(job_id))
if job = job_from_redis_without_decoding(job_id)
MultiJson.decode(job)
end
end

def changed_job?(job_id)
Expand All @@ -29,24 +32,34 @@ def job_from_redis_without_decoding(job_id)
let(:job_class_id) { cron_hash['class'] }

describe '.schedule=' do
it 'sets the schedule on redis' do
Sidekiq::Scheduler.dynamic = true
before { Sidekiq::Scheduler.dynamic = true }

it 'sets the schedule on redis' do
Sidekiq.schedule = {job_id => cron_hash}

expect(cron_hash).to eq(job_from_redis(job_id))
end

it 'uses job name as \'class\' argument if it\'s missing' do
Sidekiq::Scheduler.dynamic = true

Sidekiq.schedule = {job_class_id => cron_hash.select(&only_cron_and_args)}

job = Sidekiq.schedule[job_class_id]

expect(cron_hash).to eq(job_from_redis(job_class_id))
expect(job['class']).to eq(job_class_id)
end

context 'when job key is a symbol' do
let(:job_id) { :super_job }

context 'when schedule is set twice' do
it 'sets the schedule on redis' do
2.times { Sidekiq.schedule = { job_id => cron_hash } }

expect(cron_hash).to eq(job_from_redis(job_id))
end
end
end
end

describe '.set_schedule' do
Expand Down

0 comments on commit 8043b72

Please sign in to comment.