arizona_reloader_rule (arizona v0.4.0)

View Source

Runs one dev-reloader rule: watches a directory through the configured arizona_watcher implementation, filters the changed paths by the rule's patterns, debounces bursts, and hands the resulting file list to the rule's callback and the arizona_watcher pubsub topic.

arizona_reloader_sup runs one per rule of the reloader application env (child_specs/1); see arizona_watcher for the config and the watcher contract.

Usage

1> arizona_reloader_rule:start_link("/home/me/myapp/src", #{
       watcher => arizona_watcher_fs,
       patterns => [".*\\.erl$"],
       debounce => 200,
       callback => fun arizona_reloader:reload_erl/1
   }).
{ok, <0.123.0>}

Subscribers of the arizona_watcher pubsub topic receive {arizona_watcher, Dir, Files} after each debounced burst, Dir being the rule's absolute directory, so one subscriber can tell the rules apart.

Options

  • watcher -- the arizona_watcher implementation: a module, or {Module, Opts} to pass it options (required)
  • patterns -- list of regex strings to filter file paths (default [".*"])
  • callback -- optional fun([string()]) -> term() invoked synchronously with the changed file list before the broadcast
  • debounce -- milliseconds to wait after the last change before flushing (default 100); coalesces editor-save bursts into a single event

Summary

Functions

The child specs for the reloader application env: one rule process per rule when it holds enabled => true and a non-empty rules list, none otherwise.

Formats the reloader config errors child_specs/1 raises into an actionable boot message. Picked up by erl_error:format_exception/3 via the error_info annotation at the raise site.

Starts a rule process for Dir with the given options.

Types

callback()

-nominal callback() :: fun(([string()]) -> term()).

state()

-type state() ::
          #state{abs_dir :: string(),
                 watcher :: module(),
                 watcher_state :: term(),
                 compiled :: [re:mp()],
                 callback :: callback() | undefined,
                 debounce_ms :: pos_integer(),
                 debounce_timer :: reference() | undefined,
                 pending_files :: sets:set(string())}.

watcher()

-nominal watcher() :: module() | {module(), map()}.

Functions

child_specs(Reloader)

-spec child_specs(Reloader) -> [supervisor:child_spec()] when Reloader :: map().

The child specs for the reloader application env: one rule process per rule when it holds enabled => true and a non-empty rules list, none otherwise.

Raises a formatted error when there are rules to run but the watcher key is missing, names a module that is not on the code path (the package providing it is not a dependency of this build), or names one that does not implement arizona_watcher, and when a rule has no directory.

format_error(Reason, Stacktrace)

-spec format_error(Reason, Stacktrace) -> ErrorInfo
                      when
                          Reason :: term(), Stacktrace :: [tuple()], ErrorInfo :: #{general := iolist()}.

Formats the reloader config errors child_specs/1 raises into an actionable boot message. Picked up by erl_error:format_exception/3 via the error_info annotation at the raise site.

handle_call(Request, From, State)

handle_cast(Msg, State)

handle_info/2

init/1

-spec init({Dir, Opts}) -> {ok, state()} | {stop, term()} when Dir :: string(), Opts :: map().

start_link(Dir, Opts)

-spec start_link(Dir, Opts) -> gen_server:start_ret()
                    when Dir :: string(), Opts :: #{watcher := watcher(), _ => term()}.

Starts a rule process for Dir with the given options.

Errors with {not_a_directory, Dir} if the path is not an existing directory.

terminate/2