@@ -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
0 commit comments