From 347e2834f9e018939ffe11ccc646ef61326a46e0 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Mon, 18 Sep 2023 11:06:35 +0200
Subject: [PATCH] add running jobs

---
 classes/cronlog-renderer.class.php |  6 +++---
 classes/cronlog.class.php          | 34 ++++++++++++++++++++++++------
 2 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/classes/cronlog-renderer.class.php b/classes/cronlog-renderer.class.php
index a1f6885..82f5975 100644
--- a/classes/cronlog-renderer.class.php
+++ b/classes/cronlog-renderer.class.php
@@ -111,7 +111,7 @@ class cronlogrenderer extends cronlog{
         $sHtml='';
         
         if(!$aData){
-            $aData=$this->getServersLastLog();
+            $aData=array_merge($this->getRunningJobs(), $this->getServersLastLog());
         }
         $sTblHead='';
         $iOK=0;
@@ -201,7 +201,7 @@ class cronlogrenderer extends cronlog{
                 }
                 $sColStatus='<ul>'.$sColStatus.'</ul>';
             } else {
-                $sColStatus.=$this->t('status-ok');
+                $sColStatus.=($aEntry['SCRIPTRC'] ? $this->t('status-ok') : '');
             }
             // render table of last logfile per cron job
             $sHtml.='<tr onclick="showFile(\''.$aEntry['logfile'].'\');" title="'.sprintf($this->t('row-click-show-logfile'), $aEntry['logfile']).'">'
@@ -224,7 +224,7 @@ class cronlogrenderer extends cronlog{
                         .$sTtlHr
                     .'</td>'
                     . '<td class="'.($aEntry['SCRIPTRC']>0 ? 'message-error' : 'message-ok' ).'">'
-                        .$aEntry['SCRIPTRC']
+                        .($aEntry['SCRIPTRC'] ? $aEntry['SCRIPTRC'] : '⏳')
                         .'</td>'
                     . '<td class="'.($iNextRunWarn < date("U") 
                             ?  ($iNextRunErr < date("U") ? 'message-error' : 'message-warning')
diff --git a/classes/cronlog.class.php b/classes/cronlog.class.php
index fb75167..c05cbff 100644
--- a/classes/cronlog.class.php
+++ b/classes/cronlog.class.php
@@ -53,7 +53,8 @@ class cronlog {
     protected $_sActiveServer = false;
     
     protected $_sFileFilter_serverjoblog = '*joblog*.done';
-    protected $_sFileFilter_serverlog = '*.log';
+    protected $_sFileFilter_joblog = '*.log';
+    protected $_sFileFilter_jobrunning = '*.log.running';
 
     protected $_sLang = ''; // language ... read from config file
     protected $_aLang = []; // language data
@@ -101,7 +102,7 @@ class cronlog {
     
     /**
      * chaching: get the full path of directory for caching
-     * @return type
+     * @return string
      */
     protected function _getCacheDir(){
         return $this->_sDataDir.'/__cache';
@@ -109,8 +110,8 @@ class cronlog {
 
     /**
      * caching: get full path of a caching item
-     * @param type $sTaskId
-     * @return type
+     * @param string $sTaskId
+     * @return string
      */
     protected function _getCacheFile($sTaskId){
         return $this->_getCacheDir().'/'.$sTaskId;
@@ -118,7 +119,7 @@ class cronlog {
 
     /**
      * read logs: get full path to a servers cronjob logdata
-     * @return type
+     * @return string
      */
     protected function _getServerlogDir(){
         return $this->_sDataDir.'/'.$this->_sActiveServer;
@@ -127,7 +128,7 @@ class cronlog {
     /**
      * caching: get cached data if they exist and aren't expired
      * @param  string  $sTaskId
-     * @return boolean|array
+     * @return mixed boolean|array
      */
     protected function _getCacheData($sTaskId){
         // DISABLE CACHE return false;
@@ -298,7 +299,24 @@ class cronlog {
     public function getServersLastLog(){
         $aReturn=array();
         $aData=array();
-        foreach(glob($this->_getServerlogDir().'/'.$this->_sFileFilter_serverlog) as $sMyJobfile){
+        foreach(glob($this->_getServerlogDir().'/'.$this->_sFileFilter_joblog) as $sMyJobfile){
+            // echo "DEBUG: log file $sMyJobfile<br>";
+            $aData=$this->_parseLogfile($sMyJobfile);
+            $aData['server']=$this->_sActiveServer;
+            $aData['logfile']= $this->_sActiveServer.'/'.basename($sMyJobfile);
+            $aReturn[$aData['SCRIPTSTARTTIME'].$sMyJobfile]=$aData;
+        }
+        rsort($aReturn);
+        return $aReturn;
+    }
+    /**
+     * get logs from jobfilea of the current or given server
+     * @return array
+     */
+    public function getRunningJobs(){
+        $aReturn=array();
+        $aData=array();
+        foreach(glob($this->_getServerlogDir().'/'.$this->_sFileFilter_jobrunning) as $sMyJobfile){
             // echo "DEBUG: log file $sMyJobfile<br>";
             $aData=$this->_parseLogfile($sMyJobfile);
             $aData['server']=$this->_sActiveServer;
@@ -311,6 +329,8 @@ class cronlog {
     
     /**
      * translate ... get a language specific text of a given key
+     * @param  string  $id  id of language text
+     * @return string
      */
     public function t($id){
         return ''
-- 
GitLab