From 935b02954d961e6e3aaff488355b45f5d80ce17e Mon Sep 17 00:00:00 2001 From: Ahmad Farhat Date: Wed, 4 Dec 2024 11:29:52 -0500 Subject: [PATCH 1/3] Add a retry mechanism for recording ready notifier --- .../recording_ready_notifier_service.rb | 50 +++++++++++-------- .../recording_ready_notifier_service_spec.rb | 23 +++++++++ 2 files changed, 53 insertions(+), 20 deletions(-) diff --git a/app/services/recording_ready_notifier_service.rb b/app/services/recording_ready_notifier_service.rb index 8a97a2d2..75695359 100644 --- a/app/services/recording_ready_notifier_service.rb +++ b/app/services/recording_ready_notifier_service.rb @@ -21,8 +21,7 @@ def execute(recording_id) notify(callback_url, meeting_id, recording.record_id, tenant_name) if callback_url end - def encoded_payload(meeting_id, record_id, tenant_name) - secret = fetch_secrets(tenant_name: tenant_name)[0] + def encoded_payload(meeting_id, record_id, secret) payload = { meeting_id: meeting_id, record_id: record_id } JWT.encode(payload, secret) end @@ -31,27 +30,38 @@ def notify(callback_url, meeting_id, record_id, tenant_name) logger.info("Recording Ready Notify for [#{meeting_id}] starts") logger.info('Making callback for recording ready notification') - payload = encoded_payload(meeting_id, record_id, tenant_name) - uri = URI.parse(callback_url) - http = Net::HTTP.new(uri.host, uri.port) - http.use_ssl = (uri.scheme == 'https') - logger.info("Sending request to #{uri.scheme}://#{uri.host}#{uri.request_uri}") - request = Net::HTTP::Post.new(uri.request_uri) - request.set_form_data(signed_parameters: payload) + secrets = fetch_secrets(tenant_name: tenant_name) + success = false - response = http.request(request) - code = response.code.to_i + secrets.each do |secret| + payload = encoded_payload(meeting_id, record_id, secret) - if code == 410 - logger.info("Notified for deleted meeting: #{meeting_id}") - elsif code == 404 - logger.info("404 error when notifying for recording: #{meeting_id}, ignoring") - elsif code < 200 || code >= 300 - logger.info("Callback HTTP request failed: #{response.code} #{response.message} (code #{code})") - else - logger.info("Recording notifier successful: #{meeting_id} (code #{code})") + uri = URI.parse(callback_url) + http = Net::HTTP.new(uri.host, uri.port) + http.use_ssl = (uri.scheme == 'https') + logger.info("Sending request to #{uri.scheme}://#{uri.host}#{uri.request_uri}") + request = Net::HTTP::Post.new(uri.request_uri) + request.set_form_data(signed_parameters: payload) + + response = http.request(request) + code = response.code.to_i + + if code == 410 + logger.info("Notified for deleted meeting: #{meeting_id}") + break + elsif code == 404 + logger.info("404 error when notifying for recording: #{meeting_id}, ignoring") + break + elsif code < 200 || code >= 300 + logger.info("Callback HTTP request failed: #{response.code} #{response.message} (code #{code})") + else + logger.info("Recording notifier successful: #{meeting_id} (code #{code})") + success = true + break + end end - true + + success rescue StandardError => e logger.info('Rescued') logger.info(e.to_s) diff --git a/spec/services/recording_ready_notifier_service_spec.rb b/spec/services/recording_ready_notifier_service_spec.rb index d0430eff..6cb4f1fd 100644 --- a/spec/services/recording_ready_notifier_service_spec.rb +++ b/spec/services/recording_ready_notifier_service_spec.rb @@ -26,6 +26,29 @@ expect(return_val).to be true end + it 'retries with different secrets if multiple secrets are set' do + stub_request(:post, url) + .to_return( + { status: 401, body: '', headers: {} }, # First secret fails + { status: 401, body: '', headers: {} }, # Second secret fails + { status: 200, body: '', headers: {} } # Third secret succeeds + ) + + allow_any_instance_of(ApiHelper).to receive(:fetch_secrets).and_return(["secret1", "secret2", "secret3"]) + + allow(JWT).to receive(:encode).and_return('eyJhbGciOiJIUzI1NiJ9.eyJtZWV0aW5nX2lkIjoibWVldGluZzE5In0') + + allow(Rails.logger).to receive(:info).and_call_original # Allow all other logger calls to pass through + + expect(Rails.logger).to receive(:info).with("Callback HTTP request failed: 401 (code 401)").twice + expect(Rails.logger).to receive(:info).with("Recording notifier successful: #{recording.meeting_id} (code #{200})").once + + return_val = described_class.execute(recording.id) + + expect(return_val).to be true + end + + it 'returns false if recording ready notification fails' do stub_request(:post, url).to_timeout From 22ab84538fa0ad2a5dee4163266b90555613c8f6 Mon Sep 17 00:00:00 2001 From: Ahmad Farhat Date: Wed, 4 Dec 2024 11:41:28 -0500 Subject: [PATCH 2/3] rubo --- spec/services/recording_ready_notifier_service_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/services/recording_ready_notifier_service_spec.rb b/spec/services/recording_ready_notifier_service_spec.rb index 6cb4f1fd..2fda9046 100644 --- a/spec/services/recording_ready_notifier_service_spec.rb +++ b/spec/services/recording_ready_notifier_service_spec.rb @@ -34,7 +34,7 @@ { status: 200, body: '', headers: {} } # Third secret succeeds ) - allow_any_instance_of(ApiHelper).to receive(:fetch_secrets).and_return(["secret1", "secret2", "secret3"]) + allow_any_instance_of(ApiHelper).to receive(:fetch_secrets).and_return(%w[secret1 secret2 secret3]) allow(JWT).to receive(:encode).and_return('eyJhbGciOiJIUzI1NiJ9.eyJtZWV0aW5nX2lkIjoibWVldGluZzE5In0') From 27e5e4782df753666bdd489497d59f6530d499ed Mon Sep 17 00:00:00 2001 From: Ahmad Farhat Date: Wed, 4 Dec 2024 15:04:29 -0500 Subject: [PATCH 3/3] Rubo again --- spec/services/recording_ready_notifier_service_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/services/recording_ready_notifier_service_spec.rb b/spec/services/recording_ready_notifier_service_spec.rb index 2fda9046..7abb106c 100644 --- a/spec/services/recording_ready_notifier_service_spec.rb +++ b/spec/services/recording_ready_notifier_service_spec.rb @@ -48,7 +48,6 @@ expect(return_val).to be true end - it 'returns false if recording ready notification fails' do stub_request(:post, url).to_timeout