From 91bbaadd9be01ec1401a953147119791da7daa9e Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Wed, 8 Aug 2012 16:54:35 +0200 Subject: [PATCH] Implementation eines programmatischen Interfaces zum Task-Server --- SL/System/TaskServer.pm | 185 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 SL/System/TaskServer.pm diff --git a/SL/System/TaskServer.pm b/SL/System/TaskServer.pm new file mode 100644 index 000000000..bceb8f598 --- /dev/null +++ b/SL/System/TaskServer.pm @@ -0,0 +1,185 @@ +package SL::System::TaskServer; + +use strict; + +use parent qw(Rose::Object); + +use Rose::Object::MakeMethods::Generic ( + scalar => [ qw(last_command_output) ], +); + +use File::Slurp; +use File::Spec::Functions qw(:ALL); + +use SL::System::Process; + +use constant { + OK => 0, + ERR_PID_FILE => -1, + ERR_PROCESS => -2, +}; + +sub status { + my ($self) = @_; + + my $pid = $self->_read_pid; + return ERR_PID_FILE unless $pid; + + return kill(0, $pid) ? OK : ERR_PROCESS; +} + +sub is_running { + my ($self) = @_; + + return $self->status == OK; +} + +sub start { + my ($self) = @_; + + return $self->_run_script_command('start'); +} + +sub stop { + my ($self) = @_; + + return $self->_run_script_command('stop'); +} + +sub wake_up { + my ($self) = @_; + + my $pid = $self->_read_pid; + return undef unless $pid; + return kill('ALRM', $pid) ? 1 : undef; +} + +# +# private methods +# + +sub _read_pid { + my ($self) = @_; + + my $exe_dir = SL::System::Process->exe_dir; + my $pid_file_name = join '.', splitdir($exe_dir), 'config.lx_office.conf.pid'; + my $pid_file_path = catfile(catdir($exe_dir, 'users', 'pid'), $pid_file_name); + + return undef unless -f $pid_file_path; + return join('', read_file($pid_file_path)) * 1; +} + +sub _run_script_command { + my ($self, $command) = @_; + + my $exe = catfile(catdir(SL::System::Process->exe_dir, 'scripts'), 'task_server.pl'); + $self->last_command_output(`${exe} ${command}`); + + return $? == 0 ? 1 : undef; +} + +1; +__END__ + +=pod + +=encoding utf8 + +=head1 NAME + +SL::System::TaskServer - programmatic interface to the external task server component + +=head1 SYNOPSIS + + # Create interface + my $task_server = SL->TaskServer->new; + + # Start the server if it is not running + if (!$task_server->is_running) { + $task_server->start; + } + + # Stop it if it is running + if ($task_server->is_running) { + $task_server->stop; + } + +=head1 FUNCTIONS + +=over 4 + +=item C + +Returns C if the server is running. This is done by using +Perl's C function with a "signal" of C<0> for the process ID +which in turn is read from the daemon's PID file. + +If the PID file is not found or if C returns a non-successful +value then a C value is returned. + +=item C + +Returns the output of the last C command executed, e.g. from a +call to L or L. + +=item C + +Starts the task server. Does not check whether or not it is running, +neither before not after trying to start it. + +Returns C<1> if the system command C<./scripts/task_server.pl start> +exits with an exit code of C<0> and C otherwise. + +The command's output can be queried with L. + +=item C + +Queries the task server status. Returns one of these values: + +=over 4 + +=item * + +C or C<0>: the task server is running and signals can be sent to +it. + +=item * + +C or C<-1>: the PID file could not be found or read + +=item * + +C or C<-2>: the PID file could was found and read, but +it's not possible to send signals to the process, e.g. because it is +not running or owned by a different user ID. + +=back + +=item C + +Stops the task server. Does not check whether or not it is running, +neither before not after trying to start it. + +Returns C<1> if the system command C<./scripts/task_server.pl stop> +exits with an exit code of C<0> and C otherwise. + +The command's output can be queried with L. + +=item C + +Sends a signal to the task server process causing it to wake up and +process its job queue immediately. + +Returns C<1> if the signal could be sent and C otherwise. + +=back + +=head1 BUGS + +Nothing here yet. + +=head1 AUTHOR + +Moritz Bunkus Em.bunkus@linet-services.deE + +=cut -- 2.20.1