Class: Que::Listener
- Inherits:
-
Object
- Object
- Que::Listener
- Defined in:
- lib/que/listener.rb
Constant Summary collapse
- MESSAGE_FORMATS =
{}
Instance Attribute Summary collapse
-
#channel ⇒ Object
readonly
Returns the value of attribute channel.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Instance Method Summary collapse
-
#initialize(connection:, channel: nil) ⇒ Listener
constructor
A new instance of Listener.
- #listen ⇒ Object
- #unlisten ⇒ Object
- #wait_for_grouped_messages(timeout) ⇒ Object
- #wait_for_messages(timeout) ⇒ Object
Constructor Details
#initialize(connection:, channel: nil) ⇒ Listener
Returns a new instance of Listener.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/que/listener.rb', line 9 def initialize(connection:, channel: nil) @connection = connection @channel = channel || "que_listener_#{connection.backend_pid}" Que.internal_log :listener_instantiate, self do { backend_pid: connection.backend_pid, } end end |
Instance Attribute Details
#channel ⇒ Object (readonly)
Returns the value of attribute channel.
7 8 9 |
# File 'lib/que/listener.rb', line 7 def channel @channel end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
7 8 9 |
# File 'lib/que/listener.rb', line 7 def connection @connection end |
Instance Method Details
#listen ⇒ Object
20 21 22 |
# File 'lib/que/listener.rb', line 20 def listen connection.execute "LISTEN #{channel}" end |
#unlisten ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/que/listener.rb', line 129 def unlisten # Be sure to drain all notifications so that any code that uses this # connection later doesn't receive any nasty surprises. connection.execute "UNLISTEN *" connection.drain_notifications Que.internal_log :listener_unlisten, self do { backend_pid: connection.backend_pid, channel: channel, } end end |
#wait_for_grouped_messages(timeout) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/que/listener.rb', line 24 def (timeout) = (timeout) output = {} .each do || = .delete(:message_type) (output[.to_sym] ||= []) << .freeze end output end |
#wait_for_messages(timeout) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/que/listener.rb', line 38 def (timeout) # Make sure we never pass nil to this method, so we don't hang the thread. Que.assert(Numeric, timeout) Que.internal_log :listener_waiting, self do { backend_pid: connection.backend_pid, channel: channel, timeout: timeout, } end = [] # Notifications often come in batches (especially when a transaction that # inserted many jobs commits), so we want to loop and pick up all the # received notifications before continuing. loop do notification_received = connection.wait_for_notify(timeout) do |channel, pid, payload| # We've received a notification, so zero out the timeout before we # loop again to check for another message. This ensures that we # don't wait an additional `timeout` seconds after processing the # final message before this method returns. timeout = 0 Que.internal_log(:listener_received_notification, self) do { channel: channel, backend_pid: connection.backend_pid, source_pid: pid, payload: payload, } end # Be very defensive about the message we receive - it may not be # valid JSON or have the structure we expect. next unless = parse_payload(payload) case when Array then .concat() when Hash then << else raise Error, "Unexpected parse_payload output: #{.class}" end end break unless notification_received end return if .empty? Que.internal_log(:listener_received_messages, self) do { backend_pid: connection.backend_pid, channel: channel, messages: , } end .keep_if do || next unless .is_a?(Hash) next unless type = [:message_type] next unless type.is_a?(String) next unless format = MESSAGE_FORMATS[type.to_sym] if (, format) true else = [ "Message of type '#{type}' doesn't match format!", # Massage message and format a bit to make these errors more readable. "Message: #{Hash[.reject{|k,v| k == :message_type}.sort_by{|k,v| k}].inspect}", "Format: #{Hash[format.sort_by{|k,v| k}].inspect}", ].join("\n") Que.notify_error_async(Error.new()) false end end Que.internal_log(:listener_filtered_messages, self) do { backend_pid: connection.backend_pid, channel: channel, messages: , } end end |