-
Notifications
You must be signed in to change notification settings - Fork 123
/
queue.ex
181 lines (159 loc) · 6.36 KB
/
queue.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
defmodule AMQP.Queue do
@moduledoc """
Functions to operate on Queues.
"""
import AMQP.Core
alias AMQP.Channel
alias AMQP.Basic
alias AMQP.Utils
@doc """
Declares a queue. The optional `queue` parameter is used to set the name.
If set to an empty string (default), the server will assign a name.
Besides the queue name, the following options can be used:
# Options
* `:durable` - If set, keeps the Queue between restarts of the broker
* `:auto_delete` - If set, deletes the Queue once all subscribers disconnect
* `:exclusive` - If set, only one subscriber can consume from the Queue
* `:passive` - If set, raises an error unless the queue already exists
"""
@spec declare(Channel.t, String.t, keyword) :: {:ok, map}
def declare(%Channel{pid: pid}, queue \\ "", options \\ []) do
queue_declare =
queue_declare(queue: queue,
passive: Keyword.get(options, :passive, false),
durable: Keyword.get(options, :durable, false),
exclusive: Keyword.get(options, :exclusive, false),
auto_delete: Keyword.get(options, :auto_delete, false),
nowait: Keyword.get(options, :no_wait, false),
arguments: Keyword.get(options, :arguments, []) |> Utils.to_type_tuple)
queue_declare_ok(queue: queue,
message_count: message_count,
consumer_count: consumer_count) = :amqp_channel.call pid, queue_declare
{:ok, %{queue: queue, message_count: message_count, consumer_count: consumer_count}}
end
@doc """
Binds a Queue to an Exchange
"""
@spec bind(Channel.t, String.t, String.t, keyword) :: :ok
def bind(%Channel{pid: pid}, queue, exchange, options \\ []) do
queue_bind =
queue_bind(queue: queue,
exchange: exchange,
routing_key: Keyword.get(options, :routing_key, ""),
nowait: Keyword.get(options, :no_wait, false),
arguments: Keyword.get(options, :arguments, []) |> Utils.to_type_tuple)
queue_bind_ok() = :amqp_channel.call pid, queue_bind
:ok
end
@doc """
Unbinds a Queue from an Exchange
"""
@spec unbind(Channel.t, String.t, String.t, keyword) :: :ok
def unbind(%Channel{pid: pid}, queue, exchange, options \\ []) do
queue_unbind =
queue_unbind(queue: queue,
exchange: exchange,
routing_key: Keyword.get(options, :routing_key, ""),
arguments: Keyword.get(options, :arguments, []))
queue_unbind_ok() = :amqp_channel.call pid, queue_unbind
:ok
end
@doc """
Deletes a Queue by name
"""
@spec delete(Channel.t, String.t, keyword) :: {:ok, map}
def delete(%Channel{pid: pid}, queue, options \\ []) do
queue_delete =
queue_delete(queue: queue,
if_unused: Keyword.get(options, :if_unused, false),
if_empty: Keyword.get(options, :if_empty, false),
nowait: Keyword.get(options, :no_wait, false))
queue_delete_ok(message_count: message_count) = :amqp_channel.call pid, queue_delete
{:ok, %{message_count: message_count}}
end
@doc """
Discards all messages in the Queue
"""
@spec purge(Channel.t, String.t) :: {:ok, map}
def purge(%Channel{pid: pid}, queue) do
queue_purge_ok(message_count: message_count) = :amqp_channel.call pid, queue_purge(queue: queue)
{:ok, %{message_count: message_count}}
end
@doc """
Returns the message count and consumer count for the given queue.
Uses Queue.declare with the `passive` option set.
"""
@spec status(Channel.t, String.t) :: {:ok, map}
def status(%Channel{} = chan, queue) do
declare(chan, queue, passive: true)
end
@doc """
Returns the number of messages that are ready for delivery (e.g. not pending acknowledgements)
in the queue
"""
@spec message_count(Channel.t, String.t) :: integer
def message_count(%Channel{} = channel, queue) do
{:ok, %{message_count: message_count}} = status(channel, queue)
message_count
end
@doc """
Returns a number of active consumers on the queue
"""
@spec consumer_count(Channel.t, String.t) :: integer
def consumer_count(%Channel{} = channel, queue) do
{:ok, %{consumer_count: consumer_count}} = status(channel, queue)
consumer_count
end
@doc """
Returns true if queue is empty (has no messages ready), false otherwise
"""
@spec empty?(Channel.t, String.t) :: boolean
def empty?(%Channel{} = channel, queue) do
message_count(channel, queue) == 0
end
@doc """
Convenience to consume messages from a Queue.
The handler function must have arity 2 and will receive as arguments a binary with the message payload
and a Map with the message properties.
The consumed message will be acknowledged after executing the handler function.
If an exception is raised by the handler function, the message is rejected.
This convenience function will spawn a process and register it using AMQP.Basic.consume.
"""
@spec subscribe(Channel.t, String.t, (String.t, map -> any)) :: {:ok, String.t}
def subscribe(%Channel{} = channel, queue, fun) when is_function(fun, 2) do
consumer_pid = spawn fn -> do_start_consumer(channel, fun) end
Basic.consume(channel, queue, consumer_pid)
end
defp do_start_consumer(channel, fun) do
receive do
{:basic_consume_ok, %{consumer_tag: consumer_tag}} ->
do_consume(channel, fun, consumer_tag)
end
end
defp do_consume(channel, fun, consumer_tag) do
receive do
{:basic_deliver, payload, %{delivery_tag: delivery_tag} = meta} ->
try do
fun.(payload, meta)
Basic.ack(channel, delivery_tag)
rescue
exception ->
stacktrace = System.stacktrace
Basic.reject(channel, delivery_tag, requeue: false)
reraise exception, stacktrace
end
do_consume(channel, fun, consumer_tag)
{:basic_cancel, %{consumer_tag: ^consumer_tag, no_wait: _}} ->
exit(:basic_cancel)
{:basic_cancel_ok, %{consumer_tag: ^consumer_tag}} ->
exit(:normal)
end
end
@doc """
Convenience to end a Queue consumer.
"""
@spec unsubscribe(Channel.t, String.t) :: {:ok, String.t}
def unsubscribe(%Channel{} = channel, consumer_tag) do
Basic.cancel(channel, consumer_tag)
end
end