[erlang-questions] otp supervisor terminate callback

Vance Shipley vances@REDACTED
Wed Apr 24 15:06:55 CEST 2013

On Wed, Apr 24, 2013 at 06:49:45PM +0800, Seven Du wrote:
}  I want a terminate callback in the -behaviour(supervisor).

Sometimes it's easiest to change what you want.  :)

On Apr 20, 2013 4:34 PM, "Seven Du" <dujinfang@REDACTED> wrote:
}  Specifically I want to write an application which starts a supervisor, 
}  and add a handler to some existing gen_event by calling 
}  gen_event:add_handler() on init(), and I want to call 
}  gen_event:remove_handler() when the application is stop. 

If your application really only installs an event handler you might
not need a supervisor at all.  You can install the handler in your
application behaviour start/1,2 callback and uninstall it in the
stop/1 callback.

For a robust application you would probably want to have a supervisor
with a child worker, say a gen_server behaviour, which can supervise
the event handler.  Have your application behaviour start/1,2 callback 
start the supervisor.  The supervisor starts the gen_server.  The init/1
callback of the gen_server behaviour would use event_handler:add_sup_handler/3
to install the event handler.  Add a clause in your handle_info/2 callback
to handle the case where the event handler gets removed:

     handle_info({gen_event_EXIT, Handler, Reason}, State) -> 
          error_logger:error_report([gen_event_EXIT, {event_handler, Handler},
                    {reason, Reason}]),
          case gen_event:add_sup_handler(State#state.ref, Handler, []) of
               ok ->
                    {noreply, State}
               Other ->
                    {stop, Other, State}


More information about the erlang-questions mailing list