blib/lib/XAS/Lib/App.pm

filter: Statementstmt 25.3% 17 / 67 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 20.0% 5 / 25 Podpod 100% 8 / 8 totaltotal 29.4% 30 / 102 CC 2 SCAR 7.5
CC
2
Cov
25%
_parse_cmdline
6.0
signal_handler
2.0
define_signals
2.0
CRAP 2.1
« blib/lib/XAS/Lib/POE/Service.pm ↑ Summary blib/lib/XAS/Apps/Test/SSH/Server.pm »
1 package XAS::Lib::App;
2  
3 1 our $VERSION = '0.04';
4  
5 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Hash::Merge;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 use XAS::Class
11 1 debug => 0,
12 version => $VERSION,
13 base => 'XAS::Base',
14 mixin => 'XAS::Lib::Mixins::Handlers',
15 import => 'CLASS',
16 utils => 'dotid',
17 filesystem => 'File',
18 vars => {
19 PARAMS => {
20 -throws => { optional => 1, default => 'changeme' },
21 -facility => { optional => 1, default => 'systems' },
22 -priority => { optional => 1, default => 'low' },
23 }
24 }
25 1 ▶;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 #use Data::Dumper;
28  
29 # ----------------------------------------------------------------------
30 # Public Methods
31 # ----------------------------------------------------------------------
32  
33 sub signal_handler {
34 0 ▶ my $signal = shift;
Subroutine signal_handler: not called CC=1 SCAR=6.9
Pod documented
35  
36 0 my $ex = XAS::Exception->new(
37 type => 'xas.lib.app.signal_handler',
38 info => 'process interrupted by signal ' . $signal
39 );
40  
41 0 $ex->throw();
42  
43 }
44  
45 sub define_signals {
46 0 ▶ my $self = shift;
Subroutine define_signals: not called CC=1 SCAR=6.9
Pod documented
47  
48 0 $SIG{'INT'} = \&signal_handler;
49 0 $SIG{'QUIT'} = \&signal_handler;
50  
51 }
52  
53 sub define_pidfile {
54 0 ▶ my $self = shift;
Subroutine define_pidfile: not called CC=1 SCAR=6.9
Pod documented
55  
56 }
57  
58 sub define_daemon {
59 0 ▶ my $self = shift;
Subroutine define_daemon: not called CC=1 SCAR=6.9
Pod documented
60  
61 }
62  
63 sub run {
64 0 ▶ my $self = shift;
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
65  
66 0 my $rc = 0;
67  
68 try {
69  
70 0 ▶ $self->define_signals();
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 0 $self->define_daemon();
72 0 $self->define_pidfile();
73  
74 0 $self->main();
75  
76 } catch {
77  
78 0 ▶ my $ex = $_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
79  
80 0 $rc = $self->exit_handler($ex);
81  
82 0 };
83  
84 0 return $rc;
85  
86 }
87  
88 sub main {
89 0 ▶ my $self = shift;
Subroutine main: not called CC=1 SCAR=6.9
Pod documented
90  
91 0 $self->log->warn('You need to override main()');
92  
93 }
94  
95 sub options {
96 0 ▶ my $self = shift;
Subroutine options: not called CC=1 SCAR=6.9
Pod documented
97  
98 0 return {};
99  
100 }
101  
102 # ----------------------------------------------------------------------
103 # Private Methods
104 # ----------------------------------------------------------------------
105  
106 sub init {
107 0 ▶ my $class = shift;
Subroutine init: not called CC=1 SCAR=6.9
Pod documented
108  
109 0 my $self = $class->SUPER::init(@_);
110  
111 0 $self->class->throws($self->throws);
112  
113 0 my $options = $self->options();
114 0 my $defaults = $self->_default_options();
115  
116 0 $self->_parse_cmdline($defaults, $options);
117  
118 0 return $self;
119  
120 }
121  
122 sub _default_options {
123 0 ▶ my $self = shift;
Subroutine _default_options: not called CC=1 SCAR=6.9
124  
125 0 my $version = $self->CLASS->VERSION;
126 0 my $script = $self->env->script;
127  
128 return {
129 0 ▶ 'log-type=s' => sub { $self->env->log_type($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
130 0 ▶ 'log-facility=s' => sub { $self->env->log_facility($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
131 0 ▶ 'alerts!' => sub { $self->env->alerts($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
132 0 ▶ 'help|h|?' => sub { pod2usage(-verbose => 0, -exitstatus => 0); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
133 0 ▶ 'manual' => sub { pod2usage(-verbose => 2, -exitstatus => 0); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
134 0 ▶ 'version' => sub { printf("%s - v%s\n", $script, $version); exit 0; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 'debug' => sub {
136 0 ▶ $self->env->xdebug(1);
Subroutine __ANON__: not called CC=1 SCAR=6.9
137 0 $self->log->level('debug', 1);
138 },
139 'logfile=s' => sub {
140 0 ▶ my $logfile = File($_[1]);
Subroutine __ANON__: not called CC=1 SCAR=6.9
141 0 $self->env->log_type('file');
142 0 $self->env->logfile($logfile);
143 }
144 0 };
145  
146 }
147  
148 sub _parse_cmdline {
149 0 ▶ my ($self, $defaults, $optional) = @_;
Subroutine _parse_cmdline: not called CC=2 SCAR=17.9
150  
151 0 my $hm = Hash::Merge->new('RIGHT_PRECEDENT');
152 0 my %options = %{ $hm->merge($defaults, $optional) };
153  
154 0 ▶ GetOptions(%options) or pod2usage(-verbose => 0, -exitstatus => 1);
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(%options) or pod2usage("-verbose", 0, "-exitstatus", 1) 0 0 0
155  
156 }
157  
158 1 1;
159  
160 __END__
161  
162 =head1 NAME
163  
164 XAS::Lib::App - The base class to write procedures within the XAS environment
165  
166 =head1 SYNOPSIS
167  
168 use XAS::Lib::App;
169  
170 my $app = XAS::Lib::App->new();
171  
172 $app->run();
173  
174 =head1 DESCRIPTION
175  
176 This module defines a base class for writing procedures. It provides
177 signal handling, options processing, along with a exit handler.
178  
179 =head1 METHODS
180  
181 =head2 new
182  
183 This method initilaizes the module. It inherits from XAS::Base and takes
184 these additional parameters:
185  
186 =over 4
187  
188 =item B<-throws>
189  
190 This changes the default error message from "changeme" to something useful.
191  
192 =item B<-facility>
193  
194 This will change the facility of the alert. The default is 'systems'.
195  
196 =item B<-priority>
197  
198 This will change the priority of the alert. The default is 'low'.
199  
200 =back
201  
202 =head2 run
203  
204 This method sets up a global exception handler and calls main(). The main()
205 method will be passed one parameter: an initialised handle to this class.
206  
207 Example
208  
209 sub main {
210 my $self = shift;
211  
212 $self->log->debug('in main');
213  
214 }
215  
216 =over 4
217  
218 =item Exception Handling
219  
220 If an exception is caught, the global exception handler will send an alert,
221 write the exception to the log and returns an exit code of 1.
222  
223 =item Normal Completion
224  
225 When the procedure completes successfully, it will return an exit code of 0.
226  
227 =back
228  
229 To change this behavior you would need to override the exit_handler() method.
230  
231 =head2 main
232  
233 This is where your main line logic starts.
234  
235 =head2 options
236  
237 This method sets up additional cli options. Option handling is provided
238 by L<Getopt::Long|https://metacpan.org/pod/Getopt::Long>. To access these
239 options you need to define accessors for them.
240  
241 Example
242  
243 use XAS::Class
244 version => '0.01',
245 base => 'XAS::Lib::App',
246 accessors => 'widget'
247 ;
248  
249 sub main {
250 my $self = shift;
251  
252 $self->log->info('starting up');
253 sleep(60);
254 $self->log->info('shutting down');
255  
256 }
257  
258 sub options {
259 my $self = shift;
260  
261 return {
262 'widget=s' => sub {
263 $self->{widget} = uc($_[1]);
264 }
265 };
266  
267 }
268  
269 =head2 define_signals
270  
271 This method sets up basic signal handling. By default this is only for the INT
272 and QUIT signals.
273  
274 Example
275  
276 sub define_signals {
277 my $self = shift;
278  
279 $SIG{INT} = \&signal_handler;
280 $SIG{QUIT} = \&singal_handler;
281  
282 }
283  
284 =head2 define_pidfile
285  
286 This is an entry point to define a pid file.
287  
288 =head2 define_daemon
289  
290 This is an entry point so the procedure can daemonize.
291  
292 =head2 signal_handler($signal)
293  
294 This method is a default signal handler. By default it throws an exception.
295 It takes one parameter.
296  
297 =over 4
298  
299 =item B<$signal>
300  
301 The signal that was captured.
302  
303 =back
304  
305 =head1 OPTIONS
306  
307 This module handles the following command line options.
308  
309 =head2 --debug
310  
311 This toggles debugging output.
312  
313 =head2 --[no]alerts
314  
315 This toggles sending alerts. They are on by default.
316  
317 =head2 --help
318  
319 This prints out a short help message based on the procedures pod.
320  
321 =head2 --manual
322  
323 This displaces the procedures manual in the defined pager.
324  
325 =head2 --version
326  
327 This prints out the version of the module.
328  
329 =head2 --log-type
330  
331 What type of log to use. By default the log is displayed on the console. Log
332 types can be one of the following "console", "file", "json" or "syslog".
333  
334 =head2 --log-facility
335  
336 What log facility class to use. This follows syslog convention. By default
337 the facility is "local6".
338  
339 =head2 --logfile
340  
341 The name of the log file. When --logfile is specified, it implies a log type
342 of "file".
343  
344 =head1 SEE ALSO
345  
346 =over 4
347  
348 =item L<XAS|XAS>
349  
350 =back
351  
352 =head1 AUTHOR
353  
354 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
355  
356 =head1 COPYRIGHT AND LICENSE
357  
358 Copyright (c) 2014 Kevin L. Esteb
359  
360 This is free software; you can redistribute it and/or modify it under
361 the terms of the Artistic License 2.0. For details, see the full text
362 of the license at http://www.perlfoundation.org/artistic_license_2_0.
363  
364 =cut
« blib/lib/XAS/Lib/POE/Service.pm ↑ Summary blib/lib/XAS/Apps/Test/SSH/Server.pm »