mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-02 20:18:08 +00:00
chore: Move Twilio event processing to background job (#11094)
- Twilio events were being processed synchronously, leading to slow API responses. - This change moves Twilio event processing to a background job to improve performance and align with how other events (e.g., WhatsApp) are handled. --------- Co-authored-by: Pranav <pranav@chatwoot.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
class Twilio::CallbackController < ApplicationController
|
class Twilio::CallbackController < ApplicationController
|
||||||
def create
|
def create
|
||||||
::Twilio::IncomingMessageService.new(params: permitted_params).perform
|
Webhooks::TwilioEventsJob.perform_later(permitted_params.to_unsafe_hash)
|
||||||
|
|
||||||
head :no_content
|
head :no_content
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
class Twilio::DeliveryStatusController < ApplicationController
|
class Twilio::DeliveryStatusController < ApplicationController
|
||||||
def create
|
def create
|
||||||
::Twilio::DeliveryStatusService.new(params: permitted_params).perform
|
Webhooks::TwilioDeliveryStatusJob.perform_later(permitted_params.to_unsafe_hash)
|
||||||
|
|
||||||
head :no_content
|
head :no_content
|
||||||
end
|
end
|
||||||
|
|||||||
8
app/jobs/webhooks/twilio_delivery_status_job.rb
Normal file
8
app/jobs/webhooks/twilio_delivery_status_job.rb
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
class Webhooks::TwilioDeliveryStatusJob < ApplicationJob
|
||||||
|
queue_as :low
|
||||||
|
|
||||||
|
def perform(params = {})
|
||||||
|
# Process the Twilio delivery status webhook event in the background
|
||||||
|
::Twilio::DeliveryStatusService.new(params: params).perform
|
||||||
|
end
|
||||||
|
end
|
||||||
8
app/jobs/webhooks/twilio_events_job.rb
Normal file
8
app/jobs/webhooks/twilio_events_job.rb
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
class Webhooks::TwilioEventsJob < ApplicationJob
|
||||||
|
queue_as :low
|
||||||
|
|
||||||
|
def perform(params = {})
|
||||||
|
# Process the Twilio webhook event in the background
|
||||||
|
::Twilio::IncomingMessageService.new(params: params).perform
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -2,17 +2,27 @@ require 'rails_helper'
|
|||||||
|
|
||||||
RSpec.describe 'Twilio::CallbacksController', type: :request do
|
RSpec.describe 'Twilio::CallbacksController', type: :request do
|
||||||
include Rails.application.routes.url_helpers
|
include Rails.application.routes.url_helpers
|
||||||
let(:twilio_service) { instance_double(Twilio::IncomingMessageService) }
|
|
||||||
|
|
||||||
before do
|
describe 'POST /twilio/callback' do
|
||||||
allow(Twilio::IncomingMessageService).to receive(:new).and_return(twilio_service)
|
let(:params) do
|
||||||
allow(twilio_service).to receive(:perform)
|
{
|
||||||
end
|
'From' => '+1234567890',
|
||||||
|
'To' => '+0987654321',
|
||||||
|
'Body' => 'Test message',
|
||||||
|
'AccountSid' => 'AC123',
|
||||||
|
'SmsSid' => 'SM123'
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
describe 'GET /twilio/callback' do
|
it 'enqueues the Twilio events job' do
|
||||||
it 'calls incoming message service' do
|
expect do
|
||||||
post twilio_callback_index_url, params: {}
|
post twilio_callback_index_url, params: params
|
||||||
expect(twilio_service).to have_received(:perform)
|
end.to have_enqueued_job(Webhooks::TwilioEventsJob).with(params)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns no content status' do
|
||||||
|
post twilio_callback_index_url, params: params
|
||||||
|
expect(response).to have_http_status(:no_content)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,17 +2,25 @@ require 'rails_helper'
|
|||||||
|
|
||||||
RSpec.describe 'Twilio::DeliveryStatusController', type: :request do
|
RSpec.describe 'Twilio::DeliveryStatusController', type: :request do
|
||||||
include Rails.application.routes.url_helpers
|
include Rails.application.routes.url_helpers
|
||||||
let(:twilio_service) { instance_double(Twilio::DeliveryStatusService) }
|
|
||||||
|
|
||||||
before do
|
describe 'POST /twilio/delivery_status' do
|
||||||
allow(Twilio::DeliveryStatusService).to receive(:new).and_return(twilio_service)
|
let(:params) do
|
||||||
allow(twilio_service).to receive(:perform)
|
{
|
||||||
end
|
'MessageSid' => 'SM123',
|
||||||
|
'MessageStatus' => 'delivered',
|
||||||
|
'AccountSid' => 'AC123'
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
describe 'POST /twilio/delivery' do
|
it 'enqueues the Twilio delivery status job' do
|
||||||
it 'calls incoming message service' do
|
expect do
|
||||||
post twilio_delivery_status_index_url, params: {}
|
post twilio_delivery_status_index_url, params: params
|
||||||
expect(twilio_service).to have_received(:perform)
|
end.to have_enqueued_job(Webhooks::TwilioDeliveryStatusJob).with(params)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns no content status' do
|
||||||
|
post twilio_delivery_status_index_url, params: params
|
||||||
|
expect(response).to have_http_status(:no_content)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
26
spec/jobs/webhooks/twilio_delivery_status_job_spec.rb
Normal file
26
spec/jobs/webhooks/twilio_delivery_status_job_spec.rb
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe Webhooks::TwilioDeliveryStatusJob do
|
||||||
|
subject(:job) { described_class.perform_later(params) }
|
||||||
|
|
||||||
|
let(:params) do
|
||||||
|
{
|
||||||
|
'MessageSid' => 'SM123',
|
||||||
|
'MessageStatus' => 'delivered',
|
||||||
|
'AccountSid' => 'AC123'
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'queues the job' do
|
||||||
|
expect { job }.to have_enqueued_job(described_class)
|
||||||
|
.with(params)
|
||||||
|
.on_queue('low')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'calls the Twilio::DeliveryStatusService' do
|
||||||
|
service = double
|
||||||
|
expect(Twilio::DeliveryStatusService).to receive(:new).with(params: params).and_return(service)
|
||||||
|
expect(service).to receive(:perform)
|
||||||
|
described_class.new.perform(params)
|
||||||
|
end
|
||||||
|
end
|
||||||
28
spec/jobs/webhooks/twilio_events_job_spec.rb
Normal file
28
spec/jobs/webhooks/twilio_events_job_spec.rb
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe Webhooks::TwilioEventsJob do
|
||||||
|
subject(:job) { described_class.perform_later(params) }
|
||||||
|
|
||||||
|
let(:params) do
|
||||||
|
{
|
||||||
|
'From' => '+1234567890',
|
||||||
|
'To' => '+0987654321',
|
||||||
|
'Body' => 'Test message',
|
||||||
|
'AccountSid' => 'AC123',
|
||||||
|
'SmsSid' => 'SM123'
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'queues the job' do
|
||||||
|
expect { job }.to have_enqueued_job(described_class)
|
||||||
|
.with(params)
|
||||||
|
.on_queue('low')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'calls the Twilio::IncomingMessageService' do
|
||||||
|
service = double
|
||||||
|
expect(Twilio::IncomingMessageService).to receive(:new).with(params: params).and_return(service)
|
||||||
|
expect(service).to receive(:perform)
|
||||||
|
described_class.new.perform(params)
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user