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:
Sojan Jose
2025-03-15 13:51:08 -07:00
committed by GitHub
parent 46ec92c86e
commit 586dc800bb
8 changed files with 108 additions and 20 deletions

View File

@@ -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

View File

@@ -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

View 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

View 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

View File

@@ -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

View File

@@ -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

View 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

View 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