Skip to content

Commit

Permalink
Fix infinity loop when suspend schedule has wdays and days
Browse files Browse the repository at this point in the history
  • Loading branch information
eisuke committed Jul 4, 2017
1 parent dc95ab9 commit 5ba258f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
24 changes: 21 additions & 3 deletions app/models/kuroko2/job_schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,27 @@ def suspended_all?
launch_schedule = Chrono::Schedule.new(cron)
schedule = CHRONO_SCHEDULE_METHODS.each_with_object({}) do |method, h|
h[method] = launch_schedule.send(method)
job_definition.job_suspend_schedules.each do |suspend_schedule_model|
suspend_schedule = Chrono::Schedule.new(suspend_schedule_model.cron)
h[method] = h[method] - suspend_schedule.send(method)
end

job_definition.job_suspend_schedules.each do |suspend_schedule_model|
suspend_schedule = Chrono::Schedule.new(suspend_schedule_model.cron)
CHRONO_SCHEDULE_METHODS.each do |method|
schedule[method] = schedule[method] - suspend_schedule.send(method)
end

# https://linux.die.net/man/5/crontab
# > Note: The day of a command's execution can be specified by two fields
# > day of month, and day of week. If both fields are restricted (ie, aren't *),
# > the command will be run when either field matches the current time.
# > For example, "30 4 1,15 * 5" would cause a command to be run at 4:30 am
# > on the 1st and 15th of each month, plus every Friday.
if suspend_schedule.wdays? && suspend_schedule.days?
case
when launch_schedule.wdays? && !launch_schedule.days?
schedule[:days] = []
when !launch_schedule.wdays? && launch_schedule.days?
schedule[:wdays] = []
end
end
end

Expand Down
35 changes: 35 additions & 0 deletions spec/models/job_schedule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,41 @@
end
end

context 'When suspended schedule has wdays and days' do
let(:time) { Time.new(2016, 1, 2, 10, 0) }
before do
create(:job_suspend_schedule, job_definition: definition, cron: '* * 1 * 0') # suspend every sunday or first day of month
end

context 'If the schedule has days only' do
let(:cron) { '0 10 1 * *' }
it 'returns nil' do
expect(schedule.next(time)).to be_nil
end
end

context 'If the schedule has wdays only' do
let(:cron) { '0 10 * * 0' }
it 'returns nil' do
expect(schedule.next(time)).to be_nil
end
end

context 'If the schedule has wdays and days' do
let(:cron) { '0 10 2 * 0' }
it 'returns nil' do
expect(schedule.next(time)).to eq(Time.new(2016, 2, 2, 10, 0))
end
end

context 'If the schedule has wdays and days' do
let(:cron) { '0 10 1 * 1' }
it 'returns nil' do
expect(schedule.next(time)).to eq(Time.new(2016, 1, 4, 10, 0))
end
end
end

context 'With invalid date' do
let(:cron) { '* * 31 2 *' }
let(:time) { Time.new(2016, 2, 28, 10, 0) }
Expand Down

0 comments on commit 5ba258f

Please sign in to comment.