Skip to content

Commit 1d88b4b

Browse files
committed
fix bug in metric for failed events
1 parent 22c328a commit 1d88b4b

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

app/models/journaled/outbox/event.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Event < Journaled.outbox_base_class_name.constantize
1919
attribute :event_data, :json
2020

2121
validates :event_type, :event_data, :partition_key, :stream_name, presence: true
22+
validate :failed_at_and_failure_reason_must_be_consistent
2223

2324
scope :ready_to_process, -> {
2425
where(failed_at: nil)
@@ -63,6 +64,14 @@ def requeue!
6364
def self.oldest_non_failed_timestamp
6465
ready_to_process.order(:id).limit(1).pick(:created_at)
6566
end
67+
68+
private
69+
70+
def failed_at_and_failure_reason_must_be_consistent
71+
if failed_at.present? != failure_reason.present?
72+
errors.add(:base, 'failed_at and failure_reason must both be present or both be absent')
73+
end
74+
end
6675
end
6776
end
6877
end

lib/journaled/outbox/metric_emitter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def calculate_queue_metrics
7373
Event.select(
7474
'COUNT(*) AS total_count',
7575
'COUNT(*) FILTER (WHERE failed_at IS NULL) AS workable_count',
76-
'COUNT(*) FILTER (WHERE failure_reason IS NOT NULL AND failed_at IS NULL) AS failed_count',
76+
'COUNT(*) FILTER (WHERE failed_at IS NOT NULL) AS failed_count',
7777
'MIN(created_at) FILTER (WHERE failed_at IS NULL) AS oldest_non_failed_timestamp',
7878
).to_sql,
7979
)

spec/lib/journaled/outbox/worker_spec.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@
341341
partition_key: 'key2',
342342
stream_name: 'test_stream',
343343
failure_reason: 'Some error',
344+
failed_at: Time.current,
344345
)
345346

346347
# Start worker, then travel forward in time
@@ -405,6 +406,7 @@
405406
event_data: { test: 'data' },
406407
partition_key: 'key2',
407408
stream_name: 'test_stream',
409+
failure_reason: 'Some error',
408410
failed_at: Time.current,
409411
)
410412

@@ -433,21 +435,20 @@
433435
expect(emitted['journaled.worker.queue_workable_count'][:value]).to eq(1) # Only non-failed events
434436
end
435437

436-
it 'counts erroring events correctly' do
438+
it 'counts failed events correctly' do
437439
Journaled::Outbox::Event.create!(
438440
event_type: 'test_event',
439441
event_data: { test: 'data' },
440442
partition_key: 'key1',
441443
stream_name: 'test_stream',
442-
failure_reason: 'Error but not failed',
443444
)
444445

445446
Journaled::Outbox::Event.create!(
446447
event_type: 'test_event',
447448
event_data: { test: 'data' },
448449
partition_key: 'key2',
449450
stream_name: 'test_stream',
450-
failure_reason: 'Error and failed',
451+
failure_reason: 'Permanent failure',
451452
failed_at: Time.current,
452453
)
453454

@@ -472,7 +473,7 @@
472473
sleep 0.1 until emitted.key?('journaled.worker.queue_failed_count') || Time.current > timeout
473474
end
474475

475-
expect(emitted['journaled.worker.queue_failed_count'][:value]).to eq(1) # Only events with error but not failed
476+
expect(emitted['journaled.worker.queue_failed_count'][:value]).to eq(1)
476477
end
477478
end
478479
end

0 commit comments

Comments
 (0)