Class: Que::Listener

Inherits:
Object
  • Object
show all
Defined in:
lib/que/listener.rb

Constant Summary collapse

MESSAGE_FORMATS =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#channelObject (readonly)

Returns the value of attribute channel.



7
8
9
# File 'lib/que/listener.rb', line 7

def channel
  @channel
end

#connectionObject (readonly)

Returns the value of attribute connection.



7
8
9
# File 'lib/que/listener.rb', line 7

def connection
  @connection
end

Instance Method Details

#listenObject



20
21
22
# File 'lib/que/listener.rb', line 20

def listen
  connection.execute "LISTEN #{channel}"
end

#unlistenObject



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 wait_for_grouped_messages(timeout)
  messages = wait_for_messages(timeout)

  output = {}

  messages.each do |message|
    message_type = message.delete(:message_type)

    (output[message_type.to_sym] ||= []) << message.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 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