Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tattdcodemonkey/mon_handler
Elixir GenServer used to monitor a GenEvent event handler
https://github.com/tattdcodemonkey/mon_handler
Last synced: 1 day ago
JSON representation
Elixir GenServer used to monitor a GenEvent event handler
- Host: GitHub
- URL: https://github.com/tattdcodemonkey/mon_handler
- Owner: TattdCodeMonkey
- License: mit
- Created: 2015-05-19T14:18:56.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-09-10T04:21:05.000Z (about 7 years ago)
- Last Synced: 2024-05-18T01:03:22.671Z (6 months ago)
- Language: Elixir
- Homepage:
- Size: 13.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- freaking_awesome_elixir - Elixir - A minimal GenServer that monitors a given GenEvent handler. (Actors)
- fucking-awesome-elixir - mon_handler - A minimal GenServer that monitors a given GenEvent handler. (Actors)
README
MonHandler - Monitored Event Handler
==========
[![Hex version](https://img.shields.io/hexpm/v/mon_handler.svg "Hex
version")](https://hex.pm/packages/mon_handler)This is a minimal `GenServer` module that is used to monitor an event handler.
To use simply call `add_mon_handler` or `start_link` with the `GenEvent` event manager, event handler and args for your event handler. Optionally you can also pass arguments for the `MonHandler` `GenServer`.
`MonHanlder` will handle messages from the event manager if the event handler terminates. For normal terminations `MonHandler` will stop. For terminations due to errors `MonHandler` will re-add the event handler to the event manager.
### Use Cases
Basic:
```elixir
iex(x)> {:ok, manager} = GenEvent.start_link
{:ok, #PID}
iex(x)> {:ok, mon_han} = MonHandler.start_link(manager, YourEventHandler, event_handler_args, gen_server_args)
{:ok, #PID}
iex(x)> GenEvent.notify(manager, {:your_event, "some data"})
:ok
iex(x)> MonHandler.remove_handler(mon_han)
:ok
```
Starting with a supervisor
```elixir
mon_handler_config = MonHandler.get_config(manager, YourEventHandler, event_handler_args)
supervise([worker(MonHandler, [mon_handler_config, []])], [strategy: :one_for_one])
```If you want to use MonHandler for multiple handlers within a single app you will need to give each an `id` when adding them to the supervisor.
```elixir
supervise(
[
worker(
MonHandler,
[
MonHandler.get_config(:manager_one, YourEventHandler, []),
[]
],
[id: :first_handler]
),
worker(
MonHandler,
[
MonHandler.get_config(:manager_two, YourSecondEventHandler, []),
[]
],
[id: :second_handler]
)
],
[strategy: :one_for_one]
)
```