| 1 | package Mojolicious::Plugin::Cron::Scheduler 0.02; | ||
| 2 | 1 | ▶ | use v5.26; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | # ABSTRACT: Mojolicious Plugin that wraps Mojolicious::Plugin::Cron for job configurability | ||
| 6 | |||
| 7 | =encoding UTF-8 | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Mojolicious::Plugin::Cron::Scheduler - easily configure L<Mojolicious::Plugin::Cron> | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | # For a full Mojo app | ||
| 16 | $self->plugin('Cron::Scheduler' => { | ||
| 17 | schdules => { | ||
| 18 | do_a_thing => [ | ||
| 19 | { | ||
| 20 | schedule => { | ||
| 21 | minute => 0, | ||
| 22 | hour => '*', | ||
| 23 | day => '*', | ||
| 24 | month => '*', | ||
| 25 | weekday => '*' | ||
| 26 | } | ||
| 27 | } | ||
| 28 | } | ||
| 29 | ], | ||
| 30 | tasks => { | ||
| 31 | do_a_thing => sub { ... } | ||
| 32 | } | ||
| 33 | }); | ||
| 34 | |||
| 35 | # or, tasks can be imported from a namespace, keeping your code well-organized | ||
| 36 | package MyApp::Cron::DoAThing; | ||
| 37 | |||
| 38 | sub register($self, $app, $args) { | ||
| 39 | $app->crontask(do_a_thing => sub { ... }) | ||
| 40 | } | ||
| 41 | |||
| 42 | package MyApp; | ||
| 43 | ... | ||
| 44 | $self->plugin('Cron::Scheduler' => { | ||
| 45 | schedules => { do_a_thing => { ... } }, | ||
| 46 | namespaces => ['MyApp::Cron'] | ||
| 47 | }); | ||
| 48 | |||
| 49 | =head1 DESCRIPTION | ||
| 50 | |||
| 51 | L<Mojolicious::Plugin::Cron> is great, but is best used when the tasks are fairly | ||
| 52 | static. This module was created to wrap its functionality and add the ability | ||
| 53 | to easily pull in an external configuration of when and how tasks should be run, | ||
| 54 | cleanly separated from the implementation of those tasks themselves. | ||
| 55 | |||
| 56 | =head1 METHODS | ||
| 57 | |||
| 58 | L<Mojolicious::Plugin::Cron::Scheduler> inherits all methods from L<Mojolicious::Plugin> and implements the following | ||
| 59 | new ones | ||
| 60 | |||
| 61 | =head2 register( ..., $parameters ) | ||
| 62 | |||
| 63 | Register plugin in L<Mojolicious> application. Accepts a HashRef of parameters | ||
| 64 | with three supported, optional keys: | ||
| 65 | |||
| 66 | =head4 schedules | ||
| 67 | |||
| 68 | Optional (though if omitted, no scheduling will be performed) | ||
| 69 | |||
| 70 | A HashRef whose keys are C<crontask> names. The values of this hash are ArrayRefs, | ||
| 71 | each item of which is a HashRef with C<schedule> and C<parameters> keys. | ||
| 72 | |||
| 73 | =head6 schedule | ||
| 74 | |||
| 75 | A HashRef whose keys are C<minute>, C<hour>, C<day>, C<month>, C<weekday>, | ||
| 76 | corresponding to the L<crontab|https://linuxhandbook.com/crontab/> columns. If | ||
| 77 | any of these keys are omitted, they are assumed to be C<*> elements. | ||
| 78 | |||
| 79 | =head6 parameters | ||
| 80 | |||
| 81 | An ArrayRef of values to be passed to the task when it is run from this schedule | ||
| 82 | |||
| 83 | =head4 tasks | ||
| 84 | |||
| 85 | Optional | ||
| 86 | HashRef[CodeRef] | ||
| 87 | |||
| 88 | A HashRef whose keys are C<crontask> names. The values of this hash are CodeRefs | ||
| 89 | - the code to be run when the scheduled task is executed. The parameters passed | ||
| 90 | to the code are only those present in the L</parameters> array. | ||
| 91 | |||
| 92 | =head4 namespaces | ||
| 93 | |||
| 94 | Optional | ||
| 95 | ArrayRef[Str] | ||
| 96 | |||
| 97 | An ArrayRef of package namespaces to load. The premise is that such packages would | ||
| 98 | call L</crontask> to register schedulable tasks for this module to schedule. Any | ||
| 99 | packages in this namespace will be loaded and registered as L<Mojolicious> | ||
| 100 | L<plugins|Mojolicious::Plugin>, whether or not they register C<crontask>s. | ||
| 101 | |||
| 102 | =head2 crontask( name => $coderef ) | ||
| 103 | |||
| 104 | Registers a coderef/anonymous subroutine by name as a schedulable task. This | ||
| 105 | registration is an alternative to passing these code blocks in at the time of | ||
| 106 | plugin loading. Often this will be called from small, specialized plugins | ||
| 107 | loadable by L<namespace|/namespaces> | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | 0 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 112 | use Mojolicious::Plugin::Cron; | ||
| 113 | |||
| 114 | use List::Util qw(min max); | ||
| 115 | use Module::Find; | ||
| 116 | use Syntax::Keyword::Try; | ||
| 117 | use Readonly; | ||
| 118 | |||
| 119 | use experimental qw(signatures); | ||
| 120 | |||
| 121 | Readonly::Scalar my $CRONTAB_WILDCARD => q{*}; | ||
| 122 | Readonly::Array my @CRONTAB_FIELDS => qw(minute hour day month weekday); | ||
| 123 | |||
| 124 | my sub hashref_to_crontab_fmt($h = undef) { | ||
| 125 | die("Invalid schedule") if (!defined($h) || ref($h) ne 'HASH'); | ||
| 126 | my %s = map {$_ => exists($h->{$_}) ? $h->{$_} : $CRONTAB_WILDCARD} @CRONTAB_FIELDS; | ||
| 127 | return join(q{ }, @s{@CRONTAB_FIELDS}); | ||
| 128 | } | ||
| 129 | |||
| 130 | my sub compile_crontab($app, $jobs, $schedules) { | ||
| 131 | my %combined; | ||
| 132 | $combined{$_}->{code} = $jobs->{$_} foreach (keys($jobs->%*)); | ||
| 133 | $combined{$_}->{schedules} = $schedules->{$_} foreach (keys($schedules->%*)); | ||
| 134 | |||
| 135 | my $cron = {}; | ||
| 136 | foreach my $name (keys(%combined)) { | ||
| 137 | my $c = $combined{$name}; | ||
| 138 | $app->log->error("Cron job '$name' skipped: no implementation loaded") and next unless (defined($c->{code})); | ||
| 139 | my $i = 1; | ||
| 140 | foreach my $instance (($c->{schedules} // [])->@*) { | ||
| 141 | try { | ||
| 142 | my $schedule = hashref_to_crontab_fmt($instance->{schedule}); | ||
| 143 | my $task_name = sprintf('%s-%d', $name, $i++); | ||
| 144 | $app->log->info("Scheduling '$task_name' at {$schedule}"); | ||
| 145 | $cron->{$task_name} = { | ||
| 146 | crontab => $schedule, | ||
| 147 | code => sub { | ||
| 148 | $app->log->info("Running $name cronjob"); | ||
| 149 | $c->{code}->($instance->{parameters}->@*); | ||
| 150 | }, | ||
| 151 | }; | ||
| 152 | } catch ($e) { | ||
| 153 | $app->log->error("Cron job '$name' skipped: $e"); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | } | ||
| 157 | return $cron; | ||
| 158 | } | ||
| 159 | |||
| 160 | sub register ($self, $app, $args) { | ||
| 161 | my @namespaces = ($args->{namespaces} // [])->@*; | ||
| 162 | # %jobs = { jobname => CODEREF, ... } | ||
| 163 | my %jobs = ($args->{tasks} // {})->%*; | ||
| 164 | # %schedules = { jobname => [ { schedule => { hour => x, minute => y, ... }, parameters: { ... } }, ... ]} | ||
| 165 | my %schedules = ($args->{schedules} // {})->%*; | ||
| 166 | |||
| 167 | # crontask helper MUST be installed... | ||
| 168 | $app->helper(crontask => sub ($self, $name, $code) {$jobs{$name} = $code;}); | ||
| 169 | # ...BEFORE loading cron plugins | ||
| 170 | foreach (map {Module::Find::findallmod($_)} @namespaces) { | ||
| 171 | $app->plugin($_); | ||
| 172 | $app->log->debug("Cron loaded plugin: '$_'"); | ||
| 173 | } | ||
| 174 | |||
| 175 | my $cron = compile_crontab($app, \%jobs, \%schedules); | ||
| 176 | return $app->plugin(Cron => $cron); | ||
| 177 | } | ||
| 178 | |||
| 179 | =head1 AUTHOR | ||
| 180 | |||
| 181 | Mark Tyrrell C<< <mark@tyrrminal.dev> >> | ||
| 182 | |||
| 183 | =head1 LICENSE | ||
| 184 | |||
| 185 | Copyright (c) 2024 Mark Tyrrell | ||
| 186 | |||
| 187 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 188 | of this software and associated documentation files (the "Software"), to deal | ||
| 189 | in the Software without restriction, including without limitation the rights | ||
| 190 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 191 | copies of the Software, and to permit persons to whom the Software is | ||
| 192 | furnished to do so, subject to the following conditions: | ||
| 193 | |||
| 194 | The above copyright notice and this permission notice shall be included in all | ||
| 195 | copies or substantial portions of the Software. | ||
| 196 | |||
| 197 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 198 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 199 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 200 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 201 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 202 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 203 | SOFTWARE. | ||
| 204 | |||
| 205 | =cut | ||
| 206 | |||
| 207 | 1; | ||
| 208 | |||
| 209 | __END__ | ||