Eai.Hub.Reloader (eai v1.0.1)

Copy Markdown

Hot-reloads hook modules from config/hooks/*.exs.

Reload flow

  1. Scan config/hooks/ for *.exs files (sorted by filename = priority prefix).
  2. Code.compile_file/1 each file — this defines the hook module in the BEAM.
  3. Collect {module, priority} pairs via module.__hook_entry__/0.
  4. Pass sorted entries to Eai.Hub.Pipeline.register/1 → stored in :persistent_term.

Why Code.compile_file instead of Code.eval_file?

compile_file creates proper BEAM modules (registered in code server), while eval_file only evaluates expressions in the current binding context. Hot-reloading requires proper module registration so that subsequent calls to the hook module dispatch via the BEAM's standard module lookup.

Why Code.compile_string for Hub regeneration (see hub.ex)?

The Hub module itself is regenerated (not the hook files). We use compile_string so we can build source dynamically from the registry, embedding the hook list directly. The hook files themselves use compile_file — they are static source files.

Safety

  • Files that fail to compile are skipped with a Logger warning; the rest of the reload still proceeds.
  • Old hook registrations are fully replaced on each reload (no accumulation across reloads). :persistent_term.put is atomic.

Summary

Functions

Reload all hooks from config/hooks/*.exs.

Functions

reload!()

@spec reload!() :: :ok | {:error, term()}

Reload all hooks from config/hooks/*.exs.

Returns :ok on success, {:error, reason} if the hooks directory cannot be read. Individual file compile failures are logged but do not cause an error return.