Method: Que::Listener#wait_for_messages

Defined in:
lib/que/listener.rb

#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 wait_for_messages(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

  accumulated_messages = []

  # 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 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!",
        # Massage message and format a bit to make these errors more readable.
        "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