Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions lib/active_operator/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/active_operator/perform_operation_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
31 changes: 30 additions & 1 deletion test/active_operator/operation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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