Helios/TestService.pm

filter: Statementstmt 35.8% 19 / 53 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 6 / 9 Podpod 100% 3 / 3 totaltotal 38.3% 28 / 73 CC 8 SCAR 28.7
CC
8
Cov
31%
run
42.0
printConfigParams
6.0
printJobArgs
6.0
CRAP 17.6
« Helios/Logger/Internal.pm ↑ Summary Helios/JobType.pm »
1 package Helios::TestService;
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 base qw( Helios::Service );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Helios::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Helios::LogEntry::Levels qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # 2012-01-01: Completely rewrote this to be more clear, concise, and to use
12 # more up-to-date Perl.
13  
14 1 our $VERSION = '2.40';
15  
16 =head1 NAME
17  
18 Helios::TestService - Helios::Service subclass useful for testing
19  
20 =head1 DESCRIPTION
21  
22 You can use Helios::TestService to test the functionality of your Helios
23 collective:
24  
25 =over 4
26  
27 =item 1.
28  
29 Start a helios.pl daemon to service Helios::TestService jobs by issuing:
30  
31 helios.pl Helios::TestService
32  
33 at a command prompt.
34  
35 =item 2.
36  
37 Use the helios_job_submit.pl program to submit a Helios::TestService job to the
38 Helios collective:
39  
40 helios_job_submit.pl Helios::TestService "<job><params><arg1>value1</arg1></params></job>"
41  
42 =item 3.
43  
44 Helios should run the test job. Helios::TestService will log a "Hello World"
45 message, then any job arguments will be parsed and logged as entries in the
46 Helios log. If you are running the service in debug mode, the service's
47 config parameters and job arguments will be printed to the terminal as well.
48  
49 =back
50  
51 =head1 METHODS
52  
53 =head2 run()
54  
55 The run() method prints a "Hello World!" message to the Helios log and then
56 logs the job arguments.
57  
58 =cut
59  
60 0 ▶sub run {
Subroutine run: not called CC=6 SCAR=37.4
Pod documented
61 0 my $self = shift;
62 0 my $job = shift;
63 0 my $config = $self->getConfig();
64 0 my $args = $self->getJobArgs($job);
65  
66 eval {
67  
68 # this is just for debugging purposes, you wouldn't print
69 # to the terminal in a normal Helios service
70 0 ▶ if ($self->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 0 0
71 0 $self->printConfigParams();
72 0 $self->printJobArgs();
73 }
74  
75 # Hello World!
76 0 $self->logMsg($job, 'Helios::TestService says, "Hello World!"');
77  
78 # just for fun (and to prove we received the job correctly)
79 # log the job arguments
80 0 foreach my $arg (sort keys %$args) {
81 0 $self->logMsg($job,"JOBARG=$arg VALUE=".$args->{$arg});
82 }
83  
84 # if your job completes successfully, you need to mark it was completed
85 0 $self->completedJob($job);
86 0 1;
87 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { if ($self->debug) { $self->printConfigParams; $self->printJobArgs; } ; $self->logMsg($job, "Helios::TestService says, \"Hello World!\""); foreach my $arg (sort keys %$args) { $self->logMsg($job, "JOBARG=$arg VALUE=" . $args->{$arg}); } ; $self->completedJob($job); 1 } } or do { my $E = $@; if ($E->isa("Helios::Error::Warning")) { $self->logMsg($job, 4, 'WARNING: ' . $E); $self->completedJob($job); } elsif ($E->isa("Helios::Error::Fatal")) { $self->logMsg($job, 3, 'FAILED: ' . $E); $self->failedJob($job, $E, 1); } else { $self->logMsg($job, 3, 'FAILED with unexpected error: ' . $E); $self->failedJob($job, $E); } } 0 0 0
88 0 my $E = $@;
89 0 ▶ if ($E->isa('Helios::Error::Warning')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($E->isa('Helios::Error::Warning')) { } 0 0 0
elsif ($E->isa('Helios::Error::Fatal')) { } 0 0 0
90 # you can throw this in your run() method to indicate
91 # there was a problem, but it wasn't bad enough for the job to fail
92 0 $self->logMsg($job, LOG_WARNING, "WARNING: ".$E);
93 0 $self->completedJob($job);
94 } elsif ($E->isa('Helios::Error::Fatal')) {
95 # you can throw this in your run() method to indicate a job failed
96 # calling failedJob() will tell Helios to mark it as failed
97 # but allow it to be retried if your service supports that
98 0 $self->logMsg($job, LOG_ERR, "FAILED: ".$E);
99 0 $self->failedJob($job, $E, 1);
100 } else {
101 # this will catch all the other exceptions that happen
102 # if you don't really care what kind of error was thrown,
103 # this (and the $E declaration above) is all you really need
104 # in your 'or do' block.
105 0 $self->logMsg($job, LOG_ERR, "FAILED with unexpected error: ".$E);
106 0 $self->failedJob($job, $E);
107 }
108 };
109  
110 }
111  
112  
113 =head2 printConfigParams()
114  
115 This method prints the service's config params to the terminal. It's only
116 called when Helios::TestService is run in debug mode.
117  
118 =cut
119  
120 0 ▶sub printConfigParams {
Subroutine printConfigParams: not called CC=2 SCAR=17.9
Pod documented
121 0 my $self = shift;
122 0 my $c = $self->getConfig();
123  
124 0 print "<--CONFIG PARAMS-->\n";
125 0 foreach (sort keys %$c) {
126 0 print $_.' => '.$c->{$_}."\n";
127 }
128 0 return 1;
129 }
130  
131  
132 =head2 printJobArgs()
133  
134 This method prints the job arguments passed to the service to the terminal.
135 It's only called when Helios::TestService is run in debug mode.
136  
137 =cut
138  
139 0 ▶sub printJobArgs {
Subroutine printJobArgs: not called CC=2 SCAR=17.9
Pod documented
140 0 my $self = shift;
141 0 my $j = $self->getJob();
142 0 my $a = $self->getJobArgs($j);
143
144 0 print "<--JOB ARGUMENTS-->\n";
145 0 foreach (sort keys %$a) {
146 0 print $_.' => '.$a->{$_}."\n";
147 }
148 0 return 1;
149 }
150  
151  
152 1 1;
153 __END__
« Helios/Logger/Internal.pm ↑ Summary Helios/JobType.pm »