@@ -45,6 +45,8 @@ defmodule EctoWatch.WatcherServer do
45
45
46
46
@ impl true
47
47
def init ( { repo_mod , pub_sub_mod , options } ) do
48
+ debug_log ( options , "Starting server" )
49
+
48
50
unique_label = "#{ unique_label ( options ) } "
49
51
50
52
update_keyword =
@@ -152,7 +154,7 @@ defmodule EctoWatch.WatcherServer do
152
154
"#{ state . unique_label } "
153
155
end
154
156
155
- { :ok , { state . pub_sub_mod , channel_name } }
157
+ { :ok , { state . pub_sub_mod , channel_name , state . options . debug? } }
156
158
end
157
159
158
160
{ :reply , result , state }
@@ -182,6 +184,11 @@ defmodule EctoWatch.WatcherServer do
182
184
183
185
@ impl true
184
186
def handle_info ( { :notification , _pid , _ref , channel_name , payload } , state ) do
187
+ debug_log (
188
+ state . options ,
189
+ "Received Postgrex notification on channel `#{ channel_name } `: #{ payload } "
190
+ )
191
+
185
192
details = watcher_details ( state )
186
193
187
194
if channel_name != details . notify_channel do
@@ -210,6 +217,11 @@ defmodule EctoWatch.WatcherServer do
210
217
returned_values ,
211
218
state . identifier_columns
212
219
) do
220
+ debug_log (
221
+ state . options ,
222
+ "Broadcasting to Phoenix PubSub topic `#{ topic } `: #{ inspect ( message ) } "
223
+ )
224
+
213
225
Phoenix.PubSub . broadcast ( state . pub_sub_mod , topic , message )
214
226
end
215
227
@@ -244,11 +256,9 @@ defmodule EctoWatch.WatcherServer do
244
256
# that can be used as the watcher process name, trigger name, trigger function name,
245
257
# and Phoenix.PubSub channel name.
246
258
defp unique_label ( % WatcherOptions { } = options ) do
247
- if options . label do
248
- unique_label ( options . label )
249
- else
250
- unique_label ( { options . schema_definition . label , options . update_type } )
251
- end
259
+ options
260
+ |> identifier ( )
261
+ |> unique_label ( )
252
262
end
253
263
254
264
defp unique_label ( { schema_mod , update_type } ) do
@@ -258,4 +268,18 @@ defmodule EctoWatch.WatcherServer do
258
268
defp unique_label ( label ) do
259
269
:"ew_for_#{ Helpers . label ( label ) } "
260
270
end
271
+
272
+ defp identifier ( % WatcherOptions { } = options ) do
273
+ if options . label do
274
+ options . label
275
+ else
276
+ { options . schema_definition . label , options . update_type }
277
+ end
278
+ end
279
+
280
+ defp debug_log ( % { debug?: debug_value } = options , message ) do
281
+ if debug_value do
282
+ Helpers . debug_log ( identifier ( options ) , message )
283
+ end
284
+ end
261
285
end
0 commit comments