Skip to content

Commit

Permalink
Fix GoodJob queue size/latency query for discarded jobs
Browse files Browse the repository at this point in the history
Fixes #70

Ensure compatibility with GoodJob 3 and prevent errors in GoodJob 2

GoodJob 2 does not support error_event, and will therefore not take discarded jobs into account.
  • Loading branch information
jamescook authored and mrrooijen committed Dec 12, 2024
1 parent ee41ec2 commit 1836bd6
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/hirefire/macro/good_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def job_queue_latency(*queues)
query = good_job_class
query = query.where(queue_name: queues) if queues.any?
query = query.where(performed_at: nil)
query = query.where.not(error_event: discarded_enum).or(query.where(error_event: nil)) if error_event_supported?
query = query.where(scheduled_at: ..Time.now).or(query.where(scheduled_at: nil))
query = query.order(scheduled_at: :asc, created_at: :asc)

Expand Down Expand Up @@ -55,6 +56,7 @@ def job_queue_size(*queues)
query = good_job_class
query = query.where(queue_name: queues) if queues.any?
query = query.where(performed_at: nil)
query = query.where.not(error_event: discarded_enum).or(query.where(error_event: nil)) if error_event_supported?
query = query.where(scheduled_at: ..Time.now).or(query.where(scheduled_at: nil))
query.count
end
Expand Down
15 changes: 15 additions & 0 deletions lib/hirefire/macro/helpers/good_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ def good_job_class
::GoodJob::Execution
end
end

def error_event_supported?
Gem::Version.new(::GoodJob::VERSION) >= Gem::Version.new("3.0.0")
end

[
:interrupted,
:unhandled,
:handled,
:retried,
:retry_stopped,
:discarded
].each_with_index do |event, index|
define_method(:"#{event}_enum") { index }
end
end
end
end
Expand Down
50 changes: 50 additions & 0 deletions test/hirefire/macro/test_good_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,31 @@ def test_job_queue_latency_with_unfinished_jobs
assert_equal 0, HireFire::Macro::GoodJob.job_queue_latency
end

def test_job_queue_latency_with_discarded_jobs
skip "GoodJob #{::GoodJob::VERSION} does not support error events" unless error_event_supported?
job_id = Timecop.freeze(1.minute.ago) { BasicJob.perform_later.job_id }
good_job_class.where(active_job_id: job_id).update_all(
performed_at: nil,
scheduled_at: 1.minute.ago,
finished_at: 1.minute.ago,
error_event: discarded_enum
)
assert_equal 0, HireFire::Macro::GoodJob.job_queue_latency
end

def test_job_queue_latency_with_retried_jobs
skip "GoodJob #{::GoodJob::VERSION} does not support error events" unless error_event_supported?
job_id = Timecop.freeze(1.minute.ago) { BasicJob.perform_later.job_id }
good_job_class.where(active_job_id: job_id).update_all(
performed_at: nil,
scheduled_at: 1.minute.ago,
error_event: retried_enum
)
assert_in_delta 60, HireFire::Macro::GoodJob.job_queue_latency, LATENCY_DELTA
good_job_class.where(active_job_id: job_id).update_all(performed_at: Time.now)
assert_equal 0, HireFire::Macro::GoodJob.job_queue_latency
end

def test_job_queue_size_without_jobs
assert_equal 0, HireFire::Macro::GoodJob.job_queue_size
end
Expand All @@ -66,6 +91,31 @@ def test_job_queue_size_with_unfinished_jobs
assert_equal 0, HireFire::Macro::GoodJob.job_queue_size
end

def test_job_queue_size_with_discarded_jobs
skip "GoodJob #{::GoodJob::VERSION} does not support error events" unless error_event_supported?
job_id = Timecop.freeze(1.minute.ago) { BasicJob.perform_later.job_id }
good_job_class.where(active_job_id: job_id).update_all(
performed_at: nil,
scheduled_at: 1.minute.ago,
finished_at: 1.minute.ago,
error_event: discarded_enum
)
assert_equal 0, HireFire::Macro::GoodJob.job_queue_size
end

def test_job_queue_size_with_retried_jobs
skip "GoodJob #{::GoodJob::VERSION} does not support error events" unless error_event_supported?
job_id = Timecop.freeze(1.minute.ago) { BasicJob.perform_later.job_id }
good_job_class.where(active_job_id: job_id).update_all(
performed_at: nil,
scheduled_at: 1.minute.ago,
error_event: retried_enum
)
assert_equal 1, HireFire::Macro::GoodJob.job_queue_size
good_job_class.where(active_job_id: job_id).update_all(performed_at: Time.now)
assert_equal 0, HireFire::Macro::GoodJob.job_queue_size
end

def test_deprecated_queue_method
BasicJob.perform_later
assert_equal 1, HireFire::Macro::GoodJob.queue(:default)
Expand Down

0 comments on commit 1836bd6

Please sign in to comment.