mod_cron
Provides periodic tick notifications and module-defined cron job scheduling.
This module both sends periodic tick notifications (which can be observed by modules) and reads each module’s
-mod_cron_jobs attribute to register scheduled jobs at specific intervals.
tick
For periodic tasks the system has various periodic tick events. They are named after their interval s for seconds, m for minutes, and h for hours.
tick_1s
tick_1m
tick_10m
tick_15m
tick_30m
tick_1h
tick_2h
tick_3h
tick_4h
tick_6h
tick_12h
tick_24h
Example
Check something every hour.
-include_lib("kernel/include/logger.hrl").
observe_tick_1h(tick_1h, Context) ->
?LOG_INFO("And another hour has passed..."),
do_something(Context).
The return value is ignored.
The tick observers are called one by one in a separate process. So a slow handler can delay the other handlers.
-mod_cron_jobs Module Attribute
Modules can define periodic jobs using the module attribute -mod_cron_jobs.
mod_cron reads this attribute from active modules and registers each job with erlcron.
Each entry is one of:
{RunWhen, {Module, Function, Args}}{RunWhen, {Module, Function, Args}, JobOpts}
Where:
RunWhenis anerlcronschedule tuple (daily/weekly/every/etc.).{Module, Function, Args}is the MFA to call.JobOptsare optionalerlcronjob options.
Important: mod_cron appends the site Context as the last argument when calling the MFA, so the target function must
accept one extra argument.
Example
-mod_depends([cron]).
-mod_cron_jobs([
{{weekly, mon, {10, 0, 0}}, {?MODULE, send_weekly_reminders, []}}
]).
send_weekly_reminders(Context) ->
Events = get_this_weeks_events(Context),
send_email_reminders(Events, Context).
This schedules send_weekly_reminders/1 every Monday at 10:00 UTC.
Scheduling syntax is provided by erlcron, see:
https://hexdocs.pm/erlcron/readme.html
Accepted Events
This module handles the following notifier callbacks:
observe_tick_1h: Run periodic hourly cron checks and enqueue due jobs.