Supervised GenEvent

I’ve read the GenEvent documentation and something remains unclear to me.

I wonder how to include event managers in a supervision tree and have event handlers added to the manager after it restarts.

Suppose we have a Forum application composed as below. The Comment component provides a service that manages comments and publishes comment events when new comments are stored. The Spam service is notified of the comments and flags the comment if it is spammy. Lets say those components are each implemented as a GenServer.

defmodule Forum do
  use Application
  def start(_type, _args) do
    import Supervisor.Spec
    children = [
      # ... some other components, probably
      worker(GenEvent, [[name: CommentEvents]]),
      worker(Comment, [[manager: CommentEvents]]), # let's pass the name for testability
      worker(Spam, [[manager: CommentEvents]]),
      # ... what do ??
    Supervisor.start_link(children, strategy: :rest_for_one]

Where should I wire up the handler so the Spam service is notified of comment events even if either the manager or service is restarted.

I suppose one could add another worker to the supervision tree that monitors both processes and adds the handler on DOWN messages. Or maybe by carefully crafting a multi-level supervision tree with different restart strategies would work.

But I wonder if there is a simpler way or if anyone can share some examples they have.

Thanks a bunch!

:smile: :smile: :smile: