File tree Expand file tree Collapse file tree
app/controllers/api/testing
spec/requests/api/testing Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11# frozen_string_literal: true
22
33class API ::Testing ::VaccinationsSearchInNHSController < API ::Testing ::BaseController
4+ POLL_INTERVAL = 0.25
5+ POLL_TIMEOUT = 300
6+
47 def create
58 if params [ :wait ] . present?
69 EnqueueVaccinationsSearchInNHSJob . perform_now
10+ wait_for_search_jobs_to_complete
711 render status : :ok
812 else
913 EnqueueVaccinationsSearchInNHSJob . perform_later
1014 render status : :accepted
1115 end
1216 end
17+
18+ private
19+
20+ # EnqueueVaccinationsSearchInNHSJob fans out to per-patient
21+ # SearchVaccinationRecordsInNHSJob jobs via perform_bulk. Poll
22+ # until Sidekiq workers have drained the queue so callers see
23+ # updated patient statuses when the response arrives.
24+ def wait_for_search_jobs_to_complete
25+ queue = Sidekiq ::Queue . new ( "immunisations_api_search" )
26+ deadline = Time . current + POLL_TIMEOUT
27+
28+ # rubocop:disable Style/ZeroLengthPredicate -- Sidekiq::Queue has no #empty?
29+ sleep POLL_INTERVAL until queue . size . zero? || Time . current > deadline
30+ # rubocop:enable Style/ZeroLengthPredicate
31+ end
1332end
Original file line number Diff line number Diff line change 1717 context "with wait=true" do
1818 before do
1919 allow ( EnqueueVaccinationsSearchInNHSJob ) . to receive ( :perform_now )
20+ allow ( Sidekiq ::Queue ) . to receive ( :new ) . with (
21+ "immunisations_api_search"
22+ ) . and_return ( instance_double ( Sidekiq ::Queue , size : 0 ) )
2023 end
2124
2225 it "runs the job synchronously and responds with ok" do
You can’t perform that action at this time.
0 commit comments