File Coverage

blib/lib/Helios/TestService.pm
Criterion Covered Total %
statement 17 51 33.3
branch 0 8 0.0
condition n/a
subroutine 6 9 66.6
pod 3 3 100.0
total 26 71 36.6


line stmt bran cond sub pod time code
1             package Helios::TestService;
2              
3 1     1   792 use 5.008;
  1         2  
4 1     1   4 use strict;
  1         1  
  1         16  
5 1     1   4 use warnings;
  1         1  
  1         20  
6 1     1   3 use base qw( Helios::Service );
  1         0  
  1         56  
7              
8 1     1   5 use Helios::Error;
  1         1  
  1         21  
9 1     1   4 use Helios::LogEntry::Levels qw(:all);
  1         0  
  1         419  
10              
11             # 2012-01-01: Completely rewrote this to be more clear, concise, and to use
12             # more up-to-date Perl.
13              
14             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 "value1"
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             sub run {
61 0     0 1   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 0         if ($self->debug) {
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 0         } or do {
88 0           my $E = $@;
89 0 0         if ($E->isa('Helios::Error::Warning')) {
    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             sub printConfigParams {
121 0     0 1   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             sub printJobArgs {
140 0     0 1   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;
153             __END__