line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package XAS::Lib::App::Daemon; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
650
|
use Try::Tiny; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
48
|
|
6
|
1
|
|
|
1
|
|
4
|
use Pod::Usage; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
84
|
|
7
|
1
|
|
|
1
|
|
4
|
use Hash::Merge; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
28
|
|
8
|
1
|
|
|
1
|
|
4
|
use Getopt::Long; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
9
|
|
9
|
1
|
|
|
1
|
|
84
|
use XAS::Lib::Pidfile; |
|
1
|
|
|
|
|
1
|
|
|
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->{'daemon'} = FALSE; |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
$options->{'daemon'} = \$self->{daemon}; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
$options->{'cfg-file=s'} = sub { |
108
|
0
|
|
|
0
|
|
|
my $cfgfile = File($_[1]); |
109
|
0
|
|
|
|
|
|
$self->env->cfg_file($cfgfile); |
110
|
0
|
|
|
|
|
|
}; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
$options->{'pid-file=s'} = sub { |
113
|
0
|
|
|
0
|
|
|
my $pidfile = File($_[1]); |
114
|
0
|
|
|
|
|
|
$self->env->pid_file($pidfile); |
115
|
0
|
|
|
|
|
|
}; |
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
return $options; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
1; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
__END__ |