From 9e62a2c059af7859720b341c4c68a7fb965fa73c Mon Sep 17 00:00:00 2001 From: Brad Watson Date: Fri, 3 Jan 2025 11:35:22 -0600 Subject: [PATCH] Changes the return value of queue_latency to JSON. (#697) * Changes the return value of queue_latency to JSON. * Appeases the rubocop. --------- Co-authored-by: Alex Zotov --- config/routes.rb | 12 ++++++++---- spec/features/viewing_the_queue_latency_page_spec.rb | 10 ++++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index b0e7a52..355c72b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -21,7 +21,7 @@ mount Sidekiq::Web => '/sidekiq' match "queue-latency" => proc { - [200, { "Content-Type" => "text/plain" }, [latency_text]] + [200, { "Content-Type" => "application/json" }, [latency_text]] }, via: :get mount Qa::Engine => '/authorities' mount Hyrax::Engine, at: '/' @@ -44,9 +44,13 @@ end # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html + # rubocop:disable Rails/FindEach def latency_text - Sidekiq::Queue.all.map do |q| - "#{q.name} queue latency in seconds: #{q.latency}" - end.join(', ') + ret_hsh = { queues: [] } + Sidekiq::Queue.all.each do |q| + ret_hsh[:queues] << Hash[q.name, q.latency] + end + ret_hsh.to_json end + # rubocop:enable Rails/FindEach end diff --git a/spec/features/viewing_the_queue_latency_page_spec.rb b/spec/features/viewing_the_queue_latency_page_spec.rb index c295289..87f3235 100644 --- a/spec/features/viewing_the_queue_latency_page_spec.rb +++ b/spec/features/viewing_the_queue_latency_page_spec.rb @@ -5,9 +5,15 @@ before { visit '/queue-latency' } it('successfully loads') { expect(page).to have_http_status(:success) } - it('returns plain text') { expect(page.response_headers['Content-Type']).to eq('text/plain') } + it('returns plain text') { expect(page.response_headers['Content-Type']).to eq('application/json') } # It seems that Circle CI doesn't inherently spin up a queue. - it('contains the expected verbiage') { expect(page.html).to include('queue latency in seconds:') } unless ENV['CI'] + unless ENV['CI'] + it('contains the expected JSON') do + parsed_json = JSON.parse(page.body) + expect(parsed_json).to be_present + expect(parsed_json['queues']).to be_present + end + end it 'calls the Sidekiq queue API' do allow(::Sidekiq::Queue).to receive(:all).and_call_original