Letter: "alle" E-Mail-Adressen per Anhaken als Empfänger
[kivitendo-erp.git] / SL / System / TaskServer.pm
index bceb8f5..a37cd03 100644 (file)
@@ -10,6 +10,8 @@ use Rose::Object::MakeMethods::Generic (
 
 use File::Slurp;
 use File::Spec::Functions qw(:ALL);
+use File::Temp;
+use Sys::Hostname ();
 
 use SL::System::Process;
 
@@ -19,6 +21,10 @@ use constant {
   ERR_PROCESS  => -2,
 };
 
+use constant PID_BASE => "users/pid";
+
+my $node_id;
+
 sub status {
   my ($self) = @_;
 
@@ -40,6 +46,12 @@ sub start {
   return $self->_run_script_command('start');
 }
 
+sub start_if_not_running {
+  my ($self) = @_;
+
+  $self->start unless $self->is_running;
+}
+
 sub stop {
   my ($self) = @_;
 
@@ -54,6 +66,14 @@ sub wake_up {
   return kill('ALRM', $pid) ? 1 : undef;
 }
 
+sub node_id {
+  return $node_id if $node_id;
+
+  $node_id = ($::lx_office_conf{task_server} // {})->{node_id} || Sys::Hostname::hostname();
+
+  return $node_id;
+}
+
 #
 # private methods
 #
@@ -61,19 +81,27 @@ sub wake_up {
 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);
+  my $exe_dir = SL::System::Process->exe_dir;
 
-  return undef unless -f $pid_file_path;
-  return join('', read_file($pid_file_path)) * 1;
+  foreach my $conf (qw(kivitendo.conf lx_office.conf kivitendo.conf.default)) {
+    my $pid_file_path = catfile(catdir($exe_dir, splitdir(PID_BASE())), "config.${conf}.pid");
+
+    return join('', read_file($pid_file_path)) * 1 if -f $pid_file_path;
+  }
 }
 
 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}`);
+  my $exe              = catfile(catdir(SL::System::Process->exe_dir, 'scripts'), 'task_server.pl');
+  my $temp_file        = File::Temp->new;
+  my $file_name        = $temp_file->filename;
+
+  $temp_file->close;
+
+  system "${exe} ${command} >> ${file_name} 2>&1";
+
+  $self->last_command_output(read_file($file_name));
 
   return $? == 0 ? 1 : undef;
 }