Tarearbol.Scheduler (tarearbol v1.4.0) 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 lifetimerunner
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
Functions
Performs a GenServer.cast/2
to the worker specified by id
.
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.
Restarts the DynamicManager
to the clean state
Starts the DynamicSupervisor
and its helpers to manage dynamic children
Performs a GenServer.call/3
to the worker specified by id
.
Link to this section Types
Specs
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(Tarearbol.DynamicManager.id()) => %Tarearbol.DynamicManager.Child{ busy?: term(), opts: term(), pid: term(), value: term() } }
Specs
asynch_call(id :: nil | Tarearbol.DynamicManager.id(), message :: any()) :: :ok | :error
Performs a GenServer.cast/2
to the worker specified by id
.
Tarearbol.DynamicManager.cast/2
callback should be implemented for this to work.
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
If distributed: true
parameter was given to use Tarearbol.DynamicManager
,
deletes the worker from all the nodes managed by Cloister
. :cloister
dependency
must be added to a project to use this feature.
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
.
Use distributed: true
parameter in call to use Tarearbol.DynamicManager
and regular del/1
instead.
Dynamically adds a supervised worker implementing Tarearbol.DynamicManager
behaviour to the list of supervised children on all the nodes managed by Cloister
.
Use distributed: true
parameter in call to use Tarearbol.DynamicManager
and regular put/2
instead.
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
.
Specs
Creates and temporarily pushes the job to the list of currently scheduled jobs.
For the implementation that survives restarts use push!/3
.
Specs
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.
If distributed: true
parameter was given to use Tarearbol.DynamicManager
,
puts the worker into all the nodes managed by Cloister
. :cloister
dependency
must be added to a project to use this feature.
Restarts the DynamicManager
to the clean state
Starts the DynamicSupervisor
and its helpers to manage dynamic children
Specs
synch_call(id :: nil | Tarearbol.DynamicManager.id(), message :: any()) :: {:ok, any()} | :error
Performs a GenServer.call/3
to the worker specified by id
.
Tarearbol.DynamicManager.call/3
callback should be implemented for this to work.