johanna v0.1.4 Johanna

Simple wrapper for erlcron, the library providing testable cron like functionality for Erlang systems, with the ability to arbitrarily set the time and place along with fastforwarding through tests.

Examples (gracefully copy-pasted from original README)

{{once, {3, 30, pm}},
    {io, fwrite, ["Hello, world!~n"]}}

{{once, {12, 23, 32}},
    {io, fwrite, ["Hello, world!~n"]}}

{{once, 3600},
    {io, fwrite, ["Hello, world!~n"]}}

{{daily, {every, {23, sec}, {between, {3, pm}, {3, 30, pm}}}},
    {io, fwrite, ["Hello, world!~n"]}}

{{daily, {3, 30, pm}},
    fun() -> io:fwrite("It's three thirty~n") end}

{{daily, [{1, 10, am}, {1, 07, 30, am}]},
    {io, fwrite, ["Bing~n"]}}

{{weekly, thu, {2, am}},
    {io, fwrite, ["It's 2 Thursday morning~n"]}}

{{weekly, wed, {2, am}},
    {fun() -> io:fwrite("It's 2 Wednesday morning~n") end}

{{weekly, fri, {2, am}},
    {io, fwrite, ["It's 2 Friday morning~n"]}}

{{monthly, 1, {2, am}},
    {io, fwrite, ["First of the month!~n"]}}

{{monthly, 4, {2, am}},
    {io, fwrite, ["Fourth of the month!~n"]}}

Summary

Functions

Runs the given function recurrently at the time given

Cancels the job, previously started with at/once

Runs the given function recurrently

Returns the DateTime instance, currently set for erlcron

Sets the DateTime instance for erlcron. Useful in Timecop-like scenarios

Sets the DateTime instance for erlcron on many nodes

Runs the given function every N units (unit is :hr, :min or :sec)

Runs the given function once

Replaces the job identified by reference (by cancelling the old one and placing the new one with the same reference)

Checks whether the job spec is valid

Functions

at(time, fun)
at(:erlcron.cron_time | :erlcron.seconds | Time.t, :erlcron.callable) :: :erlcron.job_ref

Runs the given function recurrently at the time given.

Examples

▶ Johanna.at({3, :pm}, fn -> IO.puts("¡Yay!") end)
▷ ... at 15:00, daily:
▷ "¡Yay!"

▶ Johanna.at({2, 45, :pm}, {IO, :puts, ["¡Yay!"]})
▷ ... at 14:45, daily:
▷ "¡Yay!"
cancel!(job_ref)
cancel!(:erlcron.job_ref) :: :ok | :undefined

Cancels the job, previously started with at/once.

cron(job)
cron(:erlcron.job) :: :erlcron.job_ref

Runs the given function recurrently.

Examples

▶ Johanna.cron({10, :am}, fn -> IO.puts("¡Yay!") end})
▷ ... at 10AM daily
▷ "¡Yay!"
datetime()
datetime() :: DateTime.t

Returns the DateTime instance, currently set for erlcron.

Examples

▶ Johanna.datetime()
▷ %DateTime{calendar: Calendar.ISO, day: 30, hour: 14, microsecond: {0, 0},
▷     minute: 2, month: 3, second: 43, std_offset: 0, time_zone: "Etc/UTC",
▷     utc_offset: 0, year: 2017, zone_abbr: "UTC"}
datetime!(time)
datetime!(DateTime.t | NaiveDateTime.t) :: :ok

Sets the DateTime instance for erlcron. Useful in Timecop-like scenarios.

datetimes!(time)
datetimes!(DateTime.t | NaiveDateTime.t) :: :ok

Sets the DateTime instance for erlcron on many nodes.

datetimes!(nodes, time)
datetimes!([node], DateTime.t | NaiveDateTime.t) :: :ok
every(duration, constraint \\ nil, fun)
every(Integer.t | :erlcron.duration, :erlcron.constraint | nil, :erlcron.callable) :: :erlcron.job_ref

Runs the given function every N units (unit is :hr, :min or :sec).

Examples

▶ Johanna.every(10, fn -> IO.puts("¡Yay!") end)
▷ ... every 10 secs
▷ "¡Yay!"

▶ Johanna.every({10, :sec}, fn -> IO.puts("¡Yay!") end)
▷ ... every 10 secs
▷ "¡Yay!"

▶ Johanna.every({1, :min}, {IO, :puts, ["¡Yay!"]})
▷ ... every 1 min
▷ "¡Yay!"

▶ Johanna.every(10, {:between, {1, :pm}, {4, :pm}}, fn -> IO.puts("¡Siesta!") end)
▷ ... every 10 secs from 1PM till 4PM
▷ "¡Siesta!"
once(time, fun)
once(:erlcron.cron_time | :erlcron.seconds | DateTime.t, :erlcron.callable) :: :erlcron.job_ref

Runs the given function once.

Examples

▶ Johanna.once(10, fn -> IO.puts("¡Yay!") end)
▷ ... 10 sec pause
▷ "¡Yay!"
replace(ref, job)
replace(:erlcron.job_ref, :erlcron.job) :: :erlcron.job_ref

Replaces the job identified by reference (by cancelling the old one and placing the new one with the same reference).

Examples

▶ ref = Johanna.at {7, :pm}, {IO, :puts, ["Yay"]}
▷ #Reference<0.0.5.28>
▶ Johanna.replace ref, {{:daily, {7, :pm}}, {IO, :puts, ["Yay"]}}
▷ #Reference<0.0.5.28>
valid?(spec)
valid?(:erlcron.run_when) :: :valid | :invalid

Checks whether the job spec is valid.

Examples

▶ Johanna.valid?({:once, {3, :pm}})
▷ true

▶ Johanna.valid?({:daily, {3, :pm}})
▷ true

▶ Johanna.valid?({3, :pm})
▷ false