Method: Messaging::Routing#on

Defined in:
lib/messaging/routing.rb

#on(pattern = /.*/, call: nil, enqueue: nil, &block) ⇒ Object

Public: Sets up routes for the events that matches the given pattern

pattern - Which messages to route. Can be a string, a regexp,

a Message class, a module or anything that responds to call.

call: - Any object that responds to call.

Will be called immediately for matching messages.

enqueue: - A constant that responds to call.

Will be enqueued with Sidekiq for matching messages.
Needs to be a constant that Sidekiq can serialize to a string
and back again to a constant as you can't store procs in Redis.

block - An optional block that will be called with each matching message.

Examples

Messaging.routes.draw do

on 'Events::BidPlaced', call: NotifyOtherBidders

on Events::BidPlaced, enqueue: NotifyOtherBidders

on Events, do |event|
  puts event.inspect
end

on /.*Updated$/, enqueue: AuditChanges

on ->(m) { m.topic == 'my-topic' }, call: DoSometing, enqueue: DoSomethingElseWithSidekiq

end



40
41
42
43
44
# File 'lib/messaging/routing.rb', line 40

def on(pattern = /.*/, call: nil, enqueue: nil, &block)
  routes << Route.new(pattern, call) if call
  routes << Route.new(pattern, block) if block_given?
  routes << EnqueuedRoute.new(pattern, enqueue) if enqueue
end