I assume the modules are listed only once in a file. Why not make use of callbacks to collect event handlers and register handlers? This way the ‘use’ is ehhhh un’use’d
Example in Routex. (working on this lib at the moment, so maybe all I see is a hammer right now)
For your use case this could look like:
defmodule Processor do
ast = for module <- @modules do
apply(module, :generate_handlers, [])
ene
Module.create(MyHandlers, ast)
end
defmodule LoggerX do
def generate_handlers() do
quote do
def handle_event([:oban, :job, :exception], _measure, meta, _) do
Logger.error(fn ->
"[Oban] #{meta.worker} error: #{inspect(meta.error)} args: #{inspect(meta.args)}"
end)
end
end
end
The events to collect are missing in this example but that’s not that hard to add (but still on mobile). Does this seem a viable option for your use case?
- we could even traverse the returned AST to know all handled events.