Skip to content

Commit 5401b80

Browse files
authored
Merge pull request #5 from OpenMatchmaking/feature-custom-channels
Added opportunity to created custom channels
2 parents 1f7ae6c + 6312632 commit 5401b80

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The package can be installed via adding the `spotter` dependency to your list of
1414

1515
```elixir
1616
def deps do
17-
[{:spotter, "~> 0.3.0"}]
17+
[{:spotter, "~> 0.4.0"}]
1818
end
1919
```
2020

@@ -110,9 +110,9 @@ Any of those arguments (that were mentioned in the documentation) can be specifi
110110

111111
# And don't forget to ack a processed message. Or perhaps even use nack
112112
# when it will be neceessary.
113-
# We will wrap the call into `safe_run(func)` call, so that it will retry
113+
# We will wrap the call into `safe_run(channel, func)` call, so that it will retry
114114
# the executed code when the used channel is failed
115-
safe_run(fn(channel) -> AMQP.Basic.ack(channel, tag) end)
115+
safe_run(channel, fn(channel) -> AMQP.Basic.ack(channel, tag) end)
116116
end
117117
end
118118
```

lib/worker.ex

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,20 @@ defmodule Spotter.Worker do
3535
end
3636

3737
def init(opts) do
38+
{channel_name, updated_opts} = Keyword.pop(opts[:opts], :channel_name, @channel_name)
39+
opts = Map.put(opts, :opts, updated_opts)
40+
3841
case Process.whereis(@connection) do
3942
nil ->
4043
# Connection doesn't exist, lets fail to recover later
4144
{:error, :noconn}
4245
_ ->
43-
@connection.spawn_channel(@channel_name)
44-
@connection.configure_channel(@channel_name, opts[:config])
46+
@connection.spawn_channel(channel_name)
47+
@connection.configure_channel(channel_name, opts[:config])
4548

46-
channel = get_channel()
49+
channel = get_channel(channel_name)
4750
{:ok, custom} = configure(channel, opts[:opts])
48-
{:ok, [channel: channel, meta: custom]}
51+
{:ok, [channel: channel, channel_name: channel_name, meta: custom]}
4952
end
5053
end
5154

@@ -57,35 +60,36 @@ defmodule Spotter.Worker do
5760
config
5861
end
5962

60-
defp get_channel() do
61-
channel = @channel_name
62-
|> @connection.get_channel
63+
defp get_channel(channel_name) do
64+
@connection.get_channel(channel_name)
6365
end
6466

6567
def status() do
6668
GenServer.call(__MODULE__, :status)
6769
end
6870

69-
def channel_config() do
70-
Spotter.AMQP.Connection.Channel.get_config(@channel_name)
71+
def channel_config(channel_name) do
72+
Spotter.AMQP.Connection.Channel.get_config(channel_name)
7173
end
7274

7375
def handle_call(:status, _from, state) do
74-
safe_run fn(_) ->
75-
{:reply, AMQP.Queue.status(state[:channel], channel_config()[:queue][:name]), state}
76-
end
76+
safe_run(
77+
state[:channel],
78+
fn(channel) ->
79+
config = channel_config(state[:channel_name])
80+
{:reply, AMQP.Queue.status(channel, config[:queue][:name]), state}
81+
end
82+
)
7783
end
7884

79-
def safe_run(fun) do
80-
channel = get_channel()
81-
85+
def safe_run(channel, fun) do
8286
case !is_nil(channel) && Process.alive?(channel.pid) do
8387
true ->
8488
fun.(channel)
8589
_ ->
86-
Logger.warn("[GenQueue] Channel #{inspect @channel_name} is dead, waiting till it gets restarted")
90+
Logger.warn("[GenQueue] Channel #{inspect channel} is dead, waiting till it gets restarted")
8791
:timer.sleep(3_000)
88-
safe_run(fun)
92+
safe_run(channel, fun)
8993
end
9094
end
9195

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Spotter.MixProject do
22
use Mix.Project
33

4-
@version "0.3.1"
4+
@version "0.4.0"
55

66
def project do
77
[

0 commit comments

Comments
 (0)