Class: Chasqui::SubscriptionBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/chasqui/subscription_builder.rb

Overview

Provides the context used in subscribe to bind workers to channels via the #on method.

Direct Known Subclasses

SidekiqSubscriptionBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_options ⇒ Hash (readonly)

Default options for calls to #on.

Returns:

  • (Hash)


16
17
18
# File 'lib/chasqui/subscription_builder.rb', line 16

def default_options
  @default_options
end

#subscriptions ⇒ Chasqui::Subscriptions (readonly)

The collection of currently registered subscriptions.



12
13
14
# File 'lib/chasqui/subscription_builder.rb', line 12

def subscriptions
  @subscriptions
end

Instance Method Details

#on(channel, worker_or_proc, options = {}) ⇒ Object

Bind a worker to a channel.

The broker will place jobs on the worker's queue for each event published to the given channel.

Parameters:

  • channel (String) —

    the channel name

  • worker_or_proc (#perform, .perform, #call) —

    a Sidekiq Worker class, Resque worker class, or proc to handle events published to channel. If a proc is used as a worker, #on will define a new worker class that delegates #perform to proc#call.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :queue (String) —

    the worker queue. When given, this option will override the queue defined by the worker class. This option is recommended when using a proc as a worker.

  • :queue_prefix (String) —

    prefix for queue. When supplied, the value of this option is prepended to the queue name. Use this option to namespace your queues in order to prevent collisions with queues from other applicaitons sharing the same Redis database.



45
46
47
48
49
50
51
52
53
# File 'lib/chasqui/subscription_builder.rb', line 45

def on(channel, worker_or_proc, options={})
  options = default_options.merge(options)
  worker = build_worker(channel, worker_or_proc, options)

  queue = full_queue_name(worker, options)
  set_queue_name(worker, queue)
  
  subscriptions.register Chasqui::Subscriber.new(channel, queue, worker)
end