Non blocking event dispatch (#652)
- Performance improvements for event dispatch
This commit is contained in:
parent
bab9d663d2
commit
e56132c506
6 changed files with 61 additions and 2 deletions
|
@ -1,7 +1,10 @@
|
|||
class AsyncDispatcher < BaseDispatcher
|
||||
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)
|
||||
# TODO: Move this to worker
|
||||
publish(event_object.method_name, event_object)
|
||||
end
|
||||
|
||||
|
|
7
app/jobs/event_dispatcher_job.rb
Normal file
7
app/jobs/event_dispatcher_job.rb
Normal 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
|
|
@ -17,6 +17,7 @@
|
|||
- [mailers, 2]
|
||||
- [webhooks, 1]
|
||||
- [bots, 1]
|
||||
- [events, 3]
|
||||
|
||||
# you can override concurrency based on environment
|
||||
production:
|
||||
|
|
|
@ -6,6 +6,16 @@ describe ::V2::ReportBuilder do
|
|||
let!(:inbox) { create(:inbox, account: account) }
|
||||
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
|
||||
context 'when report type is account' do
|
||||
before do
|
||||
|
|
16
spec/dispatchers/async_dispatcher_spec.rb
Normal file
16
spec/dispatchers/async_dispatcher_spec.rb
Normal 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
|
22
spec/jobs/event_dispatcher_job_spec.rb
Normal file
22
spec/jobs/event_dispatcher_job_spec.rb
Normal 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
|
Loading…
Reference in a new issue