Kiq v0.6.0 Kiq.Periodic.Crontab View Source

Generate and evaluate the structs used to evaluate periodic jobs.

The Crontab module provides parsing and evaluation for standard cron expressions. Expressions are composed of rules specifying the minutes, hours, days, months and weekdays. Rules for each field are comprised of literal values, wildcards, step values or ranges:

  • * - Wildcard, matches any value (0, 1, 2, …)
  • 0 — Literal, matches only itself (only 0)
  • */15 — Step, matches any value that is a multiple (0, 15, 30, 45)
  • 0-5 — Range, matches any value within the range (0, 1, 2, 3, 4, 5)

Each part may have multiple rules, where rules are separated by a comma. The allowed values for each field are as follows:

  • minute - 0-59
  • hour - 0-23
  • days - 1-31
  • month - 1-12 (or aliases, JAN, FEB, MAR, etc.)
  • weekdays - 0-6 (or aliases, SUN, MON, TUE, etc.)

For more in depth information see the man documentation for cron and crontab in your system. Alternatively you can experiment with various expressions online at Crontab Guru.

Examples

# The first minute of every hour
Crontab.parse!("0 * * * *")

# Every fifteen minutes during standard business hours
Crontab.parse!("*/15 9-17 * * *")

# Once a day at midnight during december
Crontab.parse!("0 0 * DEC *")

# Once an hour during both rush hours on Friday the 13th
Crontab.parse!("0 7-9,4-6 13 * FRI")

Link to this section Summary

Functions

Evaluate whether a crontab matches a datetime. The current datetime in UTC is used as the default

Parse a crontab expression string into a Crontab struct

Link to this section Types

Link to this type expression() View Source
expression() :: [:*] | [non_neg_integer()]
Link to this type t() View Source
t() :: %Kiq.Periodic.Crontab{
  days: expression(),
  hours: expression(),
  minutes: expression(),
  months: expression(),
  weekdays: expression()
}

Link to this section Functions

Link to this function now?(crontab, datetime \\ DateTime.utc_now()) View Source
now?(crontab :: t(), datetime :: DateTime.t()) :: boolean()

Evaluate whether a crontab matches a datetime. The current datetime in UTC is used as the default.

Examples

iex> Kiq.Periodic.Crontab.now?(%Crontab{})
true

iex> crontab = Crontab.parse!("* * * * *")
...> Kiq.Periodic.Crontab.now?(crontab)
true

iex> crontab = Crontab.parse!("59 23 1 1 6")
...> Kiq.Periodic.Crontab.now?(crontab)
false
Link to this function parse!(input) View Source
parse!(input :: binary()) :: t()

Parse a crontab expression string into a Crontab struct.

Examples

iex> Kiq.Periodic.Crontab.parse!("0 6,12,18 * * *")
%Crontab{minutes: [0], hours: [6, 12, 18]}

iex> Kiq.Periodic.Crontab.parse!("0-2,4-6 */12 * * *")
%Crontab{minutes: [0, 1, 2, 4, 5, 6], hours: [0, 12]}

iex> Kiq.Periodic.Crontab.parse!("* * 20,21 SEP,NOV *")
%Crontab{days: [20, 21], months: [9, 11]}

iex> Kiq.Periodic.Crontab.parse!("0 12 * * SUN")
%Crontab{minutes: [0], hours: [12], weekdays: [0]}