Skip to content

Commit

Permalink
CRM457-2094: Make critical job enqueueings atomic (#812)
Browse files Browse the repository at this point in the history
## Description of change
This means if Redis fails we won't end up in an inconsistent state.

[Link to relevant
ticket](https://dsdmoj.atlassian.net/browse/CRM457-2094)
  • Loading branch information
patrick-laa authored Oct 15, 2024
1 parent ea33be0 commit d866928
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 39 deletions.
23 changes: 14 additions & 9 deletions app/forms/nsm/make_decision_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,25 @@ class MakeDecisionForm
def save
return false unless valid?

previous_state = claim.state
Claim.transaction do
claim.data.merge!('status' => state, 'updated_at' => Time.current, 'assessment_comment' => comment)
claim.update!(state:)
::Event::Decision.build(submission: claim,
comment: comment,
previous_state: previous_state,
current_user: current_user)
claim.with_lock do
change_data_and_notify_app_store!
end
NotifyAppStore.perform_later(submission: claim)

true
end

def change_data_and_notify_app_store!
previous_state = claim.state

claim.data.merge!('status' => state, 'updated_at' => Time.current, 'assessment_comment' => comment)
claim.update!(state:)
::Event::Decision.build(submission: claim,
comment: comment,
previous_state: previous_state,
current_user: current_user)
NotifyAppStore.perform_later(submission: claim)
end

def comment
case state
when Claim::GRANTED
Expand Down
6 changes: 3 additions & 3 deletions app/forms/nsm/send_back_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ def stash
def save
return false unless valid?

Claim.transaction do
claim.with_lock do
update_local_data
end

NotifyAppStore.perform_later(submission: claim)
NotifyAppStore.perform_later(submission: claim)
end

true
end
Expand Down
28 changes: 16 additions & 12 deletions app/forms/prior_authority/decision_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,27 @@ def summary
def save
return false unless valid?

PriorAuthorityApplication.transaction do
stash(add_draft_decision_event: false)
previous_state = submission.state

submission.data.merge!('status' => pending_decision, 'updated_at' => Time.current)
submission.update!(state: pending_decision)
::Event::Decision.build(submission: submission,
comment: explanation,
previous_state: previous_state,
current_user: current_user)
submission.with_lock do
change_data_and_notify_app_store!
end

NotifyAppStore.perform_later(submission:)

true
end

def change_data_and_notify_app_store!
stash(add_draft_decision_event: false)
previous_state = submission.state

submission.data.merge!('status' => pending_decision, 'updated_at' => Time.current)
submission.update!(state: pending_decision)
::Event::Decision.build(submission: submission,
comment: explanation,
previous_state: previous_state,
current_user: current_user)

NotifyAppStore.perform_later(submission:)
end

def stash(add_draft_decision_event: true)
submission.data.merge!(attributes.except('submission', 'current_user').merge('assessment_comment' => explanation))
submission.save!
Expand Down
6 changes: 3 additions & 3 deletions app/forms/prior_authority/send_back_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ def summary
def save
return false unless valid?

PriorAuthorityApplication.transaction do
submission.with_lock do
discard_all_adjustments
stash(add_draft_send_back_event: false)
update_local_records
end

NotifyAppStore.perform_later(submission:)
NotifyAppStore.perform_later(submission:)
end

true
end
Expand Down
1 change: 1 addition & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Event < ApplicationRecord

LOCAL_EVENTS = [
'Event::DraftDecision',
'Event::NewVersion',
'Event::Edit',
'Event::Note',
'Event::UndoEdit',
Expand Down
1 change: 0 additions & 1 deletion app/models/event/new_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ class Event
class NewVersion < Event
def self.build(submission:)
create(submission: submission, submission_version: submission.current_version)
.tap(&:notify)
end

def body
Expand Down
12 changes: 9 additions & 3 deletions app/services/notify_app_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ def self.perform_later(submission:, trigger_email: true)
end

def perform(submission:, trigger_email: true)
notify(MessageBuilder.new(submission:))
# This lock is important because it forces the job to wait until
# the locked transaction that enqueued this job is released,
# ensuring that when this job runs it is working with fresh
# data
submission.with_lock do
notify(MessageBuilder.new(submission:))

send_email(submission:, trigger_email:)
submission.update!(notify_app_store_completed: true)
send_email(submission:, trigger_email:)
submission.update!(notify_app_store_completed: true)
end
end

def notify(message_builder)
Expand Down
8 changes: 0 additions & 8 deletions spec/models/event/new_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@
)
end

it 'notifies the app store' do
event = Event.send(:new)
expect(described_class).to receive(:create).and_return(event)
expect(NotifyEventAppStore).to receive(:perform_later).with(event:)

subject
end

it 'has a valid title' do
expect(subject.title).to eq('Received')
end
Expand Down
2 changes: 2 additions & 0 deletions spec/services/notify_app_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
before do
allow(described_class::MessageBuilder).to receive(:new)
.and_return(message_builder)

allow(submission).to receive(:with_lock).and_yield
end

describe '.perform_later' do
Expand Down

0 comments on commit d866928

Please sign in to comment.