From 844bbca1e397b500f77eb9d2ecdfef94376cd766 Mon Sep 17 00:00:00 2001 From: Nathan Griffith Date: Thu, 22 Jul 2021 16:39:57 -0400 Subject: [PATCH] Don't allow alert_age_percent to exceed 100 --- lib/delayed/monitor.rb | 2 +- spec/delayed/monitor_spec.rb | 32 ++++++++++++++++---------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/delayed/monitor.rb b/lib/delayed/monitor.rb index c6080e2d..d2979693 100644 --- a/lib/delayed/monitor.rb +++ b/lib/delayed/monitor.rb @@ -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 diff --git a/spec/delayed/monitor_spec.rb b/spec/delayed/monitor_spec.rb index 849b801f..5ee86890 100644 --- a/spec/delayed/monitor_spec.rb +++ b/spec/delayed/monitor_spec.rb @@ -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) } @@ -144,9 +144,9 @@ .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) @@ -154,9 +154,9 @@ .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) @@ -164,9 +164,9 @@ .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) @@ -174,9 +174,9 @@ .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 @@ -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) @@ -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) @@ -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