| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package XAS::Lib::App::Daemon; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
|
4
|
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
837
|
use Try::Tiny; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
45
|
|
|
6
|
1
|
|
|
1
|
|
4
|
use Pod::Usage; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
93
|
|
|
7
|
1
|
|
|
1
|
|
4
|
use Hash::Merge; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
27
|
|
|
8
|
1
|
|
|
1
|
|
8
|
use Getopt::Long; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
7
|
|
|
9
|
1
|
|
|
1
|
|
85
|
use XAS::Lib::Pidfile; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
36
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
use XAS::Class |
|
12
|
1
|
|
|
|
|
5
|
debug => 0, |
|
13
|
|
|
|
|
|
|
version => $VERSION, |
|
14
|
|
|
|
|
|
|
import => 'class CLASS', |
|
15
|
|
|
|
|
|
|
base => 'XAS::Lib::App', |
|
16
|
|
|
|
|
|
|
utils => ':process dotid', |
|
17
|
|
|
|
|
|
|
constants => 'TRUE FALSE', |
|
18
|
|
|
|
|
|
|
accessors => 'daemon pid', |
|
19
|
1
|
|
|
1
|
|
3
|
; |
|
|
1
|
|
|
|
|
2
|
|
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
22
|
|
|
|
|
|
|
# Public Methods |
|
23
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub define_signals { |
|
26
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
27
|
|
|
|
|
|
|
|
|
28
|
0
|
|
|
|
|
|
$SIG{'INT'} = \&signal_handler; |
|
29
|
0
|
|
|
|
|
|
$SIG{'QUIT'} = \&signal_handler; |
|
30
|
0
|
|
|
|
|
|
$SIG{'TERM'} = \&signal_handler; |
|
31
|
0
|
|
|
|
|
|
$SIG{'HUP'} = \&signal_handler; |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
} |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub define_pidfile { |
|
36
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
37
|
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
my $script = $self->env->script; |
|
39
|
|
|
|
|
|
|
|
|
40
|
0
|
|
|
|
|
|
$self->log->debug('entering define_pidfile()'); |
|
41
|
|
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
|
$self->{'pid'} = XAS::Lib::Pidfile->new(-pid => $$); |
|
43
|
|
|
|
|
|
|
|
|
44
|
0
|
0
|
|
|
|
|
if (my $num = $self->pid->is_running()) { |
|
45
|
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
$self->throw_msg( |
|
47
|
|
|
|
|
|
|
dotid($self->class). '.define_pidfile.runerr', |
|
48
|
|
|
|
|
|
|
'pid_run_error', |
|
49
|
|
|
|
|
|
|
$script, $num |
|
50
|
|
|
|
|
|
|
); |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
} |
|
53
|
|
|
|
|
|
|
|
|
54
|
0
|
0
|
|
|
|
|
$self->pid->write() or |
|
55
|
|
|
|
|
|
|
$self->throw_msg( |
|
56
|
|
|
|
|
|
|
dotid($self->class) . '.define_pidfile.wrterr', |
|
57
|
|
|
|
|
|
|
'pid_write_error', |
|
58
|
|
|
|
|
|
|
$self->pid->file |
|
59
|
|
|
|
|
|
|
); |
|
60
|
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
$self->log->debug('leaving define_pidfile()'); |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
} |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub define_daemon { |
|
66
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# become a daemon... |
|
69
|
|
|
|
|
|
|
# interesting, "daemonize() if ($self->daemon);" doesn't work as expected |
|
70
|
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
$self->log->debug("before pid = " . $$); |
|
72
|
|
|
|
|
|
|
|
|
73
|
0
|
0
|
|
|
|
|
if ($self->daemon) { |
|
74
|
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
daemonize(); |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
|
$self->log->debug("after pid = " . $$); |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
} |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub run { |
|
84
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
85
|
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
my $rc = $self->SUPER::run(); |
|
87
|
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
$self->pid->remove(); |
|
89
|
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
return $rc; |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
} |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
95
|
|
|
|
|
|
|
# Private Methods |
|
96
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub _default_options { |
|
99
|
0
|
|
|
0
|
|
|
my $self = shift; |
|
100
|
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
my $options = $self->SUPER::_default_options(); |
|
102
|
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
$self->env->log_type('file'); |
|
104
|
0
|
|
|
|
|
|
$self->{'daemon'} = FALSE; |
|
105
|
|
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
|
$options->{'daemon'} = \$self->{daemon}; |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
$options->{'cfg-file=s'} = sub { |
|
109
|
0
|
|
|
0
|
|
|
my $cfgfile = File($_[1]); |
|
110
|
0
|
|
|
|
|
|
$self->env->cfg_file($cfgfile); |
|
111
|
0
|
|
|
|
|
|
}; |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
$options->{'pid-file=s'} = sub { |
|
114
|
0
|
|
|
0
|
|
|
my $pidfile = File($_[1]); |
|
115
|
0
|
|
|
|
|
|
$self->env->pid_file($pidfile); |
|
116
|
0
|
|
|
|
|
|
}; |
|
117
|
|
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
|
return $options; |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
} |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
1; |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
__END__ |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 NAME |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
XAS::Lib::App::Daemon - The base class to write daemons within the XAS environment |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
use XAS::Lib::App::Daemon; |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
my $app = XAS::Lib::App::Daemon->new(); |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
$app->run(); |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
This module defines an operating environment for daemons. A daemon is a |
|
141
|
|
|
|
|
|
|
Unix background process without a controlling terminal. Windows really |
|
142
|
|
|
|
|
|
|
doesn't have a concept for this behavior. For running background jobs |
|
143
|
|
|
|
|
|
|
on Windows please see L<XAS::Lib::App::Services|XAS::Lib::App::Services>. |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
This module is also single threaded, it doesn't use POE to provide an |
|
146
|
|
|
|
|
|
|
async environment. If you need that, then see the above module. This inherits |
|
147
|
|
|
|
|
|
|
from L<XAS::Lib::App|XAS::Lib::App>. Please see that module for additional |
|
148
|
|
|
|
|
|
|
documentation. |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 METHODS |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head2 define_pidfile |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
This method sets up the pid file for the process. By default, this file |
|
155
|
|
|
|
|
|
|
is named $XAS_RUN/<$0>.pid. This can be overridden by the --pid-file option. |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head2 define_signals |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
This method sets up basic signal handling. By default this is only for the INT, |
|
160
|
|
|
|
|
|
|
TERM, HUP and QUIT signals. |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head2 define_daemon |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
This method will cause the process to become a daemon. |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 OPTIONS |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
This module handles these additional options. |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head2 --cfg-file |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
This defines an optional configuration file. |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head2 --pid-file |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
This defines the pid file for recording the pid. |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head2 --daemon |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Become a daemon. |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=over 4 |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=item L<XAS::Lib::App|XAS::Lib::App> |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=item L<XAS|XAS> |
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=back |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head1 AUTHOR |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Copyright (c) 2012-2015 Kevin L. Esteb |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
|
201
|
|
|
|
|
|
|
the terms of the Artistic License 2.0. For details, see the full text |
|
202
|
|
|
|
|
|
|
of the license at http://www.perlfoundation.org/artistic_license_2_0. |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=cut |