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);
 
  13 use File::Temp qw(tempfile);
 
  15 use SL::System::Process;
 
  23 use constant PID_BASE => "users/pid";
 
  28   my $pid = $self->_read_pid;
 
  29   return ERR_PID_FILE unless $pid;
 
  31   return kill(0, $pid) ? OK : ERR_PROCESS;
 
  37   return $self->status == OK;
 
  43   return $self->_run_script_command('start');
 
  46 sub start_if_not_running {
 
  49   $self->start unless $self->is_running;
 
  55   return $self->_run_script_command('stop');
 
  61   my $pid = $self->_read_pid;
 
  62   return undef unless $pid;
 
  63   return kill('ALRM', $pid) ? 1 : undef;
 
  73   my $exe_dir = SL::System::Process->exe_dir;
 
  75   foreach my $conf (qw(kivitendo.conf lx_office.conf kivitendo.conf.default)) {
 
  76     my $pid_file_path = catfile(catdir($exe_dir, splitdir(PID_BASE())), "config.${conf}.pid");
 
  78     return join('', read_file($pid_file_path)) * 1 if -f $pid_file_path;
 
  82 sub _run_script_command {
 
  83   my ($self, $command) = @_;
 
  85   my ($fh, $file_name) = tempfile();
 
  86   my $exe              = catfile(catdir(SL::System::Process->exe_dir, 'scripts'), 'task_server.pl');
 
  88   system "${exe} ${command} >> ${file_name} 2>&1";
 
  92   $self->last_command_output(read_file($file_name));
 
  94   return $? == 0 ? 1 : undef;
 
 106 SL::System::TaskServer - programmatic interface to the external task server component
 
 111   my $task_server = SL->TaskServer->new;
 
 113   # Start the server if it is not running
 
 114   if (!$task_server->is_running) {
 
 118   # Stop it if it is running
 
 119   if ($task_server->is_running) {
 
 129 Returns C<trueish> if the server is running. This is done by using
 
 130 Perl's C<kill> function with a "signal" of C<0> for the process ID
 
 131 which in turn is read from the daemon's PID file.
 
 133 If the PID file is not found or if C<kill> returns a non-successful
 
 134 value then a C<falsish> value is returned.
 
 136 =item C<last_command_output>
 
 138 Returns the output of the last C<system> command executed, e.g. from a
 
 139 call to L<start> or L<stop>.
 
 143 Starts the task server. Does not check whether or not it is running,
 
 144 neither before not after trying to start it.
 
 146 Returns C<1> if the system command C<./scripts/task_server.pl start>
 
 147 exits with an exit code of C<0> and C<undef> otherwise.
 
 149 The command's output can be queried with L<last_command_output>.
 
 153 Queries the task server status. Returns one of these values:
 
 159 C<OK> or C<0>: the task server is running and signals can be sent to
 
 164 C<ERR_PID_FILE> or C<-1>: the PID file could not be found or read
 
 168 C<ERR_PROCESS> or C<-2>: the PID file could was found and read, but
 
 169 it's not possible to send signals to the process, e.g. because it is
 
 170 not running or owned by a different user ID.
 
 176 Stops the task server. Does not check whether or not it is running,
 
 177 neither before not after trying to start it.
 
 179 Returns C<1> if the system command C<./scripts/task_server.pl stop>
 
 180 exits with an exit code of C<0> and C<undef> otherwise.
 
 182 The command's output can be queried with L<last_command_output>.
 
 186 Sends a signal to the task server process causing it to wake up and
 
 187 process its job queue immediately.
 
 189 Returns C<1> if the signal could be sent and C<undef> otherwise.
 
 199 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>