| 1 | package XAS::Lib::App::Service; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | our $VERSION = '0.01'; | |||||||||
| 4 | |||||||||||
| 5 | 1 | my $mixin; | |||||||||
| 6 | |||||||||||
| 7 | BEGIN { | ||||||||||
| 8 | 1 | ▶ | $mixin = 'XAS::Lib::App::Service::Unix'; | ||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.2
| |||||||||||
| 9 | 1 | ▶ | $mixin = 'XAS::Lib::App::Service::Win32' if ($^O eq 'MSWin32'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 10 | } | ||||||||||
| 11 | |||||||||||
| 12 | 1 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use Pod::Usage; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | ▶ | use XAS::Lib::Pidfile; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 1 | ▶ | use XAS::Lib::Service; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | use XAS::Class | ||||||||||
| 18 | 1 | debug => 0, | |||||||||
| 19 | version => $VERSION, | ||||||||||
| 20 | base => 'XAS::Lib::App', | ||||||||||
| 21 | mixin => $mixin, | ||||||||||
| 22 | constants => 'TRUE FALSE', | ||||||||||
| 23 | filesystem => 'File', | ||||||||||
| 24 | utils => 'dotid', | ||||||||||
| 25 | accessors => 'daemon service pid', | ||||||||||
| 26 | 1 | ▶ | ; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | |||||||||||
| 28 | # ---------------------------------------------------------------------- | ||||||||||
| 29 | # Public Methods | ||||||||||
| 30 | # ---------------------------------------------------------------------- | ||||||||||
| 31 | |||||||||||
| 32 | sub define_signals { | ||||||||||
| 33 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
define_signals: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 34 | |||||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | sub define_pidfile { | ||||||||||
| 38 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
define_pidfile: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 39 | |||||||||||
| 40 | 0 | my $script = $self->env->script; | |||||||||
| 41 | |||||||||||
| 42 | 0 | $self->log->debug('entering define_pidfile()'); | |||||||||
| 43 | |||||||||||
| 44 | 0 | $self->{pid} = XAS::Lib::Pidfile->new(-pid => $$); | |||||||||
| 45 | |||||||||||
| 46 | 0 | ▶ | if (my $num = $self->pid->is_running()) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | |||||||||||
| 48 | 0 | $self->throw_msg( | |||||||||
| 49 | dotid($self->class). '.define_pidfile.runerr', | ||||||||||
| 50 | 'pid_run_error', | ||||||||||
| 51 | $script, $num | ||||||||||
| 52 | ); | ||||||||||
| 53 | |||||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 0 | ▶ | $self->pid->write() or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | $self->throw_msg( | ||||||||||
| 58 | dotid($self->class) . '.define_pidfile.wrterr', | ||||||||||
| 59 | 'pid_write_error', | ||||||||||
| 60 | $self->pid->file | ||||||||||
| 61 | ); | ||||||||||
| 62 | |||||||||||
| 63 | 0 | $self->log->debug('leaving define_pidfile()'); | |||||||||
| 64 | |||||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | sub run { | ||||||||||
| 68 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
run: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 69 | |||||||||||
| 70 | 0 | my $rc = $self->SUPER::run(); | |||||||||
| 71 | |||||||||||
| 72 | 0 | $self->pid->remove(); | |||||||||
| 73 | |||||||||||
| 74 | 0 | return $rc; | |||||||||
| 75 | |||||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | # ---------------------------------------------------------------------- | ||||||||||
| 79 | # Private Methods | ||||||||||
| 80 | # ---------------------------------------------------------------------- | ||||||||||
| 81 | |||||||||||
| 82 | sub init { | ||||||||||
| 83 | 0 | ▶ | my $class = shift; | ||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 84 | |||||||||||
| 85 | 0 | my $self = $class->SUPER::init(@_); | |||||||||
| 86 | |||||||||||
| 87 | 0 | $self->{service} = XAS::Lib::Service->new(); | |||||||||
| 88 | |||||||||||
| 89 | 0 | return $self; | |||||||||
| 90 | |||||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | sub _default_options { | ||||||||||
| 94 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_default_options: not called CC=1 SCAR=6.9
| |||||||||||
| 95 | |||||||||||
| 96 | 0 | my $options = $self->SUPER::_default_options(); | |||||||||
| 97 | |||||||||||
| 98 | 0 | $self->{daemon} = FALSE; | |||||||||
| 99 | 0 | $self->{cfgfile} = $self->env->cfgfile; | |||||||||
| 100 | 0 | $self->{pidfile} = $self->env->pidfile; | |||||||||
| 101 | |||||||||||
| 102 | 0 | $options->{'daemon'} = \$self->{daemon}; | |||||||||
| 103 | |||||||||||
| 104 | $options->{'install'} = sub { | ||||||||||
| 105 | 0 | ▶ | $self->install_service(); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 106 | 0 | exit 0; | |||||||||
| 107 | 0 | }; | |||||||||
| 108 | |||||||||||
| 109 | $options->{'deinstall'} = sub { | ||||||||||
| 110 | 0 | ▶ | $self->remove_service(); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 111 | 0 | exit 0; | |||||||||
| 112 | 0 | }; | |||||||||
| 113 | |||||||||||
| 114 | $options->{'pidfile=s'} = sub { | ||||||||||
| 115 | 0 | ▶ | my $pidfile = File($_[1]); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 116 | 0 | $self->env->pidfile($pidfile); | |||||||||
| 117 | 0 | }; | |||||||||
| 118 | |||||||||||
| 119 | $options->{'cfgfile=s'} = sub { | ||||||||||
| 120 | 0 | ▶ | my $cfgfile = File($_[1]); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 121 | 0 | $self->env->cfgfile($cfgfile); | |||||||||
| 122 | 0 | }; | |||||||||
| 123 | |||||||||||
| 124 | 0 | return $options; | |||||||||
| 125 | |||||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | 1 | 1; | |||||||||
| 129 | |||||||||||
| 130 | __END__ | ||||||||||
| 131 | |||||||||||
| 132 | =head1 NAME | ||||||||||
| 133 | |||||||||||
| 134 | XAS::Lib::App::Service - The base class to write services within the XAS environment | ||||||||||
| 135 | |||||||||||
| 136 | =head1 SYNOPSIS | ||||||||||
| 137 | |||||||||||
| 138 | use XAS::Lib::App::Service; | ||||||||||
| 139 | |||||||||||
| 140 | my $service = XAS::Lib::App::Service->new(); | ||||||||||
| 141 | |||||||||||
| 142 | $service->run(); | ||||||||||
| 143 | |||||||||||
| 144 | =head1 DESCRIPTION | ||||||||||
| 145 | |||||||||||
| 146 | This module defines an operating environment for Services. A service is a | ||||||||||
| 147 | managed daemon. They behave differently depending on what platform they | ||||||||||
| 148 | are running on. On Windows, they will run under the SCM, on Unix like boxes, | ||||||||||
| 149 | they may be standalone daemons. These differences are handled by mixins. | ||||||||||
| 150 | |||||||||||
| 151 | The proper mixin is loaded when the process starts, so all the interaction | ||||||||||
| 152 | happens in the background. It inherits from L<XAS::Lib::App|XAS::Lib::App>. Please see | ||||||||||
| 153 | that module for additional documentation. | ||||||||||
| 154 | |||||||||||
| 155 | =head1 OPTIONS | ||||||||||
| 156 | |||||||||||
| 157 | This module handles these additional options. | ||||||||||
| 158 | |||||||||||
| 159 | =head2 B<--cfgfile> | ||||||||||
| 160 | |||||||||||
| 161 | This defines a configuration file. | ||||||||||
| 162 | |||||||||||
| 163 | =head2 B<--pidfile> | ||||||||||
| 164 | |||||||||||
| 165 | This defines the pid file to use. | ||||||||||
| 166 | |||||||||||
| 167 | =head2 B<--install> | ||||||||||
| 168 | |||||||||||
| 169 | This will install the service with the Win32 SCM. | ||||||||||
| 170 | |||||||||||
| 171 | =head2 B<--deinstall> | ||||||||||
| 172 | |||||||||||
| 173 | This will deinstall the service from the Win32 SCM. | ||||||||||
| 174 | |||||||||||
| 175 | =head1 SEE ALSO | ||||||||||
| 176 | |||||||||||
| 177 | =over 4 | ||||||||||
| 178 | |||||||||||
| 179 | =item L<XAS|XAS> | ||||||||||
| 180 | |||||||||||
| 181 | =back | ||||||||||
| 182 | |||||||||||
| 183 | =head1 AUTHOR | ||||||||||
| 184 | |||||||||||
| 185 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||||||||||
| 186 | |||||||||||
| 187 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 188 | |||||||||||
| 189 | Copyright (C) 2014 Kevin L. Esteb | ||||||||||
| 190 | |||||||||||
| 191 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 192 | the terms of the Artistic License 2.0. For details, see the full text | ||||||||||
| 193 | of the license at http://www.perlfoundation.org/artistic_license_2_0. | ||||||||||
| 194 | |||||||||||
| 195 | =cut | ||||||||||