{"id":18759007,"url":"https://github.com/tattdcodemonkey/gen_event_patterns","last_synced_at":"2025-06-28T17:07:59.929Z","repository":{"id":30937003,"uuid":"34495014","full_name":"TattdCodeMonkey/gen_event_patterns","owner":"TattdCodeMonkey","description":"exploration with elixir GenEvent handlers","archived":false,"fork":false,"pushed_at":"2015-04-25T05:56:38.000Z","size":128,"stargazers_count":15,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-13T02:27:25.503Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TattdCodeMonkey.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-04-24T03:05:48.000Z","updated_at":"2023-09-01T10:53:34.000Z","dependencies_parsed_at":"2022-09-09T01:54:09.650Z","dependency_job_id":null,"html_url":"https://github.com/TattdCodeMonkey/gen_event_patterns","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/TattdCodeMonkey/gen_event_patterns","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TattdCodeMonkey%2Fgen_event_patterns","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TattdCodeMonkey%2Fgen_event_patterns/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TattdCodeMonkey%2Fgen_event_patterns/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TattdCodeMonkey%2Fgen_event_patterns/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TattdCodeMonkey","download_url":"https://codeload.github.com/TattdCodeMonkey/gen_event_patterns/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TattdCodeMonkey%2Fgen_event_patterns/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262465785,"owners_count":23315641,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-07T17:48:46.411Z","updated_at":"2025-06-28T17:07:59.907Z","avatar_url":"https://github.com/TattdCodeMonkey.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"Elixir GenEvent Testing\n================\n\nThis project is used to show three methods of adding event handlers to a `GenEvent` manager.\n\n1. use `add_handler`:\n  - this adds a handler to the `GenEvent` manager, but does not do any supervision and is not fault tolerant\n2. use `add_mon_handler` without handling exits:\n  - 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\n3. user `add_mon_handler` handle exits:\n  - 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`\n\nIn 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.\n\n## Generic Event Handler used in module\n```elixir\ndefmodule EventHandler do\n  use GenEvent\n  require Logger\n\n  def init(parent) do\n    {:ok, parent}\n  end\n\n  def handle_event({:log, msg}, parent) do\n    \"handled log: #{inspect msg} in #{__MODULE__}\"\n    |\u003e Logger.info\n\n    {:ok, parent}\n  end\n\n  def handle_event({:crash, _}, parent) do\n    \"crash event handler #{__MODULE__}\"\n    |\u003e Logger.info\n\n    1 = 2\n  end\n\n  def handle_event(event, parent) do\n    \"handled event: #{inspect event} in #{__MODULE__}\"\n    |\u003e Logger.info\n\n    {:ok, parent}\n  end\nend\n```\n\n## Pattern 1 - basic event handler with no monitoring\n\n```elixir\ndefmodule EventHandlerServer do\n  def start_link(opts) do\n    GenServer.start_link(__MODULE__, opts, []);\n  end\n\n  def init([opts]) do\n    GenEvent.add_handler(opts.manager_name, EventHandler, self())\n\n    {:ok, opts}\n  end\nend\n```\n\n## Pattern 2 - monitored event handler\n```elixir\ndefmodule EventHandlerServer do\n  def start_link(opts) do\n    GenServer.start_link(__MODULE__, opts,[]);\n  end\n\n  def init([opts]) do\n    :ok = GenEvent.add_mon_handler(opts.manager_name, EventHandler, self())\n\n    {:ok, opts}\n  end\nend\n```\n\n## Pattern 3 - monitored event handler, restarts handler on exit\n\n```elixir\ndefmodule EventHandlerServer do\n  def start_link(opts) do\n    GenServer.start_link(__MODULE__, opts,[]);\n  end\n\n  def init([opts]) do\n    start_handler(opts)\n    {:ok, opts}\n  end\n\n  def start_handler(opts) do\n    :ok = GenEvent.add_mon_handler(opts.manager_name, EventHandler, self())\n  end\n\n  def handle_info({:gen_event_EXIT, handler, reason}, state)\n    when reason in [:normal, :shutdown] do\n    {:stop, reason, state}\n  end\n\n  def handle_info({:gen_event_EXIT, handler, reason}, state) do\n    start_handler(state)\n\n    {:noreply, state}\n  end\nend\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftattdcodemonkey%2Fgen_event_patterns","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftattdcodemonkey%2Fgen_event_patterns","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftattdcodemonkey%2Fgen_event_patterns/lists"}