Skip to content

Latest commit

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Elixir GenEvent Testing

This project is used to show three methods of adding event handlers to a GenEvent manager.

  1. use add_handler:
  • this adds a handler to the GenEvent manager, but does not do any supervision and is not fault tolerant
  1. use add_mon_handler without handling exits:
  • this adds a monitored handler to the GenEvent manager, but does not explicitly handle :gen_event_EXIT messages. instead it relies on crashing the server its in so that it can be restarted by its supervisor
  1. user add_mon_handler handle exits:
  • this adds a monitored handler to the GenEvent manager, and explicitly handles exits. reading the event handler for any reason that is not :normal or :shutdown

In addition to the three handlers there are also two supervisors. One supervisor manages the three handlers with a :one_for_one strategy. The other manages the GenEvent manager and the handlers supervisor with a :one_for_all strategy. This ensures the handlers are re-added if the manager crashes for any reason.

Generic Event Handler used in module

defmoduleEventHandlerdouseGenEventrequireLoggerdefinit(parent)do{:ok,parent}enddefhandle_event({:log,msg},parent)do"handled log: #{inspectmsg} in #{__MODULE__}"|>Logger.info{:ok,parent}enddefhandle_event({:crash,_},parent)do"crash event handler #{__MODULE__}"|>Logger.info1=2enddefhandle_event(event,parent)do"handled event: #{inspectevent} in #{__MODULE__}"|>Logger.info{:ok,parent}endend

Pattern 1 - basic event handler with no monitoring

defmoduleEventHandlerServerdodefstart_link(opts)doGenServer.start_link(__MODULE__,opts,[]);enddefinit([opts])doGenEvent.add_handler(opts.manager_name,EventHandler,self()){:ok,opts}endend

Pattern 2 - monitored event handler

defmoduleEventHandlerServerdodefstart_link(opts)doGenServer.start_link(__MODULE__,opts,[]);enddefinit([opts])do:ok=GenEvent.add_mon_handler(opts.manager_name,EventHandler,self()){:ok,opts}endend

Pattern 3 - monitored event handler, restarts handler on exit

defmoduleEventHandlerServerdodefstart_link(opts)doGenServer.start_link(__MODULE__,opts,[]);enddefinit([opts])dostart_handler(opts){:ok,opts}enddefstart_handler(opts)do:ok=GenEvent.add_mon_handler(opts.manager_name,EventHandler,self())enddefhandle_info({:gen_event_EXIT,handler,reason},state)whenreasonin[:normal,:shutdown]do{:stop,reason,state}enddefhandle_info({:gen_event_EXIT,handler,reason},state)dostart_handler(state){:noreply,state}endend

About

exploration with elixir GenEvent handlers

Resources

Stars

15 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages