From 491b64855c6a1995162441acc8408892775d76a7 Mon Sep 17 00:00:00 2001 From: Jeremy Smith Date: Sun, 6 Jul 2025 13:35:06 -0400 Subject: [PATCH 1/2] Use force: option to re-request and reprocess --- lib/active_operator/operation.rb | 18 +++++++++--------- lib/active_operator/perform_operation_job.rb | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/active_operator/operation.rb b/lib/active_operator/operation.rb index 64e588b..a4d2178 100644 --- a/lib/active_operator/operation.rb +++ b/lib/active_operator/operation.rb @@ -10,27 +10,27 @@ def received? = received_at? def processed? = processed_at? def errored? = errored_at? - def perform - request! - process! + def perform(force: false) + request!(force:) + process!(force:) rescue errored! raise end - def perform_later - ActiveOperator::PerformOperationJob.perform_later(self) + def perform_later(force: false) + ActiveOperator::PerformOperationJob.perform_later(self, force:) end - def request! - return false if received? + def request!(force: false) + return false if received? && !force update!(response: request, received_at: Time.current) end - def process! + def process!(force: false) return false if !received? - return false if processed? + return false if processed? && !force ActiveRecord::Base.transaction do process diff --git a/lib/active_operator/perform_operation_job.rb b/lib/active_operator/perform_operation_job.rb index 3ba3478..26240bf 100644 --- a/lib/active_operator/perform_operation_job.rb +++ b/lib/active_operator/perform_operation_job.rb @@ -4,8 +4,8 @@ module ActiveOperator class PerformOperationJob < ActiveJob::Base discard_on ActiveRecord::RecordNotFound - def perform(operation) - operation.perform + def perform(operation, force: false) + operation.perform(force:) end end end From d3a0ffc0b1e4838e184716fa9326a90c86583eb8 Mon Sep 17 00:00:00 2001 From: Jeremy Smith Date: Tue, 15 Jul 2025 23:02:31 -0400 Subject: [PATCH 2/2] Add tests --- test/active_operator/operation_test.rb | 31 +++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/test/active_operator/operation_test.rb b/test/active_operator/operation_test.rb index 8f22f69..bada297 100644 --- a/test/active_operator/operation_test.rb +++ b/test/active_operator/operation_test.rb @@ -51,6 +51,26 @@ class OperationTest < ActiveSupport::TestCase end end + test "#perform with force succeeds when already received and processed" do + with_operation_methods(Geocoding::V2, GeocodingValid) do + location = create_location + + first_time = 5.minutes.ago + location.geocoding.update!(received_at: first_time, processed_at: first_time) + + assert_not_nil location.geocoding.received_at + assert_not_nil location.geocoding.processed_at + + location.geocoding.perform(force: true) + + assert_equal 40.59806, location.latitude + assert_equal -74.68148, location.longitude + assert_equal "America/New_York", location.timezone + assert location.geocoding.received_at.after?(first_time) + assert location.geocoding.processed_at.after?(first_time) + end + end + test "#perform fails on invalid request" do with_operation_methods(Geocoding::V2, GeocodingInvalidRequest) do location = create_location @@ -105,9 +125,18 @@ class OperationTest < ActiveSupport::TestCase location = create_location location.geocoding.save! - assert_enqueued_with job: ActiveOperator::PerformOperationJob, args: [location.geocoding] do + assert_enqueued_with job: ActiveOperator::PerformOperationJob, args: [location.geocoding, { force: false }] do location.geocoding.perform_later end end + + test "#perform_later with force" do + location = create_location + location.geocoding.save! + + assert_enqueued_with job: ActiveOperator::PerformOperationJob, args: [location.geocoding, { force: true }] do + location.geocoding.perform_later(force: true) + end + end end end