Skip to content
Merged
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
14 changes: 10 additions & 4 deletions app/jobs/concerns/nhs_api_concurrency_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,26 @@ module NHSAPIConcurrencyConcern

included do
# NHS API imposes a limit of 5 requests per second
good_job_control_concurrency_with perform_throttle: [5, 1.second],
key: :nhs_api
good_job_control_concurrency_with(
perform_throttle: [concurrent_jobs_per_second, 1.second],
key: concurrency_key
)

# Because the NHS API imposes a limit of 5 requests per second, we're almost
# certain to hit throttling and the default exponential backoff strategy
# appears to trigger more race conditions in the job performing code, meaning
# there’s more instances where more than 5 requests are attempted.
retry_on GoodJob::ActiveJobExtensions::Concurrency::ConcurrencyExceededError,
attempts: :unlimited,
wait: ->(executions) { (executions * 5) + rand(0.5..5) }
wait: ->(executions) do
(executions * concurrent_jobs_per_second) + rand(0.5..5)
end

retry_on Faraday::TooManyRequestsError,
attempts: :unlimited,
wait: ->(executions) { (executions * 5) + rand(0.5..5) }
wait: ->(executions) do
(executions * concurrent_jobs_per_second) + rand(0.5..5)
end

retry_on Faraday::ServerError, wait: :polynomially_longer
end
Expand Down
3 changes: 3 additions & 0 deletions app/jobs/consent_form_matching_job.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# frozen_string_literal: true

class ConsentFormMatchingJob < ApplicationJob
def self.concurrent_jobs_per_second = 5
def self.concurrency_key = :pds

include NHSAPIConcurrencyConcern

queue_as :consents
Expand Down
3 changes: 3 additions & 0 deletions app/jobs/patient_nhs_number_lookup_job.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# frozen_string_literal: true

class PatientNHSNumberLookupJob < ApplicationJob
def self.concurrent_jobs_per_second = 5
def self.concurrency_key = :pds

include NHSAPIConcurrencyConcern

queue_as :imports
Expand Down
3 changes: 3 additions & 0 deletions app/jobs/patient_update_from_pds_job.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# frozen_string_literal: true

class PatientUpdateFromPDSJob < ApplicationJob
def self.concurrent_jobs_per_second = 5
def self.concurrency_key = :pds

include NHSAPIConcurrencyConcern

queue_as :pds
Expand Down
7 changes: 5 additions & 2 deletions app/jobs/sync_vaccination_record_to_nhs_job.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# frozen_string_literal: true

class SyncVaccinationRecordToNHSJob < ApplicationJob
queue_as :immunisation_api
def self.concurrent_jobs_per_second = 2
def self.concurrency_key = :immunisations_api

include NHSAPIConcurrencyConcern

retry_on Faraday::ServerError, wait: :polynomially_longer
queue_as :immunisation_api

def perform(vaccination_record)
tx_id = SecureRandom.urlsafe_base64(16)
Expand Down