Botica.Scheduler (Botica v2.2.0)

Copy Markdown View Source

Runs check groups on a schedule.

A scheduler entry pairs a schedule with a Botica.Check.Group (or any zero-arity runner function). The scheduler fires the group, stores the result in a bounded ETS history, and optionally notifies a subscriber after every run.

Schedules

Two forms are supported:

  • every: {:minutes, N} / {:seconds, N} / {:milliseconds, N} — fixed interval.
  • cron: "*/5 * * * *" — a 5-field cron expression (minute hour day-of-month month day-of-week). Only * and */N steps are supported in the first field; other fields must be *.

Example

group = Botica.Check.Group.new(:db, [check_db], timeout_ms: 2_000)

:ok = Botica.Scheduler.add(:db_every_5m, group, every: {:minutes, 5})
:ok = Botica.Scheduler.add(:db_at_night, group, cron: "*/5 * * * *")

History

The last max_history results per schedule are kept in a private ETS table (default 10). Query with history/1.

Summary

Functions

Registers a scheduled run.

Returns all registered schedules (name → schedule spec).

Returns a specification to start this module under a supervisor.

Returns the last max_history results for a schedule (most recent first).

Removes a scheduled run.

Manually triggers a schedule now (useful for tests and on-demand runs).

Functions

add(name, group_or_fun, opts \\ [])

@spec add(atom(), Botica.Check.Group.t() | (-> term()), keyword()) ::
  :ok | {:error, term()}

Registers a scheduled run.

Options

  • :every — {:milliseconds, n} | {:seconds, n} | {:minutes, n}

  • :cron — cron expression (5 fields, */*/N in minute field)
  • :runner — zero-arity fun (defaults to fn -> Group.run(group) end)
  • :max_history — history entries kept (default 10)
  • :notify — fun.({:ok, results}) | ({:error, reason}) after each run

Exactly one of :every / :cron must be provided.

all()

@spec all() :: [{atom(), term()}]

Returns all registered schedules (name → schedule spec).

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

history(name)

@spec history(atom()) :: [term()]

Returns the last max_history results for a schedule (most recent first).

remove(name)

@spec remove(atom()) :: :ok

Removes a scheduled run.

run_now(name)

@spec run_now(atom()) :: :ok | {:error, :not_found}

Manually triggers a schedule now (useful for tests and on-demand runs).