| 1 | package XAS::Lib::App::Services; | ||
| 2 | |||
| 3 | our $VERSION = '0.01'; | ||
| 4 | |||
| 5 | my $mixin; | ||
| 6 | BEGIN { | ||
| 7 | $mixin = ($^O eq 'MSWin32') | ||
| 8 | ? 'XAS::Lib::App::Services::Win32' | ||
| 9 | : 'XAS::Lib::App::Services::Unix'; | ||
| 10 | } | ||
| 11 | |||
| 12 | use Try::Tiny; | ||
| 13 | use File::Pid; | ||
| 14 | use Pod::Usage; | ||
| 15 | use XAS::Lib::Services; | ||
| 16 | |||
| 17 | use XAS::Class | ||
| 18 | debug => 0, | ||
| 19 | version => $VERSION, | ||
| 20 | base => 'XAS::Lib::App', | ||
| 21 | mixin => $mixin, | ||
| 22 | constants => 'TRUE FALSE', | ||
| 23 | filesystem => 'File', | ||
| 24 | accessors => 'daemon service pid', | ||
| 25 | ; | ||
| 26 | |||
| 27 | # ---------------------------------------------------------------------- | ||
| 28 | # Public Methods | ||
| 29 | # ---------------------------------------------------------------------- | ||
| 30 | |||
| 31 | sub define_signals { | ||
| 32 | my $self = shift; | ||
| 33 | |||
| 34 | } | ||
| 35 | |||
| 36 | sub run { | ||
| 37 | my $self = shift; | ||
| 38 | |||
| 39 | my $rc = $self->SUPER::run(); | ||
| 40 | |||
| 41 | $self->pid->remove() if ($self->env->pidfile->exists); | ||
| 42 | |||
| 43 | return $rc; | ||
| 44 | |||
| 45 | } | ||
| 46 | |||
| 47 | # ---------------------------------------------------------------------- | ||
| 48 | # Private Methods | ||
| 49 | # ---------------------------------------------------------------------- | ||
| 50 | |||
| 51 | sub init { | ||
| 52 | my $class = shift; | ||
| 53 | |||
| 54 | my $self = $class->SUPER::init(@_); | ||
| 55 | |||
| 56 | $self->{service} = XAS::Lib::Services->new( | ||
| 57 | -alias => 'services' | ||
| 58 | ); | ||
| 59 | |||
| 60 | return $self; | ||
| 61 | |||
| 62 | } | ||
| 63 | |||
| 64 | sub _default_options { | ||
| 65 | my $self = shift; | ||
| 66 | |||
| 67 | my $options = $self->SUPER::_default_options(); | ||
| 68 | |||
| 69 | $self->{daemon} = FALSE; | ||
| 70 | |||
| 71 | $options->{'daemon'} = \$self->{daemon}; | ||
| 72 | |||
| 73 | $options->{'install'} = sub { | ||
| 74 | $self->install_service(); | ||
| 75 | exit 0; | ||
| 76 | }; | ||
| 77 | |||
| 78 | $options->{'deinstall'} = sub { | ||
| 79 | $self->remove_service(); | ||
| 80 | exit 0; | ||
| 81 | }; | ||
| 82 | |||
| 83 | $options->{'pidfile=s'} = sub { | ||
| 84 | my $pidfile = File($_[1]); | ||
| 85 | $self->env->pidfile($pidfile); | ||
| 86 | }; | ||
| 87 | |||
| 88 | $options->{'cfgfile=s'} = sub { | ||
| 89 | my $cfgfile = File($_[1]); | ||
| 90 | $self->env->cfgfile($cfgfile); | ||
| 91 | }; | ||
| 92 | |||
| 93 | return $options; | ||
| 94 | |||
| 95 | } | ||
| 96 | |||
| 97 | 1; | ||
| 98 | |||
| 99 | __END__ | ||
| 100 | |||
| 101 | =head1 NAME | ||
| 102 | |||
| 103 | XAS::Lib::App::Services - The base class to write services within the XAS environment | ||
| 104 | |||
| 105 | =head1 SYNOPSIS | ||
| 106 | |||
| 107 | use XAS::Lib::App::Services; | ||
| 108 | |||
| 109 | my $service = XAS::Lib::App::Services->new(); | ||
| 110 | |||
| 111 | $service->run(); | ||
| 112 | |||
| 113 | =head1 DESCRIPTION | ||
| 114 | |||
| 115 | This module defines an operating environment for Services. A service is a | ||
| 116 | managed daemon. They behave differently depending on what platform they | ||
| 117 | are running on. On Windows, they will run under the SCM, on Unix like boxes, | ||
| 118 | they may be standalone daemons. These differences are handled by mixins. | ||
| 119 | |||
| 120 | The proper mixin is loaded when the process starts, so all the interaction | ||
| 121 | happens in the background. It inherits from L<XAS::Lib::App|XAS::Lib::App>. Please see | ||
| 122 | that module for additional documentation. | ||
| 123 | |||
| 124 | =head1 OPTIONS | ||
| 125 | |||
| 126 | This module handles these additional options. | ||
| 127 | |||
| 128 | =head2 B<--cfgfile> | ||
| 129 | |||
| 130 | This defines a configuration file. | ||
| 131 | |||
| 132 | =head2 B<--pidfile> | ||
| 133 | |||
| 134 | This defines the pid file to use. | ||
| 135 | |||
| 136 | =head2 B<--install> | ||
| 137 | |||
| 138 | This will install the service with the Win32 SCM. | ||
| 139 | |||
| 140 | =head2 B<--deinstall> | ||
| 141 | |||
| 142 | This will deinstall the service from the Win32 SCM. | ||
| 143 | |||
| 144 | =head1 SEE ALSO | ||
| 145 | |||
| 146 | =over 4 | ||
| 147 | |||
| 148 | =item L<XAS|XAS> | ||
| 149 | |||
| 150 | =back | ||
| 151 | |||
| 152 | =head1 AUTHOR | ||
| 153 | |||
| 154 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||
| 155 | |||
| 156 | =head1 COPYRIGHT AND LICENSE | ||
| 157 | |||
| 158 | Copyright (C) 2014 Kevin L. Esteb | ||
| 159 | |||
| 160 | This library is free software; you can redistribute it and/or modify | ||
| 161 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 162 | at your option, any later version of Perl 5 you may have available. | ||
| 163 | |||
| 164 | See L<http://dev.perl.org/licenses/> for more information. | ||
| 165 | |||
| 166 | =cut |