blib/lib/Bot/BasicBot/Pluggable/Module/Crontab.pm

filter: Statementstmt 35.4% 22 / 62 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 60.0% 6 / 10 Podpod 100% 3 / 3 totaltotal 26.2% 31 / 118 CC 19 SCAR 42.9
CC
19
Cov
22%
_load_cron
132.0
tick
72.0
init
6.0
CRAP 73.1
↑ Summary
1 package Bot::BasicBot::Pluggable::Module::Crontab;
2  
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION = '1.00';
7
8 #----------------------------------------------------------------------------
9  
10 #############################################################################
11 #Library Modules #
12 #############################################################################
13  
14 4 ▶use base qw(Bot::BasicBot::Pluggable::Module);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Time::Crontab;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 #############################################################################
21 #Variables
22 #############################################################################
23  
24 4 my @crontab;
25 4 my $load_time = 0;
26  
27 #----------------------------------------------------------------------------
28  
29 #############################################################################
30 # Public Methods #
31 #############################################################################
32  
33 sub init {
34 0 ▶ my $self = shift;
Subroutine init: not called CC=2 SCAR=17.9
Pod documented
35  
36 0 my $file = $self->store->get( 'crontab', 'file' );
37 0 ▶ unless($file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 0 0
38 0 $file = $0;
39 0 $file =~ s/\.pl$/.cron/;
40 }
41  
42 0 $self->store->set( 'crontab', 'file', $file );
43 }
44
45 sub help {
46 0 ▶ return "Posts messages to specific channels based on a crontab.";
Subroutine help: not called CC=1 SCAR=6.9
Pod documented
47 }
48
49 sub tick {
50 0 ▶ my $self = shift;
Subroutine tick: not called CC=8 SCAR=42.8
Pod documented
51  
52 0 $self->_load_cron();
53 0 my $wkno = DateTime->now->week_number;
54  
55 0 for my $cron (@crontab) {
56 0 ▶ next unless($cron->{tab}->match(time));
Branch: 1 branch
BranchTrueFalseTotal
unless $cron->{"tab"}->match(time) 0 0 0
57 next unless(
58 $cron->{weekno} eq '*'
59 || ( $cron->{modulus} && $cron->{result} == ($wkno % $cron->{modulus}) )
60 || ( $cron->{weekno} =~ /^\d+$/ && $wkno == $cron->{weekno} )
61 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
unless $cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"} or $cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"} 0 0 0
Condition0%0/12
$cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"} or $cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"}
l!l&&r!l&&!r
000
$cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"}
l!l&&r!l&&!r
000
$cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"}
!ll&&!rl&&r
000
$cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"}
!ll&&!rl&&r
000
MC/DC0%0/5
$cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"} or $cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"}
$cron->{"weekno"} eq "*" $cron->{"modulus"} $cron->{"result"} == $wkno % $cron->{"modulus"} $cron->{"weekno"} =~ /^\d+$/ $wkno == $cron->{"weekno"}
Truth table0%0/8
A or B and C or D and E
A= $cron->{"weekno"} eq "*"B= $cron->{"modulus"}C= $cron->{"result"} == $wkno % $cron->{"modulus"}D= $cron->{"weekno"} =~ /^\d+$/E= $wkno == $cron->{"weekno"}
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
62  
63 $self->say(
64 channel => $cron->{channel},
65 body => $cron->{message}
66 0 );
67 }
68  
69 0 return 60 - DateTime->now->second; # ensure we are running each minute
70 }
71  
72 #############################################################################
73 # Private Methods #
74 #############################################################################
75  
76 sub _load_cron {
77 0 ▶ my $self = shift;
Subroutine _load_cron: not called CC=11 SCAR=48.8
78  
79 0 ▶ my $fn = $self->store->get( 'crontab', 'file' ) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $fn = $self->store->get("crontab", "file") or return 0 0 0 0
80 0 ▶ return 0 unless(-r $fn); # file must be readable
Branch: 1 branch
BranchTrueFalseTotal
unless -r $fn 0 0 0
81  
82 0 my $mod = (stat($fn))[9];
83 0 ▶ return 1 if($mod <= $load_time); # don't reload if not modified
Branch: 1 branch
BranchTrueFalseTotal
if $mod <= $load_time 0 0 0
84  
85 0 @crontab = ();
86  
87 0 ▶ my $fh = IO::File->new($fn,'r') or die "Cannot load file [$fn]: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($fn, "r") or die "Cannot load file [$fn]: $!\n" 0 0 0
88 0 ▶ while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
89 0 s/\s+$//;
90 0 my $line = $_;
91 0 ▶ next unless($line);
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 0 0
92  
93 0 ▶ next if($line =~ /^#/); # ignore comment lines
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 0 0 0
94 0 ▶ next if($line =~ /^$/); # ignore blank lines
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^$/ 0 0 0
95  
96 0 my @fields = split(/ /,$line,8);
97 0 my $crontab = join(' ',(@fields)[0..4]);
98 0 my $tab;
99 0 eval { $tab = Time::Crontab->new($crontab) };
100 0 ▶ next if($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
101  
102 0 my ($modulus,$result);
103 0 ▶ ($modulus,$result) = split(/\//,$fields[5],2) if($fields[5] =~ m!^\d+/\d+!);
Branch: 1 branch
BranchTrueFalseTotal
if $fields[5] =~ m[^\d+/\d+] 0 0 0
104  
105 0 push @crontab, {
106 tab => $tab,
107 weekno => $fields[5],
108 modulus => $modulus,
109 result => $result,
110 channel => $fields[6],
111 message => $fields[7]
112 };
113  
114 0 print "added $crontab $fields[5] = $fields[6] - $fields[7]\n";
115 }
116  
117 0 $fh->close;
118 0 $load_time = $mod;
119 }
120
121
122 4 1;
123
124 __END__
125
126 #----------------------------------------------------------------------------
127  
128 =head1 NAME
129
130 Bot::BasicBot::Pluggable::Module::Crontab - Provides a crontab-like message service for IRC channels
131
132 =head1 DESCRIPTION
133  
134 This module does not respond to user messages, public or private. It is purely
135 for posting messages to notminated channels as a specifed time.
136  
137 A crontab like file is used to load instruction sets, which are then acted on
138 at the designated time.
139  
140 Examples are:
141  
142 +-- minute
143 | +-- hour
144 | | +-- day of the month
145 | | | +-- month (1= January, ...)
146 | | | | +-- day of the week (0 = sunday ....)
147 | | | | | +-- week of the year (2/0 = even weeks, 2/1 = odd weeks)
148 v v v v v v
149 * * * * * * #dev Minute Check!
150 0 * * * * * #dev Hour Check!
151 */10 * * * * * #dev 10 minute Check!
152 0 9 * * 1 2/0 #dev Review every even week
153 0 9 * * 1 3/1 #dev Review every third Week
154  
155 As per normal crontabs, the first 5 fields allow for ranges, steps as well as strict values.
156 The 6th field is the week of the year field. In working with Scrum teams running 2 week sprints,
157 knowing when it was an odd week or even week, meant we knew whether we had a regular stand-up
158 or sprint planning meeting.
159  
160 By default this field can be attributed to every week using the traditional '*' symbol. However,
161 to determine when to run, a two part value is used, separated by a '/' symbol. The first part
162 designates the modulus value, and the second part the result it must match. For example, to
163 trigger fornightly on week 1, 3, 5, etc, this would be '2/1'. To trigger every second week of a
164 3 week sprint, this would be '3/1'.
165  
166 The 7th field determines the channel to post to. Note that the bot cannot post to all channels.
167 However, may be a feature added in a future release.
168  
169 The final free form field is the message. The complete line will be sent, with any line
170 continuation markers ignored. Line continuation markers may be a feature in the future.
171  
172 =head1 SYNOPSIS
173  
174 my $bot = Bot::BasicBot::Pluggable->new(
175 ... # various settings
176 };
177  
178 $bot->store->set( 'crontab', 'file', '/path/to/mycrontab.file' },
179 $bot->load('Crontab');
180  
181 =head1 METHODS
182
183 =over 4
184
185 =item tick()
186
187 Loads the crontab file, if not previously done so, and checks all entries to see whether the
188 timing stated matches the current time. If so the message is sent to the appropriate channel,
189 otherwise the entry is ignored. The process repeats every 60 seconds.
190  
191 Note that a change to the crontab file, will force a reload of the file on the next invocation.
192 As such, note that there may be a delay of up to 2 minutes before you see the next updated
193 entry actioned.
194  
195 =back
196
197 =head1 VARS
198
199 =over 4
200
201 =item crontab
202
203 Path to the crontab file.
204
205 The crontab file is assumed to be either based on the calling script, or a designated crontab
206 file. If based on the calling script, if your script was mybot.pl, the crontab file would
207 default to mybot.cron.
208  
209 If you wish to designate another filename or path, you may do this via the variable storage
210 when the bot is initiated. For example:
211  
212 my $bot = Bot::BasicBot::Pluggable->new(
213 ... # various settings
214 };
215  
216 $bot->store->set( 'crontab', 'file', '/path/to/mycrontab.file' },
217
218 =back
219
220 =head1 AUTHOR
221  
222 Barbie, <barbie@cpan.org>
223 for Miss Barbell Productions <http://www.missbarbell.co.uk>.
224  
225 =head1 COPYRIGHT AND LICENSE
226  
227 Copyright (C) 2015-2019 Barbie for Miss Barbell Productions
228  
229 This distribution is free software; you can redistribute it and/or
230 modify it under the Artistic License v2.
231  
232 =cut
↑ Summary