chore: Add timeouts to requests (#2024)

This commit is contained in:
Sojan Jose 2021-03-31 16:39:57 +05:30 committed by GitHub
parent f5a961b27c
commit c8b81b066b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 10 deletions

View file

@ -29,13 +29,13 @@ class AgentNotifications::ConversationNotificationsMailer < ApplicationMailer
send_mail_with_liquid(to: @agent.email, subject: subject) and return send_mail_with_liquid(to: @agent.email, subject: subject) and return
end end
def assigned_conversation_new_message(conversation, agent) def assigned_conversation_new_message(message, agent)
return unless smtp_config_set_or_development? return unless smtp_config_set_or_development?
# Don't spam with email notifications if agent is online # Don't spam with email notifications if agent is online
return if ::OnlineStatusTracker.get_presence(conversation.account.id, 'User', agent.id) return if ::OnlineStatusTracker.get_presence(message.account_id, 'User', agent.id)
@agent = agent @agent = agent
@conversation = conversation @conversation = message.conversation
subject = "#{@agent.available_name}, New message in your assigned conversation [ID - #{@conversation.display_id}]." subject = "#{@agent.available_name}, New message in your assigned conversation [ID - #{@conversation.display_id}]."
@action_url = app_account_conversation_url(account_id: @conversation.account_id, id: @conversation.display_id) @action_url = app_account_conversation_url(account_id: @conversation.account_id, id: @conversation.display_id)
send_mail_with_liquid(to: @agent.email, subject: subject) and return send_mail_with_liquid(to: @agent.email, subject: subject) and return

View file

@ -16,7 +16,8 @@ class LocalResource
end end
def io def io
@io ||= uri.open # TODO: should we use RestClient here too ?
@io ||= uri.open(read_timeout: 5)
end end
def encoding def encoding

View file

@ -1,6 +1,11 @@
class Webhooks::Trigger class Webhooks::Trigger
def self.execute(url, payload) def self.execute(url, payload)
RestClient.post(url, payload.to_json, { content_type: :json, accept: :json }) RestClient::Request.execute(
method: :post,
url: url, payload: payload.to_json,
headers: { content_type: :json, accept: :json },
timeout: 5
)
rescue *ExceptionList::REST_CLIENT_EXCEPTIONS, *ExceptionList::URI_EXCEPTIONS => e rescue *ExceptionList::REST_CLIENT_EXCEPTIONS, *ExceptionList::URI_EXCEPTIONS => e
Rails.logger.info "Exception: invalid webhook url #{url} : #{e.message}" Rails.logger.info "Exception: invalid webhook url #{url} : #{e.message}"
rescue StandardError => e rescue StandardError => e

View file

@ -5,11 +5,18 @@ describe Webhooks::Trigger do
describe '#execute' do describe '#execute' do
it 'triggers webhook' do it 'triggers webhook' do
params = { hello: :hello } payload = { hello: :hello }
url = 'https://test.com' url = 'https://test.com'
expect(RestClient).to receive(:post).with(url, params.to_json, { accept: :json, content_type: :json }).once expect(RestClient::Request).to receive(:execute)
trigger.execute(url, params) .with(
method: :post,
url: url,
payload: payload.to_json,
headers: { content_type: :json, accept: :json },
timeout: 5
).once
trigger.execute(url, payload)
end end
end end
end end

View file

@ -52,10 +52,11 @@ RSpec.describe AgentNotifications::ConversationNotificationsMailer, type: :maile
end end
describe 'assigned_conversation_new_message' do describe 'assigned_conversation_new_message' do
let(:mail) { described_class.assigned_conversation_new_message(conversation, agent).deliver_now } let(:message) { create(:message, conversation: conversation, account: account) }
let(:mail) { described_class.assigned_conversation_new_message(message, agent).deliver_now }
it 'renders the subject' do it 'renders the subject' do
expect(mail.subject).to eq("#{agent.available_name}, New message in your assigned conversation [ID - #{conversation.display_id}].") expect(mail.subject).to eq("#{agent.available_name}, New message in your assigned conversation [ID - #{message.conversation.display_id}].")
end end
it 'renders the receiver email' do it 'renders the receiver email' do