| 1 | package XAS::Lib::App::Services::Win32; | ||
| 2 | |||
| 3 | our $VERSION = '0.01'; | ||
| 4 | |||
| 5 | use Win32; | ||
| 6 | use Win32::Daemon; | ||
| 7 | |||
| 8 | use XAS::Class | ||
| 9 | debug => 0, | ||
| 10 | version => $VERSION, | ||
| 11 | base => 'XAS::Base', | ||
| 12 | mixins => 'define_daemon get_service_config install_service remove_service', | ||
| 13 | ; | ||
| 14 | |||
| 15 | # ---------------------------------------------------------------------- | ||
| 16 | # Public Methods | ||
| 17 | # ---------------------------------------------------------------------- | ||
| 18 | |||
| 19 | sub define_daemon { | ||
| 20 | my $self = shift; | ||
| 21 | |||
| 22 | } | ||
| 23 | |||
| 24 | sub get_service_config { | ||
| 25 | my $self = shift; | ||
| 26 | |||
| 27 | # here be dragons... the format is important, especially for the path. | ||
| 28 | |||
| 29 | my $script = Win32::GetFullPathName($0); | ||
| 30 | |||
| 31 | return { | ||
| 32 | name => $self->class->any_var('SERVICE_NAME'), | ||
| 33 | display => $self->class->any_var('SERVICE_DISPLAY_NAME'), | ||
| 34 | path => "\"$^X\" \"$script\"", | ||
| 35 | user => '', | ||
| 36 | password => '', | ||
| 37 | description => $self->class->any_var('SERVICE_DESCRIPTION') | ||
| 38 | }; | ||
| 39 | |||
| 40 | } | ||
| 41 | |||
| 42 | sub install_service { | ||
| 43 | my $self = shift; | ||
| 44 | |||
| 45 | my $config = $self->get_service_config(); | ||
| 46 | |||
| 47 | if (Win32::Daemon::CreateService($config)) { | ||
| 48 | |||
| 49 | printf("%s\n", $self->message('installed')); | ||
| 50 | |||
| 51 | } else { | ||
| 52 | |||
| 53 | printf("%s\n", $self->message('failed', 'install', _get_error())); | ||
| 54 | |||
| 55 | } | ||
| 56 | |||
| 57 | } | ||
| 58 | |||
| 59 | sub remove_service { | ||
| 60 | my $self = shift; | ||
| 61 | |||
| 62 | my $config = $self->get_service_config(); | ||
| 63 | |||
| 64 | if (Win32::Daemon::DeleteService($config->{name})) { | ||
| 65 | |||
| 66 | printf("%s\n", $self->message('removed')); | ||
| 67 | |||
| 68 | } else { | ||
| 69 | |||
| 70 | printf("%s\n", $self->message('failed', 'remove', _get_error())); | ||
| 71 | |||
| 72 | } | ||
| 73 | |||
| 74 | } | ||
| 75 | |||
| 76 | |||
| 77 | # ---------------------------------------------------------------------- | ||
| 78 | # Private Methods | ||
| 79 | # ---------------------------------------------------------------------- | ||
| 80 | |||
| 81 | sub _get_error { | ||
| 82 | |||
| 83 | return(Win32::FormatMessage(Win32::Daemon::GetLastError())); | ||
| 84 | |||
| 85 | } | ||
| 86 | |||
| 87 | 1; | ||
| 88 | |||
| 89 | __END__ | ||
| 90 | |||
| 91 | =head1 NAME | ||
| 92 | |||
| 93 | XAS::Lib::App::Services::Win32 - A mixin class for Win32 Services | ||
| 94 | |||
| 95 | =head1 DESCRIPTION | ||
| 96 | |||
| 97 | This module provides a mixin class to define the necessary functionality for | ||
| 98 | a Service to run on Win32. | ||
| 99 | |||
| 100 | =head1 METHODS | ||
| 101 | |||
| 102 | =head2 define_daemon | ||
| 103 | |||
| 104 | This method does nothing on Win32. | ||
| 105 | |||
| 106 | =head2 get_service_config | ||
| 107 | |||
| 108 | This method defines how the service is configured. This is used with the | ||
| 109 | --install and --deinstall command line options. See | ||
| 110 | L<Win32::Daemon|https://metacpan.org/pod/Win32::Daemon> for more details. | ||
| 111 | |||
| 112 | =head2 install_service | ||
| 113 | |||
| 114 | This method will install the service with the SCM. | ||
| 115 | |||
| 116 | =head2 remove_service | ||
| 117 | |||
| 118 | This method will deinstall the service for the SCM. | ||
| 119 | |||
| 120 | =head1 SEE ALSO | ||
| 121 | |||
| 122 | =over 4 | ||
| 123 | |||
| 124 | =item L<XAS|XAS> | ||
| 125 | |||
| 126 | =back | ||
| 127 | |||
| 128 | =head1 AUTHOR | ||
| 129 | |||
| 130 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||
| 131 | |||
| 132 | =head1 COPYRIGHT AND LICENSE | ||
| 133 | |||
| 134 | Copyright (C) 2014 Kevin L. Esteb | ||
| 135 | |||
| 136 | This library is free software; you can redistribute it and/or modify | ||
| 137 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 138 | at your option, any later version of Perl 5 you may have available. | ||
| 139 | |||
| 140 | See L<http://dev.perl.org/licenses/> for more information. | ||
| 141 | |||
| 142 | =cut |