Skip to content

Commit

Permalink
Don't allow alert_age_percent to exceed 100
Browse files Browse the repository at this point in the history
  • Loading branch information
smudge committed Jul 22, 2021
1 parent 4c61327 commit 844bbca
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/delayed/monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def max_age_grouped
def alert_age_percent_grouped
oldest_workable_job_grouped.each_with_object({}) do |job, metrics|
max_age = Job.db_time_now - job.run_at
metrics[[job.priority.to_i, job.queue]] = max_age / job.priority.alert_age * 100 if job.priority.alert_age
metrics[[job.priority.to_i, job.queue]] = [max_age / job.priority.alert_age * 100, 100].min if job.priority.alert_age
end
end

Expand Down
32 changes: 16 additions & 16 deletions spec/delayed/monitor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,19 @@
let(:p10_payload) { default_payload.merge(priority: 'user_visible') }
let(:p20_payload) { default_payload.merge(priority: 'eventual') }
let(:p30_payload) { default_payload.merge(priority: 'reporting') }
let!(:p0_workable_job) { Delayed::Job.create! p0_attributes.merge(run_at: now - 1.hour) }
let!(:p0_workable_job) { Delayed::Job.create! p0_attributes.merge(run_at: now - 30.seconds) }
let!(:p0_failed_job) { Delayed::Job.create! p0_attributes.merge(failed_attributes) }
let!(:p0_future_job) { Delayed::Job.create! p0_attributes.merge(run_at: now + 1.hour) }
let!(:p0_working_job) { Delayed::Job.create! p0_attributes.merge(locked_at: now - 3.minutes) }
let!(:p10_workable_job) { Delayed::Job.create! p10_attributes.merge(run_at: now - 2.hours) }
let!(:p10_workable_job) { Delayed::Job.create! p10_attributes.merge(run_at: now - 2.minutes) }
let!(:p10_failed_job) { Delayed::Job.create! p10_attributes.merge(failed_attributes) }
let!(:p10_future_job) { Delayed::Job.create! p10_attributes.merge(run_at: now + 1.hour) }
let!(:p10_working_job) { Delayed::Job.create! p10_attributes.merge(locked_at: now - 7.minutes) }
let!(:p20_workable_job) { Delayed::Job.create! p20_attributes.merge(run_at: now - 3.hours) }
let!(:p20_workable_job) { Delayed::Job.create! p20_attributes.merge(run_at: now - 1.hour) }
let!(:p20_failed_job) { Delayed::Job.create! p20_attributes.merge(failed_attributes) }
let!(:p20_future_job) { Delayed::Job.create! p20_attributes.merge(run_at: now + 1.hour) }
let!(:p20_working_job) { Delayed::Job.create! p20_attributes.merge(locked_at: now - 9.minutes) }
let!(:p30_workable_job) { Delayed::Job.create! p30_attributes.merge(run_at: now - 4.hours) }
let!(:p30_workable_job) { Delayed::Job.create! p30_attributes.merge(run_at: now - 6.hours) }
let!(:p30_failed_job) { Delayed::Job.create! p30_attributes.merge(failed_attributes) }
let!(:p30_future_job) { Delayed::Job.create! p30_attributes.merge(run_at: now + 1.hour) }
let!(:p30_working_job) { Delayed::Job.create! p30_attributes.merge(locked_at: now - 11.minutes) }
Expand All @@ -144,39 +144,39 @@
.and emit_notification("delayed.job.failed_count").with_payload(p0_payload).with_value(1)
.and emit_notification("delayed.job.working_count").with_payload(p0_payload).with_value(1)
.and emit_notification("delayed.job.workable_count").with_payload(p0_payload).with_value(1)
.and emit_notification("delayed.job.max_age").with_payload(p0_payload).with_value(1.hour)
.and emit_notification("delayed.job.max_age").with_payload(p0_payload).with_value(30.seconds)
.and emit_notification("delayed.job.max_lock_age").with_payload(p0_payload).with_value(3.minutes)
.and emit_notification("delayed.job.alert_age_percent").with_payload(p0_payload).with_value(1.0.hour / 1.minute * 100)
.and emit_notification("delayed.job.alert_age_percent").with_payload(p0_payload).with_value(30.0.seconds / 1.minute * 100)
.and emit_notification("delayed.job.count").with_payload(p10_payload).with_value(4)
.and emit_notification("delayed.job.future_count").with_payload(p10_payload).with_value(1)
.and emit_notification("delayed.job.locked_count").with_payload(p10_payload).with_value(2)
.and emit_notification("delayed.job.erroring_count").with_payload(p10_payload).with_value(1)
.and emit_notification("delayed.job.failed_count").with_payload(p10_payload).with_value(1)
.and emit_notification("delayed.job.working_count").with_payload(p10_payload).with_value(1)
.and emit_notification("delayed.job.workable_count").with_payload(p10_payload).with_value(1)
.and emit_notification("delayed.job.max_age").with_payload(p10_payload).with_value(2.hours)
.and emit_notification("delayed.job.max_age").with_payload(p10_payload).with_value(2.minutes)
.and emit_notification("delayed.job.max_lock_age").with_payload(p10_payload).with_value(7.minutes)
.and emit_notification("delayed.job.alert_age_percent").with_payload(p10_payload).with_value(2.0.hours / 3.minutes * 100)
.and emit_notification("delayed.job.alert_age_percent").with_payload(p10_payload).with_value(2.0.minutes / 3.minutes * 100)
.and emit_notification("delayed.job.count").with_payload(p20_payload).with_value(4)
.and emit_notification("delayed.job.future_count").with_payload(p20_payload).with_value(1)
.and emit_notification("delayed.job.locked_count").with_payload(p20_payload).with_value(2)
.and emit_notification("delayed.job.erroring_count").with_payload(p20_payload).with_value(1)
.and emit_notification("delayed.job.failed_count").with_payload(p20_payload).with_value(1)
.and emit_notification("delayed.job.working_count").with_payload(p20_payload).with_value(1)
.and emit_notification("delayed.job.workable_count").with_payload(p20_payload).with_value(1)
.and emit_notification("delayed.job.max_age").with_payload(p20_payload).with_value(3.hours)
.and emit_notification("delayed.job.max_age").with_payload(p20_payload).with_value(1.hour)
.and emit_notification("delayed.job.max_lock_age").with_payload(p20_payload).with_value(9.minutes)
.and emit_notification("delayed.job.alert_age_percent").with_payload(p20_payload).with_value(3.hours / 1.5.hours * 100)
.and emit_notification("delayed.job.alert_age_percent").with_payload(p20_payload).with_value(1.hour / 1.5.hours * 100)
.and emit_notification("delayed.job.count").with_payload(p30_payload).with_value(4)
.and emit_notification("delayed.job.future_count").with_payload(p30_payload).with_value(1)
.and emit_notification("delayed.job.locked_count").with_payload(p30_payload).with_value(2)
.and emit_notification("delayed.job.erroring_count").with_payload(p30_payload).with_value(1)
.and emit_notification("delayed.job.failed_count").with_payload(p30_payload).with_value(1)
.and emit_notification("delayed.job.working_count").with_payload(p30_payload).with_value(1)
.and emit_notification("delayed.job.workable_count").with_payload(p30_payload).with_value(1)
.and emit_notification("delayed.job.max_age").with_payload(p30_payload).with_value(4.hours)
.and emit_notification("delayed.job.max_age").with_payload(p30_payload).with_value(6.hours)
.and emit_notification("delayed.job.max_lock_age").with_payload(p30_payload).with_value(11.minutes)
.and emit_notification("delayed.job.alert_age_percent").with_payload(p30_payload).with_value(100) # 4 hours / 4 hours
.and emit_notification("delayed.job.alert_age_percent").with_payload(p30_payload).with_value(100) # 6 hours / 4 hours (overflow)
.and emit_notification("delayed.job.workable_count").with_payload(p30_payload.merge(queue: 'banana')).with_value(1)
.and emit_notification("delayed.job.max_age").with_payload(p30_payload.merge(queue: 'banana')).with_value(4.hours)
end
Expand All @@ -201,7 +201,7 @@
.and emit_notification("delayed.job.failed_count").with_payload(p0_payload).with_value(2)
.and emit_notification("delayed.job.working_count").with_payload(p0_payload).with_value(2)
.and emit_notification("delayed.job.workable_count").with_payload(p0_payload).with_value(2)
.and emit_notification("delayed.job.max_age").with_payload(p0_payload).with_value(2.hours)
.and emit_notification("delayed.job.max_age").with_payload(p0_payload).with_value(2.minutes)
.and emit_notification("delayed.job.max_lock_age").with_payload(p0_payload).with_value(7.minutes)
.and emit_notification("delayed.job.alert_age_percent").with_payload(p0_payload).with_value(0)
.and emit_notification("delayed.job.count").with_payload(p20_payload).with_value(8)
Expand All @@ -211,7 +211,7 @@
.and emit_notification("delayed.job.failed_count").with_payload(p20_payload).with_value(2)
.and emit_notification("delayed.job.working_count").with_payload(p20_payload).with_value(2)
.and emit_notification("delayed.job.workable_count").with_payload(p20_payload).with_value(2)
.and emit_notification("delayed.job.max_age").with_payload(p20_payload).with_value(4.hours)
.and emit_notification("delayed.job.max_age").with_payload(p20_payload).with_value(6.hours)
.and emit_notification("delayed.job.max_lock_age").with_payload(p20_payload).with_value(11.minutes)
.and emit_notification("delayed.job.alert_age_percent").with_payload(p20_payload).with_value(0)
.and emit_notification("delayed.job.workable_count").with_payload(p20_payload.merge(queue: 'banana')).with_value(1)
Expand All @@ -228,8 +228,8 @@

it 'emits the expected alert_age_percent results' do
expect { subject.run! }
.to emit_notification("delayed.job.alert_age_percent").with_payload(p0_payload).with_value(2.0.hours / 3.hours * 100)
.and emit_notification("delayed.job.alert_age_percent").with_payload(p20_payload).with_value(4.0.hours / 1.year * 100)
.to emit_notification("delayed.job.alert_age_percent").with_payload(p0_payload).with_value(2.0.minutes / 3.hours * 100)
.and emit_notification("delayed.job.alert_age_percent").with_payload(p20_payload).with_value(6.0.hours / 1.year * 100)
end
end
end
Expand Down

0 comments on commit 844bbca

Please sign in to comment.