tarearbol v0.99.4 Tarearbol.Crontab View Source

Helper functions to work with cron syntax.

Link to this section Summary

Types

t()

Internal representation of the record in cron file

Functions

Produces the single formula out of cron record. Might be useful for some external check that requires the single validation call.

Returns the next DateTime the respective cron record points to with a precision given as the third argument (default: :second.)

Returns the list of all the events after dt (default: DateTime.utc_now/0.)

Returns the stream of all the events after dt (default: DateTime.utc_now/0.)

Parses the cron string into human-readable representation.

Parses the cron string into Tarearbol.Crontab.t() struct.

Converts the Time instance into daily-execution cron string

Link to this section Types

Link to this type

t()

View Source
t() :: %Tarearbol.Crontab{
  day: term(),
  day_of_week: term(),
  hour: term(),
  minute: term(),
  month: term()
}

Internal representation of the record in cron file

Link to this section Functions

Link to this function

formula(ct)

View Source
formula(ct :: binary() | t()) :: :error | binary()

Produces the single formula out of cron record. Might be useful for some external check that requires the single validation call.

Examples

iex> Tarearbol.Crontab.formula("42 3 28 08 *").formula
"(day == 28) && (rem(day_of_week, 1) == 0) && (hour == 3) && (minute == 42) && (month == 8)"

iex> Tarearbol.Crontab.formula("423 * * * *")
{:error, [minute: {:could_not_parse_field, ["423"]}]}
Link to this function

next(dt \\ nil, input, opts \\ [])

View Source
next(dt :: nil | DateTime.t(), input :: binary(), opts :: keyword()) ::
  DateTime.t()

Returns the next DateTime the respective cron record points to with a precision given as the third argument (default: :second.)

If the first parameter is not given, it assumes the next after now.

Examples

iex> dt = DateTime.from_unix!(1567091960)
~U[2019-08-29 15:19:20Z]
iex> Tarearbol.Crontab.next(dt, "42 3 28 08 *")
[
  origin: ~U[2019-08-29 15:19:20Z],
  next: ~U[2020-08-28 03:42:00Z],
  second: 31494160
]

where origin contains the timestamp to lookup the next for, next is the DateTime instance of the next event and second is the {precision, difference_in_that_precision}.

Link to this function

next_as_list(dt \\ nil, input, opts \\ [])

View Source
next_as_list(dt :: nil | DateTime.t(), input :: binary(), opts :: keyword()) ::
  keyword()

Returns the list of all the events after dt (default: DateTime.utc_now/0.)

This function calculates the outcome greedily and, while it might be slightly faster than Tarearbol.Crontab.next_as_stream/3, it should not be used for frequently recurring cron records (like "* * * * *".)

Link to this function

next_as_stream(dt \\ nil, input, opts \\ [])

View Source
next_as_stream(dt :: nil | DateTime.t(), input :: binary(), opts :: keyword()) ::
  Enumerable.t()

Returns the stream of all the events after dt (default: DateTime.utc_now/0.)

This function calculates the outcome lazily, returning a stream.

See Tarearbol.Crontab.next_as_list/3 for greedy evaluation.

Link to this function

parse(input)

View Source
parse(input :: binary()) :: t()

Parses the cron string into human-readable representation.

This function is exported for debugging purposes only, normally one would call prepare/1 instead.

Input format: "minute hour day/month month day/week".

Examples:

iex> Tarearbol.Crontab.parse "10-30/5 */4 1 */1 6,7"
%Tarearbol.Crontab{
  day: "(day == 1)",
  day_of_week: "(day_of_week == 6 || day_of_week == 7)",
  hour: "(rem(hour, 4) == 0)",
  minute: "(rem(minute, 5) == 0 && minute >= 10 && minute <= 30)",
  month: "(rem(month, 1) == 0)"
}

In case of malformed input:

iex> Tarearbol.Crontab.parse "10-30/5 */4 1 */1 6d,7"
%Tarearbol.Crontab{
  day: "(day == 1)",
  day_of_week: {:error, {:could_not_parse_integer, "6d"}},
  hour: "(rem(hour, 4) == 0)",
  minute: "(rem(minute, 5) == 0 && minute >= 10 && minute <= 30)",
  month: "(rem(month, 1) == 0)"
}
Link to this function

prepare(input)

View Source
prepare(input :: binary() | t()) :: t()

Parses the cron string into Tarearbol.Crontab.t() struct.

Input format: "minute hour day/month month day/week".

Link to this function

to_cron(time)

View Source
to_cron(dt :: Time.t()) :: binary()

Converts the Time instance into daily-execution cron string