Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem when passing SerializedRecord over some background job systems #507

Closed
mostlyobvious opened this issue Nov 29, 2018 · 0 comments
Closed
Labels

Comments

@mostlyobvious
Copy link
Member

Given following code: https://gist.github.com/pawelpacana/57ddc0699b775059347301b32119bac1 we encounter problem on event_store.deserialize because payload cannot be unpacked to keyword arguments.

  1) backround job serialization JSON as in Sidekiq/Resque
     Failure/Error:
       client.deserialize(
         transport_as_json(
           serialized_record(event).to_h
         )
       )

     ArgumentError:
       wrong number of arguments (given 1, expected 0; required keywords: event_type, event_id, data, metadata)

That happens because:

irb(main):011:0> RubyEventStore::SerializedRecord.new(event_id: SecureRandom.uuid, data: '', metadata: '', event_type: 'DummyEvent').to_h
=> {:event_id=>"5c0f5eef-edb4-4899-9918-556e365c5acb", :data=>"", :metadata=>"", :event_type=>"DummyEvent"}
irb(main):012:0> JSON.load(JSON.dump(_))
=> {"event_id"=>"5c0f5eef-edb4-4899-9918-556e365c5acb", "data"=>"", "metadata"=>"", "event_type"=>"DummyEvent"}

and that is not compatible with method signature.

For reference:

Existing specs in RES skip that transport serialization (not sure about the one that uses Sidekiq::Testing.fake though):

  • specify 'can load serialized event when using Default mapper' do
    client = RubyEventStore::Client.new(
    mapper: RubyEventStore::Mappers::Default.new,
    repository: InMemoryRepository.new
    )
    event = OrderCreated.new(
    event_id: 'f90b8848-e478-47fe-9b4a-9f2a1d53622b',
    data: { foo: 'bar' },
    metadata: { bar: 'baz' }
    )
    serialized_event = {
    event_type: "OrderCreated",
    event_id: "f90b8848-e478-47fe-9b4a-9f2a1d53622b",
    data: "---\n:foo: bar\n",
    metadata: "---\n:bar: baz\n"
    }
    expect(client.deserialize(serialized_event)).to eq(event)
    end
    specify 'can load serialized event using Protobuf mapper' do
    begin
    require_relative 'mappers/events_pb.rb'
    client = RubyEventStore::Client.new(
    mapper: RubyEventStore::Mappers::Protobuf.new,
    repository: InMemoryRepository.new
    )
    event = RubyEventStore::Proto.new(
    event_id: "f90b8848-e478-47fe-9b4a-9f2a1d53622b",
    data: ResTesting::OrderCreated.new(
    customer_id: 123,
    order_id: "K3THNX9",
    ),
    metadata: {
    time: Time.new(2018, 12, 13, 11),
    }
    )
    serialized_event = {
    event_type: "res_testing.OrderCreated",
    event_id: "f90b8848-e478-47fe-9b4a-9f2a1d53622b",
    data: "\n\aK3THNX9\x10{",
    metadata: "\n\x10\n\x04time\x12\b:\x06\b\xA0\xDB\xC8\xE0\x05"
    }
    expect(client.deserialize(serialized_event)).to eq(event)
    rescue LoadError => exc
    skip if exc.message == "cannot load such file -- google/protobuf_c"
    end
    end
  • class MyLovelyAsyncHandler < ActiveJob::Base
    cattr_accessor :event
    def perform(payload)
    self.class.event = Rails.configuration.event_store.deserialize(payload)
    end
    end
  • class AsyncProtoHandler < ActiveJob::Base
    self.queue_adapter = :inline
    cattr_accessor :event_store
    def perform(payload)
    @@event = self.class.event_store.deserialize(payload)
    end
    def self.event
    @@event
    end
    end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant