Skip to content

Commit

Permalink
Ensure Sidekiq::Worker works with AsyncHandler helper.
Browse files Browse the repository at this point in the history
When serializing payload for background job system with JSON,
information about symbols as keys is lost.

ActiveJob persists the information about symbols in payload. This
information is used to convert those keys back to symbols when the
payload is passed back to worker.

When using plain Sidekiq::Worker without ActiveJob wrapper, this
mechanism is no longer in place. The polyfill for such behaviour is
rather simple in case of SerializedRecord thus we provide it for
convenience in RailsEventStore::AsyncHandler.

When you don't use RailsEventStore::AsyncHandler, the responsibility of
adjusting received payload for event_store.deserialize() is on you.

Closes #507.
  • Loading branch information
mostlyobvious committed Jan 18, 2019
1 parent c6f0114 commit 7d249f5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module RailsEventStore
module AsyncHandler
def perform(payload)
super(Rails.configuration.event_store.deserialize(payload))
super(Rails.configuration.event_store.deserialize(payload.symbolize_keys))
end
end

Expand Down
36 changes: 36 additions & 0 deletions rails_event_store/spec/rails_event_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ def perform(payload)
end
end

class SidekiqHandlerWithHelper
include Sidekiq::Worker

cattr_accessor :event

prepend RailsEventStore::AsyncHandler

def perform(event)
self.class.event = event
end
end

class HandlerWithHelper < ActiveJob::Base
cattr_accessor :event

Expand All @@ -38,6 +50,16 @@ def perform(event)
end
end

class CustomSidekiqScheduler
def call(klass, serialized_event)
klass.perform_async(serialized_event.to_h)
end

def verify(subscriber)
Class === subscriber && subscriber.respond_to?(:perform_async)
end
end

let(:event_store) { RailsEventStore::Client.new }

before do
Expand Down Expand Up @@ -102,6 +124,20 @@ def perform(event)
expect(MyLovelyAsyncHandler.event).to eq(ev)
end

specify 'Sidekiq::Worker without ActiveJob that requires serialization' do
event_store = RailsEventStore::Client.new(
dispatcher: RubyEventStore::ImmediateAsyncDispatcher.new(scheduler: CustomSidekiqScheduler.new)
)
ev = RailsEventStore::Event.new
Sidekiq::Testing.fake! do
SidekiqHandlerWithHelper.event = nil
event_store.subscribe_to_all_events(SidekiqHandlerWithHelper)
event_store.publish(ev)
Thread.new{ Sidekiq::Worker.drain_all }.join
end
expect(SidekiqHandlerWithHelper.event).to eq(ev)
end

private

def wait_until(&block)
Expand Down

0 comments on commit 7d249f5

Please sign in to comment.