Skip to content

Commit 7478171

Browse files
authored
Merge pull request #5260 from nhsuk/fix-race-condition-mav-906
Fix race condition when committing patient imports
2 parents a8409cb + cf86149 commit 7478171

4 files changed

Lines changed: 34 additions & 27 deletions

File tree

app/controllers/class_imports_controller.rb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,9 @@ def approve
107107
@class_import.reviewed_at << Time.zone.now
108108
@class_import.committing!
109109

110-
@class_import
111-
.changesets
112-
.from_file
113-
.ready_for_review
114-
.in_batches(of: 100) do |batch|
115-
CommitPatientChangesetsJob.perform_async(batch.ids)
116-
end
117-
118-
@class_import.changesets.from_file.ready_for_review.each(&:committing!)
110+
@class_import.commit_changesets(
111+
@class_import.changesets.from_file.ready_for_review
112+
)
119113

120114
redirect_to imports_path, flash: { info: "Import started" }
121115
end

app/controllers/cohort_imports_controller.rb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,9 @@ def approve
106106
@cohort_import.reviewed_at << Time.zone.now
107107
@cohort_import.committing!
108108

109-
@cohort_import
110-
.changesets
111-
.from_file
112-
.ready_for_review
113-
.in_batches(of: 100) do |batch|
114-
CommitPatientChangesetsJob.perform_async(batch.ids)
115-
end
116-
117-
@cohort_import.changesets.from_file.ready_for_review.each(&:committing!)
109+
@cohort_import.commit_changesets(
110+
@cohort_import.changesets.from_file.ready_for_review
111+
)
118112

119113
redirect_to imports_path, flash: { info: "Import started" }
120114
end

app/jobs/commit_patient_changesets_job.rb

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,10 @@ def perform(patient_changeset_ids)
4141
end
4242
end
4343

44-
if import.changesets.committing.none?
45-
if import.changesets.needs_re_review.any?
46-
trigger_re_review(import)
47-
else
48-
import.update_columns(processed_at: Time.zone.now, status: :processed)
49-
end
50-
import.postprocess_rows!
51-
reset_counts(import)
52-
import.update_columns(**counts)
44+
if finished_committing_changesets?(import)
45+
run_post_commit_tasks(import, counts)
5346
end
47+
5448
SyncPatientTeamJob.perform_later(SchoolMove, imported_school_move_ids)
5549
import.post_commit!
5650
end
@@ -93,6 +87,22 @@ def review_consistent?(changeset)
9387
!inconsistent
9488
end
9589

90+
def finished_committing_changesets?(import)
91+
import.changesets.committing.none?
92+
end
93+
94+
# Tasks that get run after all the other batches have run
95+
def run_post_commit_tasks(import, counts)
96+
if import.changesets.needs_re_review.any?
97+
trigger_re_review(import)
98+
else
99+
import.update_columns(processed_at: Time.zone.now, status: :processed)
100+
end
101+
import.postprocess_rows!
102+
reset_counts(import)
103+
import.update_columns(**counts)
104+
end
105+
96106
def trigger_re_review(import)
97107
import.calculating_re_review!
98108
import.changesets.needs_re_review.each do |changeset|

app/models/patient_import.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ def validate_changeset_uniqueness!
7777
end
7878
end
7979

80+
def commit_changesets(changesets)
81+
changesets_ids = changesets.ids
82+
83+
changesets.update_all(status: :committing)
84+
changesets_ids.each_slice(100) do |batch_ids|
85+
CommitPatientChangesetsJob.perform_async(batch_ids)
86+
end
87+
end
88+
8089
private
8190

8291
def check_rows_are_unique

0 commit comments

Comments
 (0)