ecrn_agent (erlcron v1.3.8)

Per-job gen_server that manages the lifecycle of a single scheduled job.

When a job is added to the scheduler, one ecrn_agent process is started for it. The process calculates the duration until the job's next execution time and uses gen_server timeouts to sleep for exactly that long. To avoid accumulated drift from DST transitions or other system-clock changes, the sleep duration is capped at 30 minutes; the next execution time is then recalculated from scratch at each wake-up.

The module also exposes the schedule normalisation and next-run calculation logic through the public functions normalize/2, and until_next_time/2, which are used by the test suite.

Summary

Functions

Return the time (millisecond epoch) at which the job is next scheduled to run

Run the job immediately, without affecting the next scheduled run time

start_link/3 deprecated

Start a job agent linked to the calling process

Calculate the duration in milliseconds until Sched next fires, relative to NowEpochTime.

Validate a erlcron:schedule/0 spec without scheduling it.

Types

job()

-type job() :: #job{schedule :: term(), task :: term(), lambda :: term()}.

normalized_period()

-type normalized_period() :: [{integer(), integer(), integer()}].

normalized_sched()

-type normalized_sched() ::
          {once, {relative | absolute, integer()}} |
          {daily, normalized_period()} |
          {weekly, [integer()], normalized_period()} |
          {monthly, integer(), normalized_period()}.

Functions

cancel(Pid)

-spec cancel(pid()) -> ok.

code_change(OldVsn, State, Extra)

get_datetime(Pid)

-spec get_datetime(pid()) -> calendar:datetime().

get_datetime/2

-spec get_datetime(pid(), local | universal) -> calendar:datetime().

handle_call/3

handle_cast/2

handle_info/2

info(Pid)

-spec info(pid()) -> #job{schedule :: term(), task :: term(), lambda :: term()}.

init/1

next_run(Pid)

-spec next_run(pid()) -> erlang:timestamp().

Return the time (millisecond epoch) at which the job is next scheduled to run

normalize/2

recalculate(Pid)

-spec recalculate(pid()) -> ok.

run(Pid)

-spec run(pid()) -> ok.

Run the job immediately, without affecting the next scheduled run time

set_datetime(Pid, DateTime, Actual)

-spec set_datetime(pid(), calendar:datetime(), erlcron:milliseconds()) -> ok.

set_datetime(Pid, DateTime, Actual, UTC)

-spec set_datetime(pid(), calendar:datetime(), erlcron:milliseconds(), UTC :: local | universal) ->
                      ok | {error, term()}.

start_link/3

This function is deprecated. Use start_link/4 instead..
-spec start_link(erlcron:job_ref(), erlcron:job(), erlcron:job_opts()) ->
                    ignore | {error, Reason :: term()} | {ok, pid()}.

start_link/4

-spec start_link(erlcron:job_ref(),
                 erlcron:job(),
                 undefined | fun(() -> any()) | fun((erlcron:job_ref(), calendar:datetime()) -> any()),
                 erlcron:job_opts()) ->
                    ignore | {error, Reason :: term()} | {ok, pid()}.

Start a job agent linked to the calling process

terminate(Reason, State)

until_next_time(NowEpochTime, Sched)

-spec until_next_time(erlcron:milliseconds(), erlcron:schedule()) -> erlcron:milliseconds().

Calculate the duration in milliseconds until Sched next fires, relative to NowEpochTime.

This is the stateless public entry point used by the test suite.

validate/1

-spec validate(erlcron:schedule() | binary() | string()) -> ok | {error, term()}.

Validate a erlcron:schedule/0 spec without scheduling it.

Returns ok when the spec is syntactically and semantically valid, or {error, Reason} otherwise.