From d58f3d1dcbe04ea70040eb4489d3b9b2959be777 Mon Sep 17 00:00:00 2001 From: Duncan Brown Date: Thu, 21 Apr 2022 14:09:07 +0100 Subject: [PATCH] Allow queue to be configured --- config/locales/en.yml | 3 +++ lib/dfe/analytics.rb | 2 ++ lib/dfe/analytics/send_events.rb | 2 ++ lib/dfe/analytics/testing/helpers.rb | 11 +++++++++++ spec/requests/analytics_spec.rb | 18 ++++++++++++++++++ 5 files changed, 36 insertions(+) diff --git a/config/locales/en.yml b/config/locales/en.yml index 4977b12d..b1145b81 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -10,6 +10,9 @@ en: async: description: Whether to use ActiveJob or dispatch events immediately. default: true + queue: + description: Which ActiveJob queue to put events on + default: ":default" bigquery_table_name: description: The name of the BigQuery table we’re writing to. default: ENV['BIGQUERY_TABLE_NAME'] diff --git a/lib/dfe/analytics.rb b/lib/dfe/analytics.rb index 2b74dd40..518105cb 100644 --- a/lib/dfe/analytics.rb +++ b/lib/dfe/analytics.rb @@ -41,6 +41,7 @@ def self.config configurables = %i[ log_only async + queue bigquery_table_name bigquery_project_id bigquery_dataset @@ -67,6 +68,7 @@ def self.configure config.environment ||= ENV.fetch('RAILS_ENV', 'development') config.log_only ||= false config.async ||= true + config.queue ||= :default end def self.enabled? diff --git a/lib/dfe/analytics/send_events.rb b/lib/dfe/analytics/send_events.rb index e7a9b3ee..fc103fd6 100644 --- a/lib/dfe/analytics/send_events.rb +++ b/lib/dfe/analytics/send_events.rb @@ -3,6 +3,8 @@ module DfE module Analytics class SendEvents < ActiveJob::Base + queue_as { DfE::Analytics.config.queue } + def self.do(events) if DfE::Analytics.async? perform_later(events) diff --git a/lib/dfe/analytics/testing/helpers.rb b/lib/dfe/analytics/testing/helpers.rb index ad074d52..0381916f 100644 --- a/lib/dfe/analytics/testing/helpers.rb +++ b/lib/dfe/analytics/testing/helpers.rb @@ -36,6 +36,17 @@ def stub_analytics_event_submission stub_request(:post, /bigquery.googleapis.com/) .to_return(status: 200, body: '{}', headers: { 'Content-Type' => 'application/json' }) end + + def with_analytics_config(options) + old_config = DfE::Analytics.config.dup + DfE::Analytics.configure do |config| + options.each { |option, value| config[option] = value } + end + + yield + ensure + DfE::Analytics.instance_variable_set(:@config, old_config) + end end end end diff --git a/spec/requests/analytics_spec.rb b/spec/requests/analytics_spec.rb index 67425b16..20c57562 100644 --- a/spec/requests/analytics_spec.rb +++ b/spec/requests/analytics_spec.rb @@ -76,4 +76,22 @@ def index expect(payload['request_uuid']).to eq(request_uuid) end).to have_been_made end + + context "when a queue is specified" do + it 'uses the specified queue' do + with_analytics_config(queue: :my_custom_queue) do + expect { + get '/example/path' + }.to have_enqueued_job.on_queue(:my_custom_queue) + end + end + end + + context "when no queue is specified" do + it 'uses the default queue' do + expect { + get '/example/path' + }.to have_enqueued_job.on_queue(:default) + end + end end