Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a retry mechanism for recording ready notifier #1129

Merged
Merged
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
50 changes: 30 additions & 20 deletions app/services/recording_ready_notifier_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions spec/services/recording_ready_notifier_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@
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(%w[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

Expand Down