File Coverage

blib/lib/Helios/TestService.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1             package Helios::TestService;
2              
3 1     1   1278 use 5.008;
  1         2  
  1         35  
4 1     1   4 use strict;
  1         1  
  1         21  
5 1     1   3 use warnings;
  1         1  
  1         19  
6 1     1   3 use base qw( Helios::Service );
  1         1  
  1         55  
7              
8             use Helios::Error;
9             use Helios::LogEntry::Levels qw(:all);
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             my $self = shift;
62             my $job = shift;
63             my $config = $self->getConfig();
64             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             if ($self->debug) {
71             $self->printConfigParams();
72             $self->printJobArgs();
73             }
74              
75             # Hello World!
76             $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             foreach my $arg (sort keys %$args) {
81             $self->logMsg($job,"JOBARG=$arg VALUE=".$args->{$arg});
82             }
83              
84             # if your job completes successfully, you need to mark it was completed
85             $self->completedJob($job);
86             1;
87             } or do {
88             my $E = $@;
89             if ($E->isa('Helios::Error::Warning')) {
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             $self->logMsg($job, LOG_WARNING, "WARNING: ".$E);
93             $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             $self->logMsg($job, LOG_ERR, "FAILED: ".$E);
99             $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             $self->logMsg($job, LOG_ERR, "FAILED with unexpected error: ".$E);
106             $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             my $self = shift;
122             my $c = $self->getConfig();
123              
124             print "<--CONFIG PARAMS-->\n";
125             foreach (sort keys %$c) {
126             print $_.' => '.$c->{$_}."\n";
127             }
128             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             my $self = shift;
141             my $j = $self->getJob();
142             my $a = $self->getJobArgs($j);
143            
144             print "<--JOB ARGUMENTS-->\n";
145             foreach (sort keys %$a) {
146             print $_.' => '.$a->{$_}."\n";
147             }
148             return 1;
149             }
150              
151              
152             1;
153             __END__