WE/Util/AtJobManager.pm

filter: Statementstmt 13.1% 15 / 114 Branchbran 2.0% 1 / 50 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 28.5% 4 / 14 Podpod 14.2% 1 / 7 totaltotal 10.8% 21 / 193 CC 26 SCAR 35.2
CC
26
Cov
9%
check_daemon
72.0
init
42.0
start_job
42.0
CRAP 33.7
« WE_Frontend/Plugin/Navigation.pm ↑ Summary WE/DB/ObjBase.pm »
1 # -*- perl -*-
2  
3 #
4 # $Id: AtJobManager.pm,v 1.4 2004/06/09 06:11:44 eserte Exp $
5 # Author: Slaven Rezic
6 #
7 # Copyright (C) 2004 Slaven Rezic. All rights reserved.
8 # This package is free software; you can redistribute it and/or
9 # modify it under the same terms as Perl itself.
10 #
11 # Mail: slaven@rezic.de
12 # WWW: http://www.rezic.de/eserte/
13 #
14  
15 package WE::Util::AtJobManager;
16  
17 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 $VERSION = sprintf("%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/);
20  
21 1 ▶use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use WE::Util::Functions qw(is_in_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 __PACKAGE__->mk_accessors(qw(at_path at_queue));
26  
27 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
28 0 my($class, @args) = @_;
29 0 my $self = bless {}, $class;
30 0 $self->init(@args);
31 0 $self;
32 }
33  
34 0 ▶sub init {
Subroutine init: not called CC=6 SCAR=37.4
Pod undocumented
35 0 my($self, %args) = @_;
36 0 ▶ if (!$self->at_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->at_path) 0 0 0
37 0 my $path = is_in_path("at");
38 0 ▶ if (!$path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path) 0 0 0
39 0 $path = "/usr/bin/at";
40 0 ▶ undef $path if (!-x $path);
Branch: 1 branch
BranchTrueFalseTotal
unless -x $path 0 0 0
41 }
42 0 ▶ die "Can't find `at' command" if !$path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 0 0
43 0 $self->at_path("at");
44 }
45  
46 0 ▶ my $queue = $args{queue} || "c";
Condition0%0/2
$args{'queue'} || 'c'
l!l
00
MC/DC0%0/1
$args{'queue'} || 'c'
$args{'queue'}
Truth table0%0/2
A
A= $args{'queue'}
Aresult
11
00
47 0 $self->at_queue($queue);
48 }
49  
50 0 ▶sub start_job {
Subroutine start_job: not called CC=6 SCAR=37.4
Pod undocumented
51 0 my($self, $time, $command, $args) = @_;
52 0 my @cmd = $command;
53 0 ▶ if ($args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args) 0 0 0
54 0 push @cmd, @$args;
55 }
56 0 ▶ if (!@cmd) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@cmd) 0 0 0
57 0 die "No command supplied";
58 }
59  
60 0 $time = $self->_epoch_to_crontime($time);
61  
62 0 my @at_cmd = $self->at_path;
63 0 ▶ if ($self->at_queue) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->at_queue) 0 0 0
64 0 push @at_cmd, "-q" => $self->at_queue;
65 }
66 0 push @at_cmd, $time;
67 0 my $pid_status;
68 0 ▶ local $SIG{CHLD} = sub { $pid_status = $? }; # linux reaps by default
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 0 local $SIG{PIPE} = "IGNORE";
70 0 ▶ my $pid = open(AT, "|-") or do {
Branch: 1 branch
BranchTrueFalseTotal
my $pid = open(AT, "|-") or do { warn "@at_cmd\n"; exec @at_cmd; die "Can't run @{at_cmd}: $!" } 0 0 0
71 0 warn "@at_cmd\n";
72 0 exec @at_cmd;
73 0 die "Can't run @at_cmd: $!";
74 };
75 0 warn "@cmd\n";
76 0 print AT "@cmd\n";
77 0 close AT;
78 0 waitpid($pid, 0);
79 0 ▶ if ($pid_status != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid_status != 0) 0 0 0
80 0 die "at job <pid=$pid> failed to start and returned exit code $pid_status";
81 }
82 }
83  
84 0 ▶sub list_jobs {
Subroutine list_jobs: not called CC=3 SCAR=24.8
Pod undocumented
85 0 my $self = shift;
86  
87 0 require Time::Local;
88  
89 0 my @jobs;
90 0 my $queue = $self->at_queue;
91 0 ▶ open(ATQ, $self->at_path . " -l -q $queue |")
Branch: 1 branch
BranchTrueFalseTotal
open ATQ, $self->at_path . " -l -q $queue |" or die "Can't call at -l" 0 0 0
92 or die "Can't call at -l";
93 0 ▶ while(<ATQ>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline ATQ)) 0 0 0
94 0 chomp;
95 0 my($jobnr, $date, $time, $queue, $user) = split /\s+/;
96 0 my($Y,$M,$D) = split /-/, $date;
97 0 my($h,$m) = split /:/, $time;
98 0 my $epoch = Time::Local::timelocal(0, $m, $h, $D, $M-1, $Y-1900);
99 0 push @jobs, {jobnumber => $jobnr,
100 date => $date,
101 time => $time,
102 queue => $queue,
103 user => $user,
104 epoch => $epoch,
105 };
106 }
107 0 close ATQ;
108 0 @jobs;
109 }
110  
111 0 ▶sub delete_job {
Subroutine delete_job: not called CC=1 SCAR=6.9
Pod undocumented
112 0 my($self, $jobnr) = @_;
113 0 system($self->at_path, "-d", $jobnr);
114 }
115  
116 0 ▶sub check_daemon {
Subroutine check_daemon: not called CC=8 SCAR=42.8
Pod undocumented
117 0 my $self = shift;
118 0 my $atd_running = $self->_check_process("atd");
119 0 ▶ return 1 if $atd_running;
Branch: 1 branch
BranchTrueFalseTotal
if $atd_running 0 0 0
120  
121 0 ▶ if ($^O =~ /bsd/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /bsd/i) 0 0 0
122 0 my $cron_running = $self->_check_process("cron");
123 0 ▶ return 0 if !$cron_running;
Branch: 1 branch
BranchTrueFalseTotal
unless $cron_running 0 0 0
124  
125 0 my $found_atrun;
126 0 ▶ open(CRONTAB, "/etc/crontab") or die "Can't open crontab: $!";
Branch: 1 branch
BranchTrueFalseTotal
open CRONTAB, "/etc/crontab" or die "Can't open crontab: $!" 0 0 0
127 0 ▶ while(<CRONTAB>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline CRONTAB)) 0 0 0
128 0 ▶ m{/usr/libexec/atrun} and do {
Branch: 1 branch
BranchTrueFalseTotal
m[/usr/libexec/atrun] and do { $found_atrun = 1; last } 0 0 0
129 0 $found_atrun = 1;
130 0 last;
131 }
132 }
133 0 close CRONTAB;
134 0 ▶ return 1 if $found_atrun;
Branch: 1 branch
BranchTrueFalseTotal
if $found_atrun 0 0 0
135  
136 0 die "Can't find atrun entry in /etc/crontab";
137 }
138  
139 0 0;
140 }
141  
142 0 ▶sub restart_daemon {
Subroutine restart_daemon: not called CC=3 SCAR=24.8
Pod undocumented
143 0 ▶ if (-x "/etc/init.d/atd") {
Branch: 1 branch
BranchTrueFalseTotal
if (-x "/etc/init.d/atd") 0 0 0
144 0 system "/etc/init.d/atd", "start";
145 0 return $? == 0;
146 }
147  
148 0 ▶ if ($^O =~ /bsd/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /bsd/i) 0 0 0
149 0 system "/usr/sbin/cron";
150 0 return $? == 0;
151 }
152  
153 0 die "Don't know how to restart the atd/crond daemon";
154 }
155  
156 0 ▶sub _check_process {
Subroutine _check_process: not called CC=5 SCAR=34.0
157 0 my($self, $procname) = @_;
158 0 ▶ if (open(PID, "/var/run/$procname.pid")) {
Branch: 1 branch
BranchTrueFalseTotal
if (open PID, "/var/run/$procname.pid") 0 0 0
159 0 chomp(my $pid = <PID>);
160 0 close PID;
161 0 ▶ if (defined $pid) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pid) 0 0 0
162 0 ▶ if (kill 0 => $pid or $!{EPERM}) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $pid or $!{'EPERM'}) { } 0 0 0
Condition0%0/3
kill 0, $pid or $!{"EPERM"}
l!l&&r!l&&!r
000
MC/DC0%0/2
kill 0, $pid or $!{"EPERM"}
kill 0, $pid $!{"EPERM"}
Truth table0%0/3
A or B
A= kill 0, $pidB= $!{"EPERM"}
ABresult
1X1
011
000
163 0 return 1;
164 } else {
165 0 return 0;
166 }
167 }
168 }
169 0 return 0;
170 }
171  
172 0 ▶sub _epoch_to_crontime {
Subroutine _epoch_to_crontime: not called CC=1 SCAR=6.9
173 0 my($self, $time) = @_;
174  
175 0 my @l = localtime $time;
176 0 $l[4]++;
177 0 $l[5]+=1900;
178 0 sprintf "%02d:%02d %02d.%02d.%04d", @l[2,1,3,4,5];
179 }
180  
181 1 ▶return 1 if caller;
Branch: 1 branch
BranchTrueFalseTotal
if caller 1 0 1
182  
183 0 my $ajm = __PACKAGE__->new;
184 0 my $method = shift;
185 0 ▶if ($method eq 'start_job') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "start_job") 0 0 0
186 0 $ARGV[2] = [ splice @ARGV, 2 ];
187 }
188 0 my @res = $ajm->$method(@ARGV);
189 0 require Data::Dumper;
190 0 print "The result is:\n" . Data::Dumper::Dumper(@res) . "\n";
191  
192 __END__
« WE_Frontend/Plugin/Navigation.pm ↑ Summary WE/DB/ObjBase.pm »