Skip to content

Commit

Permalink
Merge pull request #12 from rawleyfowler/master
Browse files Browse the repository at this point in the history
Pass $app through to code ref
  • Loading branch information
dmanto authored Aug 12, 2024
2 parents 706d652 + 99de520 commit d94ef2c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
10 changes: 5 additions & 5 deletions lib/Mojolicious/Plugin/Cron.pm
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ sub register {

# special case, plugin => 'mm hh dd ...' => sub {}
$self->_cron($app->moniker,
{crontab => (keys %$cronhashes)[0], code => (values %$cronhashes)[0]});
{crontab => (keys %$cronhashes)[0], code => (values %$cronhashes)[0]}, $app);
}
else {
$self->_cron($_, $cronhashes->{$_}) for keys %$cronhashes;
$self->_cron($_, $cronhashes->{$_}, $app) for keys %$cronhashes;
}
});
}

sub _cron {
my ($self, $sckey, $cronhash) = @_;
my ($self, $sckey, $cronhash, $app) = @_;
my $code = delete $cronhash->{code};
my $all_proc = delete $cronhash->{all_proc} // '';
my $test_key
Expand Down Expand Up @@ -82,7 +82,7 @@ sub _cron {
undef $dat;
undef $sem; # unlock
}
$code->($time) if $fire;
$code->($time, $app) if $fire;
$task->();
}
);
Expand Down Expand Up @@ -234,7 +234,7 @@ of simultaneous scheduled tasks to just one on a multi-host environment.
# Execute some job every 5 minutes, only on one of the existing hosts
plugin Cron => ( '*/5 * * * *' => sub {
my $target_epoch = shift;
my ($target_epoch, $app) = @_;
my $last_epoch = some_kind_of_atomic_swap_function(
key => "some id key for this crontab",
value => $target_epoch
Expand Down
5 changes: 4 additions & 1 deletion t/one_crontab.t
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ $ENV{MOJO_MODE} = 'test';
my %local_tstamps;

my $last_epoch;
my $app;

plugin Cron => (
'*/10 15 * * *' => sub {
$last_epoch = shift;
($last_epoch, $app) = @_;
$local_tstamps{fmt_time(localtime)}++;
Mojo::IOLoop->stop;
}
Expand All @@ -41,6 +42,8 @@ is \%local_tstamps,
"$lday 15:50" => 1,
}, # no more because hour is always 15 local
'exact tstamps';
ok $app, 'is app passed?';
is ref($app), 'Mojolicious::Lite', 'is app correct type?';

# check that new $epock parameter is available on callback
ok defined $last_epoch && $last_epoch =~ /^\d{10}$/, "got epoch on callback";
Expand Down

0 comments on commit d94ef2c

Please sign in to comment.