johanna v0.2.5 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)

Called when an application is started

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>
start(type, args)

Called when an application is started.

This function is called when an application is started using Application.start/2 (and functions on top of that, such as Application.ensure_started/2). This function should start the top-level process of the application (which should be the top supervisor of the application’s supervision tree if the application follows the OTP design principles around supervision).

start_type defines how the application is started:

  • :normal - used if the startup is a normal startup or if the application is distributed and is started on the current node because of a failover from another node and the application specification key :start_phases is :undefined.
  • {:takeover, node} - used if the application is distributed and is started on the current node because of a failover on the node node.
  • {:failover, node} - used if the application is distributed and is started on the current node because of a failover on node node, and the application specification key :start_phases is not :undefined.

start_args are the arguments passed to the application in the :mod specification key (e.g., mod: {MyApp, [:my_args]}).

This function should either return {:ok, pid} or {:ok, pid, state} if startup is successful. pid should be the PID of the top supervisor. state can be an arbitrary term, and if omitted will default to []; if the application is later stopped, state is passed to the stop/1 callback (see the documentation for the c:stop/1 callback for more information).

use Application provides no default implementation for the start/2 callback.

Callback implementation for Application.start/2.

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
vomit()