Module: ActionSubscriber::MarchHare::Subscriber

Includes:
Logging
Included in:
RouteSet
Defined in:
lib/action_subscriber/march_hare/subscriber.rb

Instance Method Summary collapse

Methods included from Logging

initialize_logger, logger, #logger, logger=

Instance Method Details

#auto_pop!Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/action_subscriber/march_hare/subscriber.rb', line 16

def auto_pop!
  # Because threadpools can be large we want to cap the number
  # of times we will pop each time we poll the broker
  times_to_pop = [::ActionSubscriber::Threadpool.ready_size, ::ActionSubscriber.config.times_to_pop].min
  times_to_pop.times do
    subscriptions.each do |subscription|
      route = subscription[:route]
      queue = subscription[:queue]
      # Handle busy checks on a per threadpool basis
      next if route.threadpool.busy?

      , encoded_payload = queue.pop(route.queue_subscription_options)
      next unless encoded_payload
      ::ActiveSupport::Notifications.instrument "popped_event.action_subscriber", :payload_size => encoded_payload.bytesize, :queue => queue.name
      properties = {
        :action => route.action,
        :content_type => .content_type,
        :delivery_tag => .delivery_tag,
        :exchange => .exchange,
        :headers => _normalized_headers(),
        :message_id => .message_id,
        :routing_key => .routing_key,
        :queue => queue.name,
      }
      env = ::ActionSubscriber::Middleware::Env.new(route.subscriber, encoded_payload, properties)
      enqueue_env(route.threadpool, env)
    end
  end

rescue ::MarchHare::ChannelAlreadyClosed => e
  # The connection has gone down, we can just try again on the next pop
end

#auto_subscribe!Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/action_subscriber/march_hare/subscriber.rb', line 49

def auto_subscribe!
  subscriptions.each do |subscription|
    route = subscription[:route]
    queue = subscription[:queue]
    queue.channel.prefetch = route.prefetch if route.acknowledgements?
    consumer = queue.subscribe(route.queue_subscription_options) do |, encoded_payload|
      ::ActiveSupport::Notifications.instrument "received_event.action_subscriber", :payload_size => encoded_payload.bytesize, :queue => queue.name
      properties = {
        :action => route.action,
        :channel => queue.channel,
        :content_type => .content_type,
        :delivery_tag => .delivery_tag,
        :exchange => .exchange,
        :headers => _normalized_headers(),
        :message_id => .message_id,
        :routing_key => .routing_key,
        :queue => queue.name,
      }
      env = ::ActionSubscriber::Middleware::Env.new(route.subscriber, encoded_payload, properties)
      run_env(env)
    end

    march_hare_consumers << consumer
  end
end

#cancel_consumers!Object



6
7
8
# File 'lib/action_subscriber/march_hare/subscriber.rb', line 6

def cancel_consumers!
  march_hare_consumers.each(&:cancel)
end

#create_queue(channel, queue_name, queue_options) ⇒ Object



10
11
12
13
14
# File 'lib/action_subscriber/march_hare/subscriber.rb', line 10

def create_queue(channel, queue_name, queue_options)
  queue = ::MarchHare::Queue.new(channel, queue_name, queue_options)
  queue.declare!
  queue
end

#march_hare_consumersObject



75
76
77
# File 'lib/action_subscriber/march_hare/subscriber.rb', line 75

def march_hare_consumers
  @march_hare_consumers ||= []
end