Skip to content

Commit e8b976e

Browse files
authored
Split up consent request and reminder jobs (#3328)
This splits the jobs in to two separate jobs each, one which runs on a schedule and for each session queues up a separate job which handles the process of sending the communications for a particular session. We're finding that the current job is unable to handle a high number of sessions and runs out of resources. Instead, by queuing a job for each session we can avoid the individual jobs being too resource hungry and gives us more flexibility in terms of running the jobs manually. As part of this change I've also renamed the other jobs for consistency, specifically jobs which are prefixed with `Enqueue` do nothing other than queue up other jobs, and jobs prefixed with `Send` do the task of actually sending out communications.
2 parents e620c90 + 330bd08 commit e8b976e

27 files changed

Lines changed: 219 additions & 102 deletions

app/jobs/clinic_session_invitations_job.rb renamed to app/jobs/enqueue_clinic_session_invitations_job.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
class ClinicSessionInvitationsJob < ApplicationJob
3+
class EnqueueClinicSessionInvitationsJob < ApplicationJob
44
queue_as :notifications
55

66
def perform
@@ -9,7 +9,7 @@ def perform
99
.includes(:programmes)
1010
.joins(:location)
1111
.merge(Location.clinic)
12-
.each do |session|
12+
.find_each do |session|
1313
# We're only inviting patients who don't have a school.
1414
# Patients who have a school are sent invitations manually by the
1515
# nurse when they're finished at a school.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
class EnqueueSchoolConsentRemindersJob < ApplicationJob
4+
queue_as :notifications
5+
6+
def perform
7+
sessions =
8+
Session.send_consent_reminders.joins(:location).merge(Location.school)
9+
10+
sessions.find_each do |session|
11+
SendSchoolConsentRemindersJob.perform_later(session)
12+
end
13+
end
14+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
class EnqueueSchoolConsentRequestsJob < ApplicationJob
4+
queue_as :notifications
5+
6+
def perform
7+
sessions =
8+
Session.send_consent_requests.joins(:location).merge(Location.school)
9+
10+
sessions.find_each do |session|
11+
SendSchoolConsentRequestsJob.perform_later(session)
12+
end
13+
end
14+
end

app/jobs/bulk_update_patients_from_pds_job.rb renamed to app/jobs/enqueue_update_patients_from_pds_job.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
class BulkUpdatePatientsFromPDSJob < ApplicationJob
3+
class EnqueueUpdatePatientsFromPDSJob < ApplicationJob
44
include GoodJob::ActiveJobExtensions::Concurrency
55

66
queue_as :pds

app/jobs/school_consent_reminders_job.rb renamed to app/jobs/send_school_consent_reminders_job.rb

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
# frozen_string_literal: true
22

3-
class SchoolConsentRemindersJob < ApplicationJob
3+
class SendSchoolConsentRemindersJob < ApplicationJob
44
queue_as :notifications
55

6-
def perform
7-
sessions =
8-
Session
9-
.send_consent_reminders
10-
.joins(:location)
11-
.includes(:session_dates, :programmes, :patient_sessions, :location)
12-
.merge(Location.school)
6+
def perform(session)
7+
return unless session.school? && session.open_for_consent?
138

14-
sessions.find_each(batch_size: 1) do |session|
15-
next unless session.open_for_consent?
16-
17-
session.patient_sessions.each do |patient_session|
9+
session
10+
.patient_sessions
11+
.includes_programmes
12+
.includes(patient: %i[consent_notifications consents vaccination_records])
13+
.find_each do |patient_session|
1814
ProgrammeGrouper
1915
.call(patient_session.programmes)
2016
.each_value do |programmes|
@@ -39,7 +35,6 @@ def perform
3935
)
4036
end
4137
end
42-
end
4338
end
4439

4540
def should_send_notification?(patient_session:, programmes:)

app/jobs/school_consent_requests_job.rb renamed to app/jobs/send_school_consent_requests_job.rb

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
# frozen_string_literal: true
22

3-
class SchoolConsentRequestsJob < ApplicationJob
3+
class SendSchoolConsentRequestsJob < ApplicationJob
44
queue_as :notifications
55

6-
def perform
7-
sessions =
8-
Session
9-
.send_consent_requests
10-
.joins(:location)
11-
.includes(:session_dates, :programmes, :patient_sessions, :location)
12-
.merge(Location.school)
6+
def perform(session)
7+
return unless session.school? && session.open_for_consent?
138

