Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
# frozen_string_literal: true

class API::Testing::VaccinationsSearchInNHSController < API::Testing::BaseController
POLL_INTERVAL = 0.25
POLL_TIMEOUT = 300

def create
if params[:wait].present?
EnqueueVaccinationsSearchInNHSJob.perform_now
wait_for_search_jobs_to_complete
render status: :ok
else
EnqueueVaccinationsSearchInNHSJob.perform_later
render status: :accepted
end
EnqueueVaccinationsSearchInNHSJob.perform_now
render status: :accepted
end

private

# EnqueueVaccinationsSearchInNHSJob fans out to per-patient
# SearchVaccinationRecordsInNHSJob jobs via perform_bulk. Poll
# until Sidekiq workers have drained the queue so callers see
# updated patient statuses when the response arrives.
def wait_for_search_jobs_to_complete
def show
queue = Sidekiq::Queue.new("immunisations_api_search")
deadline = Time.current + POLL_TIMEOUT

# rubocop:disable Style/ZeroLengthPredicate -- Sidekiq::Queue has no #empty?
sleep POLL_INTERVAL until queue.size.zero? || Time.current > deadline
render status: queue.size.zero? ? :ok : :accepted
# rubocop:enable Style/ZeroLengthPredicate
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
get "refresh-reporting", to: "refresh_reporting#create"
post "vaccinations-search-in-nhs",
to: "vaccinations_search_in_nhs#create"
get "vaccinations-search-in-nhs", to: "vaccinations_search_in_nhs#show"
end
end

Expand Down
36 changes: 24 additions & 12 deletions spec/requests/api/testing/vaccinations_search_in_nhs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,40 @@
before { Flipper.enable(:testing_api) }

describe "POST" do
context "without wait param" do
it "enqueues the job and responds with accepted" do
expect {
post "/api/testing/vaccinations-search-in-nhs"
}.to have_enqueued_job(EnqueueVaccinationsSearchInNHSJob)
expect(response).to have_http_status(:accepted)
end
before { allow(EnqueueVaccinationsSearchInNHSJob).to receive(:perform_now) }

it "runs the enqueue job synchronously and responds with accepted" do
post "/api/testing/vaccinations-search-in-nhs"
expect(EnqueueVaccinationsSearchInNHSJob).to have_received(:perform_now)
expect(response).to have_http_status(:accepted)
end
end

context "with wait=true" do
describe "GET" do
context "when the search queue is empty" do
before do
allow(EnqueueVaccinationsSearchInNHSJob).to receive(:perform_now)
allow(Sidekiq::Queue).to receive(:new).with(
"immunisations_api_search"
).and_return(instance_double(Sidekiq::Queue, size: 0))
end

it "runs the job synchronously and responds with ok" do
post "/api/testing/vaccinations-search-in-nhs", params: { wait: "true" }
expect(EnqueueVaccinationsSearchInNHSJob).to have_received(:perform_now)
it "responds with ok" do
get "/api/testing/vaccinations-search-in-nhs"
expect(response).to have_http_status(:ok)
end
end

context "when the search queue has pending jobs" do
before do
allow(Sidekiq::Queue).to receive(:new).with(
"immunisations_api_search"
).and_return(instance_double(Sidekiq::Queue, size: 3))
end

it "responds with accepted" do
get "/api/testing/vaccinations-search-in-nhs"
expect(response).to have_http_status(:accepted)
end
end
end
end
Loading