| 1 | package XAS::Lib::App::Service::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('service_installed')); | ||
| 50 | |||
| 51 | } else { | ||
| 52 | |||
| 53 | printf("%s\n", $self->message('service_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('service_removed')); | ||
| 67 | |||
| 68 | } else { | ||
| 69 | |||
| 70 | printf("%s\n", $self->message('service_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::Service::Win32 - A mixin class for Win32 Services | ||
| 94 | |||
| 95 | =head1 SYNOPSIS | ||
| 96 | |||
| 97 | use XAS::Class | ||
| 98 | debug => 0, | ||
| 99 | version => $VERSION, | ||
| 100 | base => 'XAS::Lib::App::Service', | ||
| 101 | mixin => 'XAS::Lib::App::Service::Win32', | ||
| 102 | constants => 'TRUE FALSE', | ||
| 103 | filesystem => 'File', | ||
| 104 | accessors => 'daemon service pid', | ||
| 105 | ; | ||
| 106 | |||
| 107 | =head1 DESCRIPTION | ||
| 108 | |||
| 109 | This module provides a mixin class to define the necessary functionality for | ||
| 110 | a Service to run on Win32. | ||
| 111 | |||
| 112 | =head1 METHODS | ||
| 113 | |||
| 114 | =head2 define_daemon | ||
| 115 | |||
| 116 | This method does nothing on Win32. | ||
| 117 | |||
| 118 | =head2 get_service_config | ||
| 119 | |||
| 120 | This method defines how the service is configured. This is used with the | ||
| 121 | --install and --deinstall command line options. See | ||
| 122 | L<Win32::Daemon|https://metacpan.org/pod/Win32::Daemon> for more details. | ||
| 123 | |||
| 124 | =head2 install_service | ||
| 125 | |||
| 126 | This method will install the service with the SCM. | ||
| 127 | |||
| 128 | =head2 remove_service | ||
| 129 | |||
| 130 | This method will deinstall the service for the SCM. | ||
| 131 | |||
| 132 | =head1 SEE ALSO | ||
| 133 | |||
| 134 | =over 4 | ||
| 135 | |||
| 136 | =item L<XAS::Lib::App::Service|XAS::Lib::App::Service> | ||
| 137 | |||
| 138 | =item L<XAS|XAS> | ||
| 139 | |||
| 140 | =back | ||
| 141 | |||
| 142 | =head1 AUTHOR | ||
| 143 | |||
| 144 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||
| 145 | |||
| 146 | =head1 COPYRIGHT AND LICENSE | ||
| 147 | |||
| 148 | Copyright (C) 2014 Kevin L. Esteb | ||
| 149 | |||
| 150 | This is free software; you can redistribute it and/or modify it under | ||
| 151 | the terms of the Artistic License 2.0. For details, see the full text | ||
| 152 | of the license at http://www.perlfoundation.org/artistic_license_2_0. | ||
| 153 | |||
| 154 | =cut |