1 package SL::System::TaskServer;
5 use parent qw(Rose::Object);
7 use Rose::Object::MakeMethods::Generic (
8 scalar => [ qw(last_command_output) ],
12 use File::Spec::Functions qw(:ALL);
16 use SL::System::Process;
24 use constant PID_BASE => "users/pid";
31 my $pid = $self->_read_pid;
32 return ERR_PID_FILE unless $pid;
34 return kill(0, $pid) ? OK : ERR_PROCESS;
40 return $self->status == OK;
46 return $self->_run_script_command('start');
49 sub start_if_not_running {
52 $self->start unless $self->is_running;
58 return $self->_run_script_command('stop');
64 my $pid = $self->_read_pid;
65 return undef unless $pid;
66 return kill('ALRM', $pid) ? 1 : undef;
70 return $node_id if $node_id;
72 $node_id = ($::lx_office_conf{task_server} // {})->{node_id} || Sys::Hostname::hostname();
84 my $exe_dir = SL::System::Process->exe_dir;
86 foreach my $conf (qw(kivitendo.conf lx_office.conf kivitendo.conf.default)) {
87 my $pid_file_path = catfile(catdir($exe_dir, splitdir(PID_BASE())), "config.${conf}.pid");
89 return join('', read_file($pid_file_path)) * 1 if -f $pid_file_path;
93 sub _run_script_command {
94 my ($self, $command) = @_;
96 my $exe = catfile(catdir(SL::System::Process->exe_dir, 'scripts'), 'task_server.pl');
97 my $temp_file = File::Temp->new;
98 my $file_name = $temp_file->filename;
102 system "${exe} ${command} >> ${file_name} 2>&1";
104 $self->last_command_output(read_file($file_name));
106 return $? == 0 ? 1 : undef;
118 SL::System::TaskServer - programmatic interface to the external task server component
123 my $task_server = SL->TaskServer->new;
125 # Start the server if it is not running
126 if (!$task_server->is_running) {
130 # Stop it if it is running
131 if ($task_server->is_running) {
141 Returns C<trueish> if the server is running. This is done by using
142 Perl's C<kill> function with a "signal" of C<0> for the process ID
143 which in turn is read from the daemon's PID file.
145 If the PID file is not found or if C<kill> returns a non-successful
146 value then a C<falsish> value is returned.
148 =item C<last_command_output>
150 Returns the output of the last C<system> command executed, e.g. from a
151 call to L<start> or L<stop>.
155 Starts the task server. Does not check whether or not it is running,
156 neither before not after trying to start it.
158 Returns C<1> if the system command C<./scripts/task_server.pl start>
159 exits with an exit code of C<0> and C<undef> otherwise.
161 The command's output can be queried with L<last_command_output>.
165 Queries the task server status. Returns one of these values:
171 C<OK> or C<0>: the task server is running and signals can be sent to
176 C<ERR_PID_FILE> or C<-1>: the PID file could not be found or read
180 C<ERR_PROCESS> or C<-2>: the PID file could was found and read, but
181 it's not possible to send signals to the process, e.g. because it is
182 not running or owned by a different user ID.
188 Stops the task server. Does not check whether or not it is running,
189 neither before not after trying to start it.
191 Returns C<1> if the system command C<./scripts/task_server.pl stop>
192 exits with an exit code of C<0> and C<undef> otherwise.
194 The command's output can be queried with L<last_command_output>.
198 Sends a signal to the task server process causing it to wake up and
199 process its job queue immediately.
201 Returns C<1> if the signal could be sent and C<undef> otherwise.
211 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>