Module: QueuePatch

Included in:
Sneakers::Queue
Defined in:
lib/strum/patch/sneakers/queue_patch.rb

Instance Method Summary collapse

Instance Method Details

#actions_subscribes(actions, **custom_headers) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/strum/patch/sneakers/queue_patch.rb', line 48

def actions_subscribes(actions, **custom_headers)
  actions.each do |_action|
    exchange, action, resource = Strum::Esb::Functions.action_explain(_action)
    raise StandardError "action binding format must be `exchange:action/resource`" unless resource && action

    @opts[:header_bindings] << {
      exchange: exchange,
      arguments: {
        action: action,
        resource: resource
      }.merge(custom_headers)
    }
  end
end

#events_subscribes(events, **custom_headers) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/strum/patch/sneakers/queue_patch.rb', line 63

def events_subscribes(events, **custom_headers)
  events.each do |_event|
    exchange, resource, event, state = Strum::Esb::Functions.event_explain(_event)
    raise StandardError "binding format must be `exchange:resource/event{/state}`" unless resource && event

    @opts[:header_bindings] << {
      exchange: exchange,
      arguments: {
        resource: resource,
        event: event,
        state: state
      }.merge(custom_headers)
    }
  end
end

#header_subscribe(worker) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/strum/patch/sneakers/queue_patch.rb', line 108

def header_subscribe(worker)
  @bunny = @opts[:connection]
  @bunny ||= create_bunny_connection
  @bunny.start

  @channel = @bunny.create_channel
  @channel.prefetch(@opts[:prefetch])

  handler_klass = worker.opts[:handler] || Sneakers::CONFIG.fetch(:handler)
  # Configure options if needed
  if handler_klass.respond_to?(:configure_queue)
    @opts[:queue_options] = handler_klass.configure_queue(@name, @opts[:queue_options])
  end

  queue = @channel.queue(@name, @opts[:queue_options])

  header_bindings = [*@opts[:header_bindings]]
  header_bindings.each do |header_binding|
    exchange_name = header_binding[:exchange] || @opts[:exchange]
    exchange = @channel.exchange(exchange_name, @opts[:exchange_options].merge(type: :headers))
    queue.bind(exchange,
               arguments: {}.tap do |args|
                            args["x-match"] = :all
                          end
                 .merge(header_binding[:arguments]))
  end

  handler = handler_klass.new(@channel, queue, worker.opts)

  @consumer = queue.subscribe(block: false, manual_ack: @opts[:ack]) do |delivery_info, , msg|
    worker.do_work(delivery_info, , msg, handler)
  end
  nil
end

#infos_subscribes(infos, **custom_headers) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/strum/patch/sneakers/queue_patch.rb', line 79

def infos_subscribes(infos, **custom_headers)
  infos.each do |_info|
    exchange, resource = Strum::Esb::Functions.info_explain(_info)
    raise StandardError "info binding format must be a `exchange:resource`" unless resource

    @opts[:header_bindings] << {
      exchange: exchange,
      arguments: {
        info: resource
      }.merge(custom_headers)
    }
  end
end

#notices_subscribes(notices, **custom_headers) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/strum/patch/sneakers/queue_patch.rb', line 93

def notices_subscribes(notices, **custom_headers)
  notices.each do |_notice|
    exchange, resource, notice = Strum::Esb::Functions.notice_explain(_notice)
    raise StandardError "notice binding format must be a `exchange:resource`" unless resource

    @opts[:header_bindings] << {
      exchange: exchange,
      arguments: {
        notice: notice,
        resource: resource
      }.merge(custom_headers)
    }
  end
end

#pipeline_subscribes(pipelines) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/strum/patch/sneakers/queue_patch.rb', line 39

def pipeline_subscribes(pipelines)
  return if pipelines.nil?

  actions_subscribes([*(pipelines[:actions] || pipelines[:action])], pipeline: pipelines[:name])
  events_subscribes([*(pipelines[:events] || pipelines[:event])], pipeline: pipelines[:name])
  infos_subscribes([*(pipelines[:infos] || pipelines[:info])], pipeline: pipelines[:name])
  notices_subscribes([*(pipelines[:notices] || pipelines[:notice])], pipeline: pipelines[:name])
end

#strum_subscribe(worker) ⇒ Object

bindings: {

actions: "get/account",
events: %w[account/update account/delete],
infos: "account"
pipeline: {
  name: "pipeline-name",
  actions: "get/account",
  events: %w[account/update account/delete],
  infos: "account"
}

}



29
30
31
32
33
34
35
36
37
# File 'lib/strum/patch/sneakers/queue_patch.rb', line 29

def strum_subscribe(worker)
  @opts[:header_bindings] = []
  actions_subscribes([*(@opts[:bindings][:actions] || @opts[:bindings][:action])])
  events_subscribes([*(@opts[:bindings][:events] || @opts[:bindings][:event])])
  infos_subscribes([*(@opts[:bindings][:infos] || @opts[:bindings][:info])])
  notices_subscribes([*(@opts[:bindings][:notices] || @opts[:bindings][:notice])])
  pipeline_subscribes(@opts[:bindings][:pipeline] || @opts[:bindings][:pipelines])
  header_subscribe(worker)
end

#subscribe(worker) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/strum/patch/sneakers/queue_patch.rb', line 7

def subscribe(worker)
  if @opts[:bindings]
    strum_subscribe(worker)
  elsif @opts[:header_bindings]
    header_subscribe(worker)
  else
    super(worker)
  end
end