File Coverage

blib/lib/EV/Cron.pm
Criterion Covered Total %
statement 38 39 97.4
branch 1 2 50.0
condition n/a
subroutine 13 14 92.8
pod 2 2 100.0
total 54 57 94.7


line stmt bran cond sub pod time code
1             #
2             # This file is part of EV-Cron
3             #
4             # This software is copyright (c) 2012 by Loïc TROCHET.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9              
10 2     2   68705 use strict;
  2         4  
  2         67  
11 2     2   9 use warnings;
  2         5  
  2         100  
12              
13             package EV::Cron;
14             {
15             $EV::Cron::VERSION = '0.123600';
16             }
17             # ABSTRACT: Add crontab watcher into EV
18              
19 2     2   10 use feature qw(state);
  2         3  
  2         209  
20 2     2   803 use EV;
  2         2973  
  2         273  
21 2     2   6934 use DateTime;
  2         416237  
  2         83  
22 2     2   3157 use DateTime::Event::Cron;
  2         213879  
  2         87  
23 2     2   27 use Params::Validate qw(validate SCALAR CODEREF);
  2         4  
  2         151  
24              
25             BEGIN
26             {
27 2     2   10 no strict 'refs';
  2         4  
  2         151  
28 2     2   6 foreach my $function (qw(cron cron_ns)) { *{ "EV::$function" } = *{ "EV::Cron::$function" }; }
  4         7  
  4         255  
  4         18  
29             }
30              
31             my $local_TZ = DateTime::TimeZone::Local->TimeZone();
32              
33             sub _add_watcher
34             {
35 1     1   44 my %params = validate
36             (
37             @_
38             , {
39             start => { type => SCALAR }
40             , cron => { type => SCALAR }
41             , cb => { type => CODEREF }
42             }
43             );
44              
45 1 50       10 my $ev_call = $params{start}
46             ? 'EV::periodic'
47             : 'EV::periodic_ns';
48            
49 2     2   11 no strict 'refs';
  2         4  
  2         482  
50             return &$ev_call
51             (
52             0
53             , 0
54             , sub #-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55             {
56 2     2   39320682 my ($watcher, $now) = @_;
57 2         19 state $dt_event = DateTime::Event::Cron->new($params{cron});
58 2         1506 return $dt_event->next(DateTime->from_epoch(epoch => $now, time_zone => $local_TZ))->epoch;
59             } #-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60 1         33 , $params{cb}
61             );
62             }
63              
64              
65             sub cron
66             {
67 1     1 1 673 return _add_watcher(start => 1, cron => $_[0], cb => $_[1]);
68             }
69              
70              
71             sub cron_ns
72             {
73 0     0 1   return _add_watcher(start => 0, cron => $_[0], cb => $_[1]);
74             }
75              
76             1;
77              
78             __END__