Skip to content

Commit 2256373

Browse files
committed
Refactor ElasticsearchIndexJob and GeocodingJob to improve error handling and job queueing
1 parent dce0fcd commit 2256373

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

app/jobs/better_together/elasticsearch_index_job.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ module BetterTogether
55
# This job is responsible for indexing and deleting documents in Elasticsearch
66
# when records are created, updated, or destroyed.
77
class ElasticsearchIndexJob < ApplicationJob
8-
queue_as :default
8+
queue_as :es_indexing
9+
10+
# Don't retry on deserialization errors - the record no longer exists
11+
discard_on ActiveJob::DeserializationError
912

1013
def perform(record, action)
1114
return unless record.respond_to? :__elasticsearch__
@@ -18,6 +21,9 @@ def perform(record, action)
1821
else
1922
raise ArgumentError, "Unknown action: #{action}"
2023
end
24+
rescue ActiveRecord::RecordNotFound
25+
# Record was deleted before the job could run - this is expected for delete operations
26+
Rails.logger.info "ElasticsearchIndexJob: Record no longer exists, skipping #{action} operation"
2127
end
2228
end
2329
end

app/jobs/better_together/geography/geocoding_job.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ class GeocodingJob < ApplicationJob # rubocop:todo Style/Documentation
66
queue_as :geocoding
77
retry_on StandardError, wait: :polynomially_longer, attempts: 5
88

9+
# Don't retry on deserialization errors - the record no longer exists
10+
discard_on ActiveJob::DeserializationError
11+
912
def perform(geocodable)
1013
coords = geocodable.geocode
1114
geocodable.save if coords
15+
rescue ActiveRecord::RecordNotFound
16+
# Record was deleted before the job could run
17+
Rails.logger.info 'GeocodingJob: Record no longer exists, skipping geocoding operation'
1218
end
1319
end
1420
end

0 commit comments

Comments
 (0)