Non blocking event dispatch (#652)

- Performance improvements for event dispatch
This commit is contained in:
Subin T P 2020-03-29 19:18:30 +05:30 committed by GitHub
parent bab9d663d2
commit e56132c506
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 2 deletions

View file

@ -1,7 +1,10 @@
class AsyncDispatcher < BaseDispatcher class AsyncDispatcher < BaseDispatcher
def dispatch(event_name, timestamp, data) def dispatch(event_name, timestamp, data)
EventDispatcherJob.perform_later(event_name, timestamp, data)
end
def publish_event(event_name, timestamp, data)
event_object = Events::Base.new(event_name, timestamp, data) event_object = Events::Base.new(event_name, timestamp, data)
# TODO: Move this to worker
publish(event_object.method_name, event_object) publish(event_object.method_name, event_object)
end end

View file

@ -0,0 +1,7 @@
class EventDispatcherJob < ApplicationJob
queue_as :events
def perform(event_name, timestamp, data)
Rails.configuration.dispatcher.async_dispatcher.publish_event(event_name, timestamp, data)
end
end

View file

@ -17,6 +17,7 @@
- [mailers, 2] - [mailers, 2]
- [webhooks, 1] - [webhooks, 1]
- [bots, 1] - [bots, 1]
- [events, 3]
# you can override concurrency based on environment # you can override concurrency based on environment
production: production:

View file

@ -6,6 +6,16 @@ describe ::V2::ReportBuilder do
let!(:inbox) { create(:inbox, account: account) } let!(:inbox) { create(:inbox, account: account) }
let(:inbox_member) { create(:inbox_member, user: user, inbox: inbox) } let(:inbox_member) { create(:inbox_member, user: user, inbox: inbox) }
# Running jobs inline to calculate the exact metrics
around do |test|
current_adapter = ActiveJob::Base.queue_adapter
ActiveJob::Base.queue_adapter = :inline
test.run
ensure
ActiveJob::Base.queue_adapter = current_adapter
end
describe '#timeseries' do describe '#timeseries' do
context 'when report type is account' do context 'when report type is account' do
before do before do

View file

@ -0,0 +1,16 @@
require 'rails_helper'
describe AsyncDispatcher do
subject(:dispatcher) { described_class.new }
let!(:conversation) { create(:conversation) }
let(:event_name) { 'conversation.created' }
let(:timestamp) { Time.zone.now }
let(:event_data) { { conversation: conversation } }
describe '#dispatch' do
it 'enqueue job to dispatch event' do
expect(EventDispatcherJob).to receive(:perform_later).with(event_name, timestamp, event_data).once
dispatcher.dispatch(event_name, timestamp, event_data)
end
end
end

View file

@ -0,0 +1,22 @@
require 'rails_helper'
RSpec.describe EventDispatcherJob, type: :job do
subject(:job) { described_class.perform_later(event_name, timestamp, event_data) }
let!(:conversation) { create(:conversation) }
let(:event_name) { 'conversation.created' }
let(:timestamp) { Time.zone.now }
let(:event_data) { { conversation: conversation } }
it 'queues the job' do
expect { job }.to have_enqueued_job(described_class)
.with(event_name, timestamp, event_data)
.on_queue('events')
end
it 'publishes event' do
expect(Rails.configuration.dispatcher.async_dispatcher).to receive(:publish_event).with(event_name, timestamp, event_data).once
event_dispatcher = described_class.new
event_dispatcher.perform(event_name, timestamp, event_data)
end
end