Skip to content

Commit

Permalink
Non blocking event dispatch (#652)
Browse files Browse the repository at this point in the history
- Performance improvements for event dispatch
  • Loading branch information
subintp committed Mar 29, 2020
1 parent bab9d66 commit e56132c
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 2 deletions.
5 changes: 4 additions & 1 deletion app/dispatchers/async_dispatcher.rb
@@ -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

Expand Down
7 changes: 7 additions & 0 deletions app/jobs/event_dispatcher_job.rb
@@ -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
3 changes: 2 additions & 1 deletion config/sidekiq.yml
Expand Up @@ -17,7 +17,8 @@
- [mailers, 2]
- [webhooks, 1]
- [bots, 1]

- [events, 3]

# you can override concurrency based on environment
production:
:concurrency: 10
Expand Down
10 changes: 10 additions & 0 deletions spec/builders/v2/report_builder_spec.rb
Expand Up @@ -6,6 +6,16 @@
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
Expand Down
16 changes: 16 additions & 0 deletions spec/dispatchers/async_dispatcher_spec.rb
@@ -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 changes: 22 additions & 0 deletions spec/jobs/event_dispatcher_job_spec.rb
@@ -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

0 comments on commit e56132c

Please sign in to comment.