Webqq/Client/Cron.pm

filter: Statementstmt 32.6% 16 / 49 Branchbran 0.0% 0 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 71.4% 5 / 7 Podpod 0.0% 0 / 1 totaltotal 24.1% 21 / 87 CC 17 SCAR 52.5
CC
17
Cov
20%
add_job
272.0
__ANON__
6.0
BEGIN
1.0
CRAP 190.0
« Webqq/Client/Plugin/ShowMsg.pm ↑ Summary Webqq/Client/Plugin/IrcSync.pm »
1 package Webqq::Client::Cron;
2 1 ▶use AE;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Webqq::Client::Util qw(console_stderr console);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use POSIX qw(mktime);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Time::Seconds;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶sub add_job{
Subroutine add_job: not called CC=16 SCAR=56.1
Pod undocumented
8 0 my $self = shift;
9 #AE::now_update;
10 0 my($type,$t,$callback) = @_;
11 0 ▶ if(ref $callback ne 'CODE'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $callback ne "CODE") 0 0 0
12 0 console_stderr("Webqq::Client::Cron->add_job()设置的callback无效\n");
13 0 exit;
14 }
15 0 my($hour,$minute) = split /:/,$t;
16 0 my $time = {hour => $hour,minute => $minute,second=>0};
17 0 my $delay;
18 #my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
19 0 my @now = localtime;
20 0 my $now = mktime(@now);
21 0 my @next = @{[@now]};
22 0 for my $k (keys %$time){
23 $k eq 'year' ? ($next[5]=$time->{$k}-1900)
24 : $k eq 'month' ? ($next[4]=$time->{$k}-1)
25 : $k eq 'day' ? ($next[3]=$time->{$k})
26 : $k eq 'hour' ? ($next[2]=$time->{$k})
27 : $k eq 'minute' ? ($next[1]=$time->{$k})
28 0 ▶ : $k eq 'second' ? ($next[0]=$time->{$k})
Branch: 6 branches
BranchTrueFalseTotal
$k eq 'year' ? : 0 0 0
$k eq 'month' ? : 0 0 0
$k eq 'day' ? : 0 0 0
$k eq 'hour' ? : 0 0 0
$k eq 'minute' ? : 0 0 0
$k eq 'second' ? : 0 0 0
29 : next;
30 }
31  
32 0 my $next = mktime(@next);
33 0 $now = localtime($now);
34 0 $next = localtime($next);
35  
36 0 ▶ if($now >= $next){
Branch: 1 branch
BranchTrueFalseTotal
if ($now >= $next) 0 0 0
37 0 ▶ if( $time->{month} ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($time->{'month'}) { } 0 0 0
elsif ($time->{'day'}) { } 0 0 0
elsif ($time->{'hour'}) { } 0 0 0
elsif ($time->{'minute'}) { } 0 0 0
elsif ($time->{'second'}) { } 0 0 0
38 0 $next->add_years(1);
39 }
40 elsif( $time->{day} ) {
41 0 $next->add_months(1);
42 }
43 elsif( $time->{hour} ) {
44 0 $next += ONE_DAY;
45 }
46 elsif( $time->{minute} ) {
47 0 $next += ONE_HOUR;
48 }
49 elsif( $time->{second} ) {
50 0 $next += ONE_MINUTE;
51 }
52 }
53
54 0 ▶ console "[$type]下一次触发时间为:" . $next->strftime("%Y/%m/%d %H:%M:%S\n") if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
55 0 $delay = $next - $now;
56 0 my $rand_watcher_id = rand();
57 0 ▶ $self->{watchers}{$rand_watcher_id} = AE::timer $delay,0,sub{
Subroutine __ANON__: not called CC=2 SCAR=17.9
58 0 delete $self->{watchers}{$rand_watcher_id};
59 0 eval{
60 0 $callback->();
61 };
62 0 ▶ console $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
63 0 $self->add_job($type,$t,$callback);
64 0 };
65 }
66 1 1;
« Webqq/Client/Plugin/ShowMsg.pm ↑ Summary Webqq/Client/Plugin/IrcSync.pm »