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