Mojolicious::Plugin::Cron - a Cron-like helper for Mojolicious and Mojolicious::Lite projects
# Execute some job every 5 minutes, from 9 to 5 # Mojolicious::Lite plugin Cron( '*/5 9-17 * * *' => sub { # do someting non-blocking but useful }); # Mojolicious $self->plugin(Cron => '*/5 9-17 * * *' => sub { # same here });
# More than one schedule, or more options requires extended syntax
plugin Cron => ( sched1 => { base => 'utc', # not needed for local time crontab => '*/10 15 * * *', # every 10 minutes starting at minute 15, every hour code => sub { # job 1 here } }, sched2 => { crontab => '*/15 15 * * *', # every 15 minutes starting at minute 15, every hour code => sub { # job 2 here } });
Mojolicious::Plugin::Cron is a Mojolicious plugin that allows to schedule tasks directly from inside a Mojolicious application. You should not consider it as a *nix cron replacement, but as a method to make a proof of concept of a project.
When using preforked servers (as applications running with hypnotoad), some coordination is needed so jobs are not executed several times. Mojolicious::Plugin::Cron uses standard Fcntl functions for that coordination, to assure a platform-independent behavior.
When using extended syntax, you can define more than one crontab line, and have access to more options
plugin Cron => {key1 => {crontab line 1}, key2 => {crontab line 2}, ...};
Keys are the names that identify each crontab line. They are used to form the locking semaphore to avoid multiple processes starting the same job. You can use the same name in different Mojolicious applications, and this will ensure that not more that one instance of the cron job will take place at a specific scheduled time.
Each crontab line consists of a hash with the following keys:
Gives the time base used for scheduling. Either utc or local.
utc
local
Gives the crontab schedule in 5 or 6 space-separated fields.
Optional. Gives the schedule in a set of individual fields, if the crontab field is not specified.
crontab
For more information on base, crontab and other time related keys, please refer to Algorithm::Cron Contstructor Attributes.
Mandatory. Is the code that will be executed whenever the crontab rule fires. Note that this code *MUST* be non-blocking.
Mojolicious::Plugin::Cron inherits all methods from Mojolicious::Plugin and implements the following new ones.
$plugin->register(Mojolicious->new, {Cron => '* * * * *' => sub {}});
Register plugin in Mojolicious application.
To install in windows environments, you need to force-install module Test::Mock::Time, or installation tests will fail.
Daniel Mantovani, dmanto@cpan.org
dmanto@cpan.org
Copyright 2018, Daniel Mantovani.
This library is free software; you may redistribute it and/or modify it under the terms of the Artistic License version 2.0.
Mojolicious, Mojolicious::Guides, Mojolicious::Plugins, Algorithm::Cron
To install Mojolicious::Plugin::Cron, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::Cron
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::Cron
For more information on module installation, please visit the detailed CPAN module installation guide.