Helios/JobType.pm

filter: Statementstmt 23.1% 25 / 108 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 33.3% 8 / 24 Podpod 33.3% 5 / 15 totaltotal 19.7% 38 / 192 CC 17 SCAR 28.4
CC
17
Cov
17%
lookup
56.0
create
30.0
remove
20.0
CRAP 17.1
« Helios/TestService.pm ↑ Summary Helios/ObjectDriver.pm »
1 package Helios::JobType;
2  
3 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use TheSchwartz::FuncMap;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Helios::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Helios::ObjectDriver;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Helios::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Helios::Error::JobTypeError;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our $VERSION = '2.80';
14  
15 =head1 NAME
16  
17 Helios::JobType - class to represent Helios jobtypes
18  
19 =head1 SYNOPSIS
20  
21 # use the lookup() class method to retrieve jobtypes
22 # from the Helios collective database
23 my $jobtype = Helios::JobType->lookup(name => 'Helios::TestService');
24 --OR--
25 my $jobtype = Helios::JobType->lookup(jobtypeid => 1);
26  
27 print "Name: ", $jobtype->getName, " Jobtypeid: ", $jobtype->getJobtypeid,"\n";
28
29 # use new() and create() to create new jobtypes
30 my $newtype = Helios::JobType->new( name => 'NewJobType' );
31 $newtype->create();
32 print "Created jobtype ",$newtype->getJobtypeid,"\n";
33  
34  
35 =head1 DESCRIPTION
36  
37 Objects of the Helios::JobType class represent jobtypes in the Helios job
38 processing system. Every job has a jobtype, which is roughly analogous to the
39 queue a job is in. Usually, a jobtype's name is the same as the Helios service
40 that will be running the job, in effect creating a single queue for each Helios
41 service. In certain advanced configurations, Helios services can be
42 configured to service jobs of several jobtypes.
43  
44 =head1 ACCESSOR METHODS
45  
46 set/getName name of the jobtype in the Helios database
47 set/getJobtypeid jobtypeid in the Helios database
48
49 set/getConfig config hash to use
50 set/getDriver Data::ObjectDriver to Helios database
51  
52 =cut
53  
54 0 ▶sub debug { defined($_[1]) ? $_[0]->{debug} = $_[1] : return $_[0]->{debug} }
Subroutine debug: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 0 0 0
55  
56 0 ▶sub setName {
Subroutine setName: not called CC=1 SCAR=6.9
Pod undocumented
57 0 $_[0]->{name} = $_[1];
58 }
59 0 ▶sub getName {
Subroutine getName: not called CC=1 SCAR=6.9
Pod undocumented
60 0 return $_[0]->{name};
61 }
62  
63 0 ▶sub setJobtypeid {
Subroutine setJobtypeid: not called CC=1 SCAR=6.9
Pod undocumented
64 0 $_[0]->{jobtypeid} = $_[1];
65 }
66 0 ▶sub getJobtypeid {
Subroutine getJobtypeid: not called CC=1 SCAR=6.9
Pod undocumented
67 0 return $_[0]->{jobtypeid};
68 }
69  
70 0 ▶sub setConfig {
Subroutine setConfig: not called CC=1 SCAR=6.9
Pod undocumented
71 0 $_[0]->{config} = $_[1];
72 }
73 0 ▶sub getConfig {
Subroutine getConfig: not called CC=1 SCAR=6.9
Pod undocumented
74 0 return $_[0]->{config};
75 }
76  
77 0 ▶sub setDriver {
Subroutine setDriver: not called CC=1 SCAR=6.9
Pod undocumented
78 0 $_[0]->{driver} = $_[1];
79 }
80 0 ▶sub getDriver {
Subroutine getDriver: not called CC=1 SCAR=6.9
Pod undocumented
81 0 initDriver(@_);
82 }
83 0 ▶sub initDriver {
Subroutine initDriver: not called CC=1 SCAR=6.9
Pod undocumented
84 0 my $self = shift;
85 0 my $d = Helios::ObjectDriver->getDriver(@_);
86 0 $self->setDriver($d);
87 0 return $d;
88 }
89  
90 =head1 OBJECT INITIALIZATION
91  
92 =head2 new([name => $jobtypename][, config => $config_hashref][, driver => $driver_obj][, obj => $elemental_obj])
93  
94 Creates a new Helios::JobType object. If parameters are passed, calls init()
95 with those parameters to initialize the object's values.
96  
97 =cut
98  
99 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
100 0 my $cl = shift;
101 0 my $self = {
102 jobtypeid => undef,
103 name => undef,
104  
105 _obj => undef,
106 debug => undef,
107 driver => undef,
108 config => undef,
109 };
110 0 bless $self, $cl;
111 0 ▶ $self->init(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
112 0 return $self;
113 }
114  
115  
116 =head2 init()
117  
118 If any parameters are passed to new(), init() will be called to further
119 initialize the object.
120  
121 =cut
122  
123 0 ▶sub init {
Subroutine init: not called CC=2 SCAR=17.9
Pod documented
124 0 my $self = shift;
125 0 my %params = @_;
126 # populate any params we were given
127 0 $self->{debug} = $params{debug};
128 0 $self->{name} = $params{name};
129 0 $self->{jobtypeid} = $params{jobtypeid};
130 0 $self->{driver} = $params{driver};
131 0 $self->{config} = $params{config};
132 # if we were given an elemental object, inflate our object from it
133 # otherwise, our init() is done
134 0 ▶ if ($params{obj}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'obj'}) { } 0 0 0
135 0 return $self->_inflate($params{obj});
136 } else {
137 0 return $self;
138 }
139 }
140  
141  
142 =head2 _inflate()
143  
144 If an elemental object is passed to new() or init() using the 'obj' parameter,
145 _inflate() will be called to expand the elemental object into the full Helios
146 object.
147  
148 Helios::JobType objects can be inflated from TheSchwartz::FuncMap objects.
149  
150 =cut
151  
152 0 ▶sub _inflate {
Subroutine _inflate: not called CC=1 SCAR=6.9
153 0 my $self = shift;
154 0 my $obj = shift;
155 # we were given an object to inflate from
156 0 $self->{_obj} = $obj;
157 0 $self->{name} = $obj->funcname();
158 0 $self->{jobtypeid} = $obj->funcid();
159 0 return $self;
160 }
161  
162  
163 =head1 CLASS METHODS
164  
165 =head2 lookup([name => $name]|[jobtypeid => $jobtypeid])
166  
167 Given either a jobtype name or jobtypeid, the lookup method will attempt to
168 find a jobtype matching that criteria in the collective database and returns a
169 Helios::JobType object representing that jobtype to the calling routine. If a
170 matching jobtype is not found, undef is returned.
171  
172 THROWS: Helios::Error::JobTypeError if a problem occurs querying the Helios
173 database.
174  
175 =cut
176  
177 0 ▶sub lookup {
Subroutine lookup: not called CC=7 SCAR=40.3
Pod documented
178 0 my $self = shift;
179 0 my %params = @_;
180 0 my $jobtypeid = $params{jobtypeid};
181 0 my $name = $params{name};
182 0 my $config = $params{config};
183 0 ▶ my $debug = $params{debug} || 0;
Condition0%0/2
$params{'debug'} || 0
l!l
00
MC/DC0%0/1
$params{'debug'} || 0
$params{'debug'}
Truth table0%0/2
A
A= $params{'debug'}
Aresult
11
00
184 0 my $drvr;
185 my $obj;
186
187 # throw an error if we don't have either name or jobtypeid
188 0 ▶ unless ($params{jobtypeid} || $params{name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"jobtypeid"} or $params{"name"}) 0 0 0
Condition0%0/3
$params{"jobtypeid"} or $params{"name"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{"jobtypeid"} or $params{"name"}
$params{"jobtypeid"} $params{"name"}
Truth table0%0/3
A or B
A= $params{"jobtypeid"}B= $params{"name"}
ABresult
1X1
011
000
189 0 Helios::Error::JobTypeError->throw('lookup(): Either a jobtypeid or name is required.');
190 }
191  
192 eval {
193 0 $drvr = Helios::ObjectDriver->getDriver(config => $config);
194 0 ▶ if ($jobtypeid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jobtypeid) { } 0 0 0
195 # use $jobtypeid!
196 0 $obj = $drvr->lookup('TheSchwartz::FuncMap' => $jobtypeid);
197 } else {
198 # use name
199 0 my $itr = $drvr->search('TheSchwartz::FuncMap' => {funcname => $name});
200 0 $obj = $itr->next();
201 }
202
203 0 1;
204 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $drvr = "Helios::ObjectDriver"->getDriver("config", $config); if ($jobtypeid) { $obj = $drvr->lookup("TheSchwartz::FuncMap", $jobtypeid); } else { my $itr = $drvr->search("TheSchwartz::FuncMap", {"funcname", $name}); $obj = $itr->next; } ; 1 } } or do { my $E = $@; "Helios::Error::JobTypeError"->throw('lookup(): ' . "$E") } 0 0 0
205 0 my $E = $@;
206 0 Helios::Error::JobTypeError->throw('lookup(): '."$E");
207 };
208
209 0 ▶ if (defined($obj)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj) { } 0 0 0
210 # we found it!
211 0 return Helios::JobType->new(
212 obj => $obj,
213 driver => $drvr,
214 config => Helios::ObjectDriver->getConfig(),
215 debug => $debug,
216 );
217 } else {
218 # we didn't find it
219 0 return undef;
220 }
221 }
222  
223  
224 =head1 OBJECT METHODS
225  
226 =head2 create([name => $name])
227  
228 Given a jobtype name, create() creates a jobtype with that name in the Helios
229 collective database and returns the new jobtype's jobtypeid.
230  
231 If the jobtype's name is not specified, the value returned by getName() will be
232 used.
233  
234 THROWS: Helios::Error::JobTypeError if the JobType creation fails.
235  
236 =cut
237  
238 0 ▶sub create {
Subroutine create: not called CC=5 SCAR=34.0
Pod documented
239 0 my $self = shift;
240 0 my %params = @_;
241 0 ▶ my $name = $params{name} || $self->getName();
Condition0%0/3
$params{'name'} || $self->getName
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{'name'} || $self->getName
$params{'name'} $self->getName
Truth table0%0/3
A || B
A= $params{'name'}B= $self->getName
ABresult
1X1
011
000
242 0 ▶ my $config = $params{config} || $self->getConfig();
Condition0%0/3
$params{'config'} || $self->getConfig
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{'config'} || $self->getConfig
$params{'config'} $self->getConfig
Truth table0%0/3
A || B
A= $params{'config'}B= $self->getConfig
ABresult
1X1
011
000
243 0 my $id;
244 my $obj;
245
246 0 ▶ unless ($name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 0 0
247 0 Helios::Error::JobTypeError->throw('create(): A jobtype name is required to create a jobtype.');
248 }
249
250 eval {
251 0 my $drvr = $self->getDriver(config => $config);
252 0 $obj = TheSchwartz::FuncMap->new( funcname => $name);
253 0 $drvr->insert($obj);
254 0 1;
255 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $drvr = $self->getDriver("config", $config); $obj = "TheSchwartz::FuncMap"->new("funcname", $name); $drvr->insert($obj); 1 } } or do { my $E = $@; "Helios::Error::JobTypeError"->throw("create(): $E") } 0 0 0
256 0 my $E = $@;
257 0 Helios::Error::JobTypeError->throw("create(): $E");
258 };
259 # use the new TheSchwartz::FuncMap object to (re)inflate $self
260 0 $self->_inflate($obj);
261 # the calling routine expects to receive the jobtypeid
262 0 return $self->getJobtypeid;
263 }
264  
265  
266 =head2 remove()
267  
268 The remove() method deletes a jobtype from the Helios collective database.
269 It returns 1 if successful and throws a Helios::Error::JobTypeError if the
270 removal operation fails.
271  
272 USE WITH CAUTION. All Helios jobs, both enqueued and completed, have an
273 associated jobtype, and removing a jobtype that still has associated jobs in
274 the system will have unintended consequences!
275  
276 THROWS: Helios::Error::JobTypeError if the removal operation fails.
277  
278 =cut
279  
280 0 ▶sub remove {
Subroutine remove: not called CC=4 SCAR=30.0
Pod documented
281 0 my $self = shift;
282 0 my $jobtypeid = $self->getJobtypeid;
283 0 my $drvr;
284 my $r;
285
286 # we actually need the FuncMap object here, because we're going to use
287 # D::OD to do the delete operation.
288 0 ▶ unless ($self->{_obj} && $jobtypeid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_obj"} and $jobtypeid) 0 0 0
Condition0%0/3
$self->{"_obj"} and $jobtypeid
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_obj"} and $jobtypeid
$self->{"_obj"} $jobtypeid
Truth table0%0/3
A and B
A= $self->{"_obj"}B= $jobtypeid
ABresult
0X0
100
111
289 0 Helios::Error::JobTypeError->throw('remove(): Helios::JobType object was not properly initialized; cannot remove.');
290 }
291
292 eval {
293 0 $drvr = $self->getDriver();
294 0 $drvr->remove($self->{_obj});
295  
296 0 1;
297 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $drvr = $self->getDriver; $drvr->remove($self->{"_obj"}); 1 } } or do { my $E = $@; "Helios::Error::JobTypeError"->throw("remove(): $E") } 0 0 0
298 0 my $E = $@;
299 0 Helios::Error::JobTypeError->throw("remove(): $E");
300 };
301 # signal the calling routine remove was successful
302 0 return 1;
303 }
304  
305  
306 1 1;
307 __END__
« Helios/TestService.pm ↑ Summary Helios/ObjectDriver.pm »