| 1 | # | ||||||||||||||||||||||||||||
| 2 | # $Id: Job.pm 67 2008-02-11 10:49:36Z sini $ | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # CA::AutoSys - Perl Interface to CA's AutoSys job control. | ||||||||||||||||||||||||||||
| 5 | # Copyright (c) 2007 Sinisa Susnjar <sini@cpan.org> | ||||||||||||||||||||||||||||
| 6 | # See LICENSE for terms of distribution. | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # This library is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 9 | # modify it under the terms of the GNU Lesser General Public | ||||||||||||||||||||||||||||
| 10 | # License as published by the Free Software Foundation; either | ||||||||||||||||||||||||||||
| 11 | # version 2.1 of the License, or (at your option) any later version. | ||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||
| 13 | # This library is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||||||||||||||||||||||||||
| 16 | # Lesser General Public License for more details. | ||||||||||||||||||||||||||||
| 17 | # | ||||||||||||||||||||||||||||
| 18 | # You should have received a copy of the GNU Lesser General Public | ||||||||||||||||||||||||||||
| 19 | # License along with this library; if not, write to the Free Software | ||||||||||||||||||||||||||||
| 20 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||||||||||||||||||||||||||||
| 21 | # | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | package CA::AutoSys::Job; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | require CA::AutoSys::Status; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 1 | ▶ | use vars qw(@ISA @EXPORT $VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 1 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 33 | 1 | @EXPORT = qw(&new); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | $VERSION = '1.03'; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 1 | my $debug = 0; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | 0 | my $self = {}; | |||||||||||||||||||||||||||
| 41 | 0 | my $class = shift(); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 45 | 0 | ▶ | $self->{parent} = $args{parent} ? $args{parent} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | $self->{dbh} = $args{database_handle} ? $args{database_handle} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | $self->{sth} = $args{statement_handle} ? $args{statement_handle} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | $self->{parent_job} = $args{parent_job} ? $args{parent_job} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | if ($debug) { printf("DEBUG: Job(%s) created.\n", $self); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | bless($self); | |||||||||||||||||||||||||||
| 54 | 0 | return $self; | |||||||||||||||||||||||||||
| 55 | } # new() | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub _fetch_next { | ||||||||||||||||||||||||||
|
Subroutine
_fetch_next: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 58 | 0 | my $self = shift(); | |||||||||||||||||||||||||||
| 59 | 0 | ▶ | if ($debug) { printf("DEBUG: Job(%s): _fetch_next()\n", $self); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if (my $h = $self->{sth}->fetchrow_hashref()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if (defined($self->{parent_job})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | $self->{parent_job}->{child_cnt}++; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 0 | foreach (keys %{ $h }) { | |||||||||||||||||||||||||||
| 65 | 0 | $self->{$_} = $h->{$_}; | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | $self->{$_} =~ s/^[ ]*$//g if ($self->{$_}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | $self->{status} = CA::AutoSys::Status->new(parent => $self->{parent}, last_start => $self->{last_start}, | ||||||||||||||||||||||||||||
| 69 | last_end => $self->{last_end}, status => $self->{status}, | ||||||||||||||||||||||||||||
| 70 | run_num => $self->{run_num}, ntry => $self->{ntry}, | ||||||||||||||||||||||||||||
| 71 | exit_code => $self->{exit_code}, | ||||||||||||||||||||||||||||
| 72 | 0 | status_time => $self->{status_time}); | |||||||||||||||||||||||||||
| 73 | 0 | return $self; | |||||||||||||||||||||||||||
| 74 | } else { | ||||||||||||||||||||||||||||
| 75 | 0 | $self->{sth}->finish(); | |||||||||||||||||||||||||||
| 76 | 0 | return undef; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } # _fetch_next() | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub _query { | ||||||||||||||||||||||||||
|
Subroutine
_query: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 81 | 0 | my $query = qq{ | |||||||||||||||||||||||||||
| 82 | select j.*, s.*, j2.job_name as box_name | ||||||||||||||||||||||||||||
| 83 | from job j join job_status s | ||||||||||||||||||||||||||||
| 84 | on j.joid = s.joid | ||||||||||||||||||||||||||||
| 85 | left outer join job j2 | ||||||||||||||||||||||||||||
| 86 | on j.box_joid = j2.joid | ||||||||||||||||||||||||||||
| 87 | }; | ||||||||||||||||||||||||||||
| 88 | 0 | return $query; | |||||||||||||||||||||||||||
| 89 | } # _query() | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | sub find_jobs { | ||||||||||||||||||||||||||
|
Subroutine
find_jobs: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 92 | 0 | my $self = shift(); | |||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ($debug) { printf("DEBUG: Job(%s): find_jobs()\n", $self); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | my $job_name = shift(); | |||||||||||||||||||||||||||
| 95 | 0 | my $query = _query() . qq{ | |||||||||||||||||||||||||||
| 96 | where j.job_name like '$job_name' | ||||||||||||||||||||||||||||
| 97 | order by j.joid | ||||||||||||||||||||||||||||
| 98 | }; | ||||||||||||||||||||||||||||
| 99 | 0 | my $sth = $self->{dbh}->prepare($query); | |||||||||||||||||||||||||||
| 100 | 0 | ▶ | if (!$sth->execute()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | $self->{parent}->{errstr} = "can't select info for job ".$job_name.": ".$sth->errstr(); | |||||||||||||||||||||||||||
| 102 | 0 | return undef; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | return CA::AutoSys::Job->new(parent => $self->{parent}, database_handle => $self->{dbh}, | ||||||||||||||||||||||||||||
| 105 | 0 | statement_handle => $sth); | |||||||||||||||||||||||||||
| 106 | } # find_jobs() | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub find_children { | ||||||||||||||||||||||||||
|
Subroutine
find_children: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | 0 | my $self = shift(); | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | if ($debug) { printf("DEBUG: Job(%s): find_children()\n", $self); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | my $query = _query() . qq{ | |||||||||||||||||||||||||||
| 112 | where j.box_joid = $self->{joid} | ||||||||||||||||||||||||||||
| 113 | order by j.joid | ||||||||||||||||||||||||||||
| 114 | }; | ||||||||||||||||||||||||||||
| 115 | 0 | my $sth = $self->{dbh}->prepare($query); | |||||||||||||||||||||||||||
| 116 | 0 | ▶ | if ($debug) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | printf("DEBUG: Job(%s): selecting children for joid %d\n", $self, $self->{joid}); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 0 | $self->{child_cnt} = 0; | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | if (!$sth->execute()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | $self->{parent}->{errstr} = "can't select children for job ".$self->{job_name}.": ".$sth->errstr(); | |||||||||||||||||||||||||||
| 122 | 0 | return undef; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | return CA::AutoSys::Job->new(parent => $self->{parent}, database_handle => $self->{dbh}, | ||||||||||||||||||||||||||||
| 125 | 0 | statement_handle => $sth, parent_job => $self); | |||||||||||||||||||||||||||
| 126 | } # find_children() | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub next_job { | ||||||||||||||||||||||||||
|
Subroutine
next_job: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 0 | my $self = shift(); | |||||||||||||||||||||||||||
| 130 | 0 | ▶ | if ($debug) { printf("DEBUG: Job(%s): next_job()\n", $self); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | return $self->_fetch_next(); | |||||||||||||||||||||||||||
| 132 | } # next_job() | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub next_child { | ||||||||||||||||||||||||||
|
Subroutine
next_child: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 0 | my $self = shift(); | |||||||||||||||||||||||||||
| 136 | 0 | ▶ | if ($debug) { printf("DEBUG: Job(%s): next_child()\n", $self); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 0 | return $self->_fetch_next(); | |||||||||||||||||||||||||||
| 138 | } # next_child() | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | sub get_status { | ||||||||||||||||||||||||||
|
Subroutine
get_status: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 141 | 0 | my $self = shift(); | |||||||||||||||||||||||||||
| 142 | 0 | ▶ | if ($debug) { printf("DEBUG: Job(%s): get_status()\n", $self); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | return $self->{status}; | |||||||||||||||||||||||||||
| 144 | } # get_status() | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | sub has_children { | ||||||||||||||||||||||||||
|
Subroutine
has_children: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 147 | 0 | my $self = shift(); | |||||||||||||||||||||||||||
| 148 | 0 | ▶ | if ($debug) { printf("DEBUG: Job(%s): has_children()\n", $self); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 0 | ▶ | if (!defined($self->{child_cnt}) || $self->{child_cnt} == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $self->{"child_cnt"} or $self->{"child_cnt"} == 0
MC/DC0%0/2
not defined $self->{"child_cnt"} or $self->{"child_cnt"} == 0
not defined $self->{"child_cnt"}
$self->{"child_cnt"} == 0
Truth table0%0/3
not(A) or B
A= not defined $self->{"child_cnt"}B= $self->{"child_cnt"} == 0
| |||||||||||||||||||||||||||||
| 150 | 0 | return 0; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | 0 | return 1; | |||||||||||||||||||||||||||
| 153 | } # has_children() | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 1 | 1; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | __END__ | ||||||||||||||||||||||||||||