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 wait_for_messages(timeout)
Que.assert(Numeric, timeout)
Que.internal_log :listener_waiting, self do
{
backend_pid: connection.backend_pid,
channel: channel,
timeout: timeout,
}
end
accumulated_messages = []
loop do
notification_received =
connection.wait_for_notify(timeout) do |channel, pid, payload|
timeout = 0
Que.internal_log(:listener_received_notification, self) do
{
channel: channel,
backend_pid: connection.backend_pid,
source_pid: pid,
payload: payload,
}
end
next unless message = parse_payload(payload)
case message
when Array then accumulated_messages.concat(message)
when Hash then accumulated_messages << message
else raise Error, "Unexpected parse_payload output: #{message.class}"
end
end
break unless notification_received
end
return accumulated_messages if accumulated_messages.empty?
Que.internal_log(:listener_received_messages, self) do
{
backend_pid: connection.backend_pid,
channel: channel,
messages: accumulated_messages,
}
end
accumulated_messages.keep_if do |message|
next unless message.is_a?(Hash)
next unless type = message[:message_type]
next unless type.is_a?(String)
next unless format = MESSAGE_FORMATS[type.to_sym]
if message_matches_format?(message, format)
true
else
error_message = [
"Message of type '#{type}' doesn't match format!",
"Message: #{Hash[message.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(error_message))
false
end
end
Que.internal_log(:listener_filtered_messages, self) do
{
backend_pid: connection.backend_pid,
channel: channel,
messages: accumulated_messages,
}
end
accumulated_messages
end
|