@@ -9,8 +9,11 @@ defmodule Logger.Watcher do
9
9
Starts the watcher supervisor.
10
10
"""
11
11
def start_link ( m , f , a ) do
12
- options = [ strategy: :one_for_one , name: @ name ]
13
- case Supervisor . start_link ( [ ] , options ) do
12
+ import Supervisor.Spec
13
+ child = worker ( __MODULE__ , [ ] ,
14
+ [ function: :watcher , restart: :transient ] )
15
+ options = [ strategy: :simple_one_for_one , name: @ name ]
16
+ case Supervisor . start_link ( [ child ] , options ) do
14
17
{ :ok , _ } = ok ->
15
18
_ = for { mod , handler , args } <- apply ( m , f , a ) do
16
19
{ :ok , _ } = watch ( mod , handler , args )
@@ -32,16 +35,9 @@ defmodule Logger.Watcher do
32
35
Watches the given handler as part of the watcher supervision tree.
33
36
"""
34
37
def watch ( mod , handler , args ) do
35
- import Supervisor.Spec
36
- id = { mod , handler }
37
- child = worker ( __MODULE__ , [ mod , handler , args ] ,
38
- [ id: id , function: :watcher , restart: :transient ] )
39
- case Supervisor . start_child ( @ name , child ) do
38
+ case Supervisor . start_child ( @ name , [ mod , handler , args ] ) do
40
39
{ :ok , _pid } = result ->
41
40
result
42
- { :error , :already_present } ->
43
- _ = Supervisor . delete_child ( @ name , id )
44
- watch ( mod , handler , args )
45
41
{ :error , _reason } = error ->
46
42
error
47
43
end
@@ -64,17 +60,28 @@ defmodule Logger.Watcher do
64
60
ref = Process . monitor ( mod )
65
61
66
62
case GenEvent . add_handler ( mod , handler , args , monitor: true ) do
67
- :ok -> { :ok , { mod , handler , ref } }
68
- { :error , :ignore } -> :ignore
69
- { :error , reason } -> { :stop , reason }
63
+ :ok ->
64
+ { :ok , { mod , handler , ref } }
65
+ { :error , :ignore } ->
66
+ # Can't return :ignore as a transient child under a simple_one_for_one.
67
+ # Instead return ok and then immediately exit normally - using a fake
68
+ # message.
69
+ send ( self ( ) , { :gen_event_EXIT , handler , :normal } )
70
+ { :ok , { mod , handler , ref } }
71
+ { :error , reason } ->
72
+ { :stop , reason }
70
73
end
71
74
end
72
75
73
76
def init ( { mod , handler , args , :link } ) do
74
77
case :gen_event . add_sup_handler ( mod , handler , args ) do
75
- :ok -> { :ok , { mod , handler , nil } }
76
- { :error , :ignore } -> :ignore
77
- { :error , reason } -> { :stop , reason }
78
+ :ok ->
79
+ { :ok , { mod , handler , nil } }
80
+ { :error , :ignore } ->
81
+ send ( self ( ) , { :gen_event_EXIT , handler , :normal } )
82
+ { :ok , { mod , handler , nil } }
83
+ { :error , reason } ->
84
+ { :stop , reason }
78
85
end
79
86
end
80
87
0 commit comments