tarearbol v0.99.10 Tarearbol.Scheduler View Source

Cron-like task scheduler. Accepts both static and dynamic configurations.

Usage

Add Tarearbol.Scheduler to the list of supervised workers. It would attempt to read the static configuration (see below) and start the DynamicSupervisor with all the scheduled jobs as supervised workers.

The runner is the function of arity zero, that should return {:ok, result} tuple upon completion. The job will be rescheduled according to its schedule.

The last result returned will be stored in the state and might be retrieved later with get/1 passing the job name.

Static Configuration

Upon starts it looks up :tarearbol section of Mix.Project for :jobs and :jobs_file keys. The latter has a default .tarearbol.exs. This won’t work with releases.

Also it looks up :tarearbol, :jobs section of config.exs. Everything found is unioned. Jobs with the same names are overriden, the file has precedence over project config, the application config has least precedence.

If found, jobs as a list of tuples of {name, runner, schedule} are scheduled. These are expected to be in the following form.

  • name might be whatever, used to refer to the job during it’s lifetime
  • runner might be either {module, function} tuple or a reference to the function of arity zero (&Foo.bar/0)
  • schedule in standard cron notation, see https://crontab.guru

Dynamic Configuration

Use Tarearbol.Scheduler.push/3, Tarearbol.Scheduler.pop/1 to add/remove jobs temporarily and/or Tarearbol.Scheduler.push!/3, Tarearbol.Scheduler.pop!/1 to reflect changes in the configuration file.

Tarearbol.Scheduler.push(TestJob, &Foo.bar/0, "3-5/1 9-18 * * 6-7")

Link to this section Summary

Types

Type of the job runner, an {m, f} tuple or a function of arity zero, returning one of the outcomes below

Type of possible job schedules: binary cron format, Time to be executed once DateTime for the daily execution

Functions

Returns a specification to start this module under a supervisor.

Dynamically removes a supervised worker implementing Tarearbol.DynamicManager behaviour from the list of supervised children

Retrieves the information (payload, timeout, lull etc.) assotiated with the supervised worker

Dynamically removes a supervised worker implementing Tarearbol.DynamicManager behaviour from the list of supervised children on all the nodes managed by Cloister

Dynamically adds a supervised worker implementing Tarearbol.DynamicManager behaviour to the list of supervised children on all the nodes managed by Cloister

Removes the scheduled job from the schedule by id.

Removes the scheduled job from the schedule by id and updated the configuration.

Creates and temporarily pushes the job to the list of currently scheduled jobs.

Creates and pushes the job to the list of currently scheduled jobs, updates the permanent list of scheduled jobs.

Dynamically adds a supervised worker implementing Tarearbol.DynamicManager behaviour to the list of supervised children

Starts the DynamicSupervisor and its helpers to manage dynamic children

Link to this section Types

Specs

runner() ::
  {atom(), atom()} | (() -> :halt | {:ok | {:reschedule, binary()}, any()})

Type of the job runner, an {m, f} tuple or a function of arity zero, returning one of the outcomes below

Specs

schedule() :: binary() | non_neg_integer() | DateTime.t() | Time.t()

Type of possible job schedules: binary cron format, Time to be executed once DateTime for the daily execution

Link to this section Functions

Specs

active_jobs() :: %{
  required(atom()) => %Tarearbol.DynamicManager.Child{
    opts: term(),
    pid: term(),
    value: term()
  }
}

Returns a specification to start this module under a supervisor.

See Supervisor.

Dynamically removes a supervised worker implementing Tarearbol.DynamicManager behaviour from the list of supervised children

Retrieves the information (payload, timeout, lull etc.) assotiated with the supervised worker

Dynamically removes a supervised worker implementing Tarearbol.DynamicManager behaviour from the list of supervised children on all the nodes managed by Cloister

Dynamically adds a supervised worker implementing Tarearbol.DynamicManager behaviour to the list of supervised children on all the nodes managed by Cloister

Specs

pop(name :: any()) :: :ok

Removes the scheduled job from the schedule by id.

For the implementation that survives restarts use pop!/1.

Specs

pop!(name :: any()) :: :ok

Removes the scheduled job from the schedule by id and updated the configuration.

For the implementation that removes jobs temporarily, use pop!/1.

Link to this function

push(name, runner, schedule)

View Source

Specs

push(name :: binary(), runner :: runner(), schedule :: schedule()) :: :ok

Creates and temporarily pushes the job to the list of currently scheduled jobs.

For the implementation that survives restarts use push!/3.

Link to this function

push!(name, runner, schedule)

View Source

Specs

push!(name :: binary(), runner :: runner(), schedule :: schedule()) :: :ok

Creates and pushes the job to the list of currently scheduled jobs, updates the permanent list of scheduled jobs.

For the implementation that temporarily pushes a job, use push/3.

Dynamically adds a supervised worker implementing Tarearbol.DynamicManager behaviour to the list of supervised children

Starts the DynamicSupervisor and its helpers to manage dynamic children