Task-Server: PID-File nur aus relativem Konfigurationsdateinamen ableiten
[kivitendo-erp.git] / SL / System / TaskServer.pm
1 package SL::System::TaskServer;
2
3 use strict;
4
5 use parent qw(Rose::Object);
6
7 use Rose::Object::MakeMethods::Generic (
8   scalar => [ qw(last_command_output) ],
9 );
10
11 use File::Slurp;
12 use File::Spec::Functions qw(:ALL);
13 use File::Temp qw(tempfile);
14
15 use SL::System::Process;
16
17 use constant {
18   OK           =>  0,
19   ERR_PID_FILE => -1,
20   ERR_PROCESS  => -2,
21 };
22
23 use constant PID_BASE => "users/pid";
24
25 sub status {
26   my ($self) = @_;
27
28   my $pid = $self->_read_pid;
29   return ERR_PID_FILE unless $pid;
30
31   return kill(0, $pid) ? OK : ERR_PROCESS;
32 }
33
34 sub is_running {
35   my ($self) = @_;
36
37   return $self->status == OK;
38 }
39
40 sub start {
41   my ($self) = @_;
42
43   return $self->_run_script_command('start');
44 }
45
46 sub stop {
47   my ($self) = @_;
48
49   return $self->_run_script_command('stop');
50 }
51
52 sub wake_up {
53   my ($self) = @_;
54
55   my $pid = $self->_read_pid;
56   return undef unless $pid;
57   return kill('ALRM', $pid) ? 1 : undef;
58 }
59
60 #
61 # private methods
62 #
63
64 sub _read_pid {
65   my ($self) = @_;
66
67   my $exe_dir = SL::System::Process->exe_dir;
68
69   foreach my $conf (qw(kivitendo.conf lx_office.conf kivitendo.conf.default)) {
70     my $pid_file_path = catfile(catdir($exe_dir, splitdir(PID_BASE())), "config.${conf}.pid");
71
72     return join('', read_file($pid_file_path)) * 1 if -f $pid_file_path;
73   }
74 }
75
76 sub _run_script_command {
77   my ($self, $command) = @_;
78
79   my ($fh, $file_name) = tempfile();
80   my $exe              = catfile(catdir(SL::System::Process->exe_dir, 'scripts'), 'task_server.pl');
81
82   system "${exe} ${command} >> ${file_name} 2>&1";
83
84   $fh->close;
85
86   $self->last_command_output(read_file($file_name));
87
88   return $? == 0 ? 1 : undef;
89 }
90
91 1;
92 __END__
93
94 =pod
95
96 =encoding utf8
97
98 =head1 NAME
99
100 SL::System::TaskServer - programmatic interface to the external task server component
101
102 =head1 SYNOPSIS
103
104   # Create interface
105   my $task_server = SL->TaskServer->new;
106
107   # Start the server if it is not running
108   if (!$task_server->is_running) {
109     $task_server->start;
110   }
111
112   # Stop it if it is running
113   if ($task_server->is_running) {
114     $task_server->stop;
115   }
116
117 =head1 FUNCTIONS
118
119 =over 4
120
121 =item C<is_running>
122
123 Returns C<trueish> if the server is running. This is done by using
124 Perl's C<kill> function with a "signal" of C<0> for the process ID
125 which in turn is read from the daemon's PID file.
126
127 If the PID file is not found or if C<kill> returns a non-successful
128 value then a C<falsish> value is returned.
129
130 =item C<last_command_output>
131
132 Returns the output of the last C<system> command executed, e.g. from a
133 call to L<start> or L<stop>.
134
135 =item C<start>
136
137 Starts the task server. Does not check whether or not it is running,
138 neither before not after trying to start it.
139
140 Returns C<1> if the system command C<./scripts/task_server.pl start>
141 exits with an exit code of C<0> and C<undef> otherwise.
142
143 The command's output can be queried with L<last_command_output>.
144
145 =item C<status>
146
147 Queries the task server status. Returns one of these values:
148
149 =over 4
150
151 =item *
152
153 C<OK> or C<0>: the task server is running and signals can be sent to
154 it.
155
156 =item *
157
158 C<ERR_PID_FILE> or C<-1>: the PID file could not be found or read
159
160 =item *
161
162 C<ERR_PROCESS> or C<-2>: the PID file could was found and read, but
163 it's not possible to send signals to the process, e.g. because it is
164 not running or owned by a different user ID.
165
166 =back
167
168 =item C<stop>
169
170 Stops the task server. Does not check whether or not it is running,
171 neither before not after trying to start it.
172
173 Returns C<1> if the system command C<./scripts/task_server.pl stop>
174 exits with an exit code of C<0> and C<undef> otherwise.
175
176 The command's output can be queried with L<last_command_output>.
177
178 =item C<wake_up>
179
180 Sends a signal to the task server process causing it to wake up and
181 process its job queue immediately.
182
183 Returns C<1> if the signal could be sent and C<undef> otherwise.
184
185 =back
186
187 =head1 BUGS
188
189 Nothing here yet.
190
191 =head1 AUTHOR
192
193 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
194
195 =cut