14-
sessions.find_each(batch_size: 1) do |session|
15-
next unless session.open_for_consent?
16-
17-
session.patient_sessions.each do |patient_session|
9+
session
10+
.patient_sessions
11+
.includes_programmes
12+
.includes(patient: %i[consent_notifications consents vaccination_records])
13+
.find_each do |patient_session|
1814
ProgrammeGrouper
1915
.call(patient_session.programmes)
2016
.each_value do |programmes|
@@ -30,7 +26,6 @@ def perform
3026
)
3127
end
3228
end
33-
end
3429
end
3530

3631
def should_send_notification?(patient:, programmes:)

app/jobs/school_session_reminders_job.rb renamed to app/jobs/send_school_session_reminders_job.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
class SchoolSessionRemindersJob < ApplicationJob
3+
class SendSchoolSessionRemindersJob < ApplicationJob
44
queue_as :notifications
55

66
def perform
@@ -16,7 +16,7 @@ def perform
1616
.merge(Session.has_date(date))
1717
.notification_not_sent(date)
1818

19-
patient_sessions.each do |patient_session|
19+
patient_sessions.find_each do |patient_session|
2020
next unless should_send_notification?(patient_session:)
2121

2222
SessionNotification.create_and_send!(

app/jobs/vaccination_confirmations_job.rb renamed to app/jobs/send_vaccination_confirmations_job.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
class VaccinationConfirmationsJob < ApplicationJob
3+
class SendVaccinationConfirmationsJob < ApplicationJob
44
include VaccinationMailerConcern
55

66
queue_as :notifications

config/environments/production.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,25 +97,20 @@
9797

9898
config.good_job.enable_cron = true
9999
config.good_job.cron = {
100-
bulk_update_patients_from_pds: {
101-
cron: "every day at 6:00 and 18:00",
102-
class: "BulkUpdatePatientsFromPDSJob",
103-
description: "Keep patient details up to date with PDS."
104-
},
105-
clinic_invitation: {
100+
clinic_session_invitations: {
106101
cron: "every day at 9am",
107-
class: "ClinicSessionInvitationsJob",
102+
class: "EnqueueClinicSessionInvitationsJob",
108103
description: "Send school clinic invitation emails to parents"
109104
},
110-
consent_request: {
105+
school_consent_requests: {
111106
cron: "every day at 4pm",
112-
class: "SchoolConsentRequestsJob",
107+
class: "EnqueueSchoolConsentRequestsJob",
113108
description:
114109
"Send school consent request emails to parents for each session"
115110
},
116111
consent_reminder: {
117-
cron: "every day at 4pm",
118-
class: "SchoolConsentRemindersJob",
112+
cron: "every day at 9am",
113+
class: "SendSchoolConsentReminderJob",
119114
description:
120115
"Send school consent reminder emails to parents for each session"
121116
},
@@ -125,9 +120,9 @@
125120
description:
126121
"Invalidate all self-consents and associated triage for the previous day"
127122
},
128-
session_reminder: {
123+
school_session_reminders: {
129124
cron: "every day at 9am",
130-
class: "SchoolSessionRemindersJob",
125+
class: "SendSchoolSessionRemindersJob",
131126
description: "Send school session reminder emails to parents"
132127
},
133128
remove_import_csv: {
@@ -145,9 +140,14 @@
145140
class: "TrimActiveRecordSessionsJob",
146141
description: "Remove ActiveRecord sessions older than 30 days"
147142
},
143+
update_patients_from_pds: {
144+
cron: "every day at 6:00 and 18:00",
145+
class: "EnqueueUpdatePatientsFromPDSJob",
146+
description: "Keep patient details up to date with PDS."
147+
},
148148
vaccination_confirmations: {
149149
cron: "every day at 7pm",
150-
class: "VaccinationConfirmationsJob",
150+
class: "SendVaccinationConfirmationsJob",
151151
description: "Send vaccination confirmation emails to parents"
152152
}
153153
}

spec/features/doubles_vaccination_administered_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def then_i_see_the_patient_is_vaccinated_for_td_ipv
133133
end
134134

135135
def when_vaccination_confirmations_are_sent
136-
VaccinationConfirmationsJob.perform_now
136+
SendVaccinationConfirmationsJob.perform_now
137137
end
138138

139139
def then_an_email_is_sent_to_the_parent_confirming_the_vaccinations

0 commit comments

Comments
 (0)