Sha256: 81145d084ed71f051f95bb13b16fa24a16ee2e7698b69f8b2bf659d4a1213cbd

Contents?: true

Size: 1.33 KB

Versions: 15

Compression:

Stored size: 1.33 KB

Contents

#
# This example assumes that you want to save all events in your database for
# recovery purposes. The consumer will process the message and perform other
# operations, this implementation assumes a generic way to save the events.
#
# Setup your database connection using `phobos_boot.rb`. Remember to Setup
# a hook to disconnect, e.g: `at_exit { Database.disconnect! }`
#
class HandlerSavingEventsDatabase
  include Phobos::Handler
  include Phobos::Producer

  def self.around_consume(payload, metadata)
    #
    # Let's assume `::from_message` will initialize our object with `payload`
    #
    event = Model::Event.from_message(payload)

    #
    # If event already exists in the database, skip this message
    #
    return if event.exists?

    Model::Event.transaction do
      #
      # Executes `#consume` method
      #
      new_values = yield

      #
      # `#consume` method can return additional data (up to your code)
      #
      event.update_with_new_attributes(new_values)

      #
      # Let's assume the event is just initialized and now is the time to save it
      #
      event.save!
    end
  end

  def consume(payload, metadata)
    #
    # Process the event, it might index it to elasticsearch or notify other
    # system, you should process your message inside this method.
    #
    { new_vale: payload.length % 3 }
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
phobos-1.8.0 examples/handler_saving_events_database.rb
phobos-1.7.2 examples/handler_saving_events_database.rb
phobos-1.7.1 examples/handler_saving_events_database.rb
phobos-1.7.0 examples/handler_saving_events_database.rb
phobos-1.6.1 examples/handler_saving_events_database.rb
phobos-1.6.0 examples/handler_saving_events_database.rb
phobos-1.5.0 examples/handler_saving_events_database.rb
phobos-1.4.2 examples/handler_saving_events_database.rb
phobos-1.4.1 examples/handler_saving_events_database.rb
phobos-1.4.0 examples/handler_saving_events_database.rb
phobos-1.3.0 examples/handler_saving_events_database.rb
phobos-1.2.1 examples/handler_saving_events_database.rb
phobos-1.2.0 examples/handler_saving_events_database.rb
phobos-1.1.0 examples/handler_saving_events_database.rb
phobos-1.0.0 examples/handler_saving_events_database.rb