Skip to content

Commit

Permalink
Jonathan/appeals 24987 (#19003)
Browse files Browse the repository at this point in the history
* APPEALS-24987 Added redis-mutex to gemfile

* APPEALS-24987 added RedisMutex init and lock

* removed "on" lock in EPE

* added comments, retrying lock using "id"

* removing lock on "id"

* deleting extra line

* saving progress

* removing debugging lines

* changed mutex logic to use "with_lock"

* updated mutex lock key

---------

Co-authored-by: Jonathan Tsang <[email protected]>
  • Loading branch information
jtsangVA and Jonathan Tsang authored Jul 14, 2023
1 parent 3ae4068 commit 704d89f
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions app/models/end_product_establishment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,27 +211,31 @@ def cancel_unused_end_product!
end

def sync!
# There is no need to sync end_product_status if the status
# is already inactive since an EP can never leave that state
return true unless status_active?

fail EstablishedEndProductNotFound, id unless result

# load contentions now, in case "source" needs them.
# this VBMS call is slow and will cause the transaction below
# to timeout in some cases.
contentions unless result.status_type_code == EndProduct::STATUSES.key("Canceled")
RedisMutex.with_lock("EndProductEstablishment:#{id}", block: 60, expire: 100) do # key => "EndProductEstablishment:id"
# There is no need to sync end_product_status if the status
# is already inactive since an EP can never leave that state
return true unless status_active?

fail EstablishedEndProductNotFound, id unless result

# load contentions now, in case "source" needs them.
# this VBMS call is slow and will cause the transaction below
# to timeout in some cases.
contentions unless result.status_type_code == EndProduct::STATUSES.key("Canceled")

transaction do
update!(
synced_status: result.status_type_code,
last_synced_at: Time.zone.now
)
status_cancelled? ? handle_cancelled_ep! : sync_source!
close_request_issues_with_no_decision!
end

transaction do
update!(
synced_status: result.status_type_code,
last_synced_at: Time.zone.now
)
status_cancelled? ? handle_cancelled_ep! : sync_source!
close_request_issues_with_no_decision!
save_updated_end_product_code!
end

save_updated_end_product_code!
rescue RedisMutex::LockError
Rails.logger.error('failed to acquire lock! EPE sync is being called by another process. Please try again later.')
rescue EstablishedEndProductNotFound, AppealRepository::AppealNotValidToReopen => error
raise error
rescue StandardError => error
Expand Down

0 comments on commit 704d89f

Please sign in to comment.