From a5cf177025d12c7419665a0730f315e74555fe49 Mon Sep 17 00:00:00 2001 From: lmarcouiller Date: Fri, 11 Dec 2020 15:22:29 +0100 Subject: [PATCH 1/3] Close #15729 : first push --- htdocs/core/boxes/box_scheduled_jobs.php | 187 ++++++++++++++++++ htdocs/core/modules/modCron.class.php | 4 +- .../install/mysql/migration/12.0.0-13.0.0.sql | 4 +- htdocs/langs/en_US/boxes.lang | 1 + htdocs/langs/en_US/cron.lang | 6 +- 5 files changed, 199 insertions(+), 3 deletions(-) create mode 100644 htdocs/core/boxes/box_scheduled_jobs.php diff --git a/htdocs/core/boxes/box_scheduled_jobs.php b/htdocs/core/boxes/box_scheduled_jobs.php new file mode 100644 index 00000000000..21c0019d525 --- /dev/null +++ b/htdocs/core/boxes/box_scheduled_jobs.php @@ -0,0 +1,187 @@ + + * Copyright (C) 2005-2017 Laurent Destailleur + * Copyright (C) 2005-2011 Regis Houssin + * Copyright (C) 2017 Nicolas Zabouri + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * \file htdocs/core/boxes/box_scheduled_jobs.php + * \ingroup task + * \brief Widget of scheduled jobs + */ + +include_once DOL_DOCUMENT_ROOT . '/core/boxes/modules_boxes.php'; + + +/** + * Class to manage the box to show last contracted products/services lines + */ +class box_scheduled_jobs extends ModeleBoxes +{ + public $boxcode = "scheduledjobs"; + public $boximg = "object_cron"; + public $boxlabel = "BoxScheduledJobs"; + public $depends = array("cron"); + + /** + * @var DoliDB Database handler. + */ + public $db; + + public $param; + + public $info_box_head = array(); + public $info_box_contents = array(); + + + /** + * Constructor + * + * @param DoliDB $db Database handler + * @param string $param More parameters + */ + public function __construct($db, $param) + { + global $user; + + $this->db = $db; + + $this->hidden = !($user->rights->service->lire && $user->rights->contrat->lire); + } + + /** + * Load data into info_box_contents array to show array later. + * + * @param int $max Maximum number of records to load + * @return void + */ + public function loadBox($max = 5) + { + global $user, $langs, $conf; + $this->max = $max; + $langs->load("cron"); + $this->info_box_head = array('text' => $langs->trans("BoxScheduledJobs", $max)); + + if ($user->rights->cron->read) { + include_once DOL_DOCUMENT_ROOT . '/cron/class/cronjob.class.php'; + $cronstatic = new Cronjob($this->db); + $nomUrlArray; + + $result = 0; + $sql = "SELECT t.rowid, t.datelastrun, t.datenextrun"; + $sql .= ", t.label, t.status, t.lastresult"; + $sql .= " FROM " . MAIN_DB_PREFIX . "cronjob as t"; + $sql .= $this->db->order("t.datelastrun", "DESC"); + + $result = $this->db->query($sql); + $line = 0; + $nbjobsinerror = 0; + if ($result) { + $num = $this->db->num_rows($result); + $i = 1; + $objp = $this->db->fetch_object($result); + $cronstatic->id = $objp->rowid; + $cronstatic->ref = $objp->rowid; + $cronstatic->status = $objp->status; + $cronstatic->datenextrun = $objp->datenextrun; + $cronstatic->datelastrun = $objp->datelastrun; + while ($i < $num) { + if ($line == 0) { + $resultarray[$line] = array( + $langs->trans("LastExecutedScheduledJob"), + $cronstatic->getNomUrl(1), + $this->db->jdate($cronstatic->datelastrun), + $cronstatic->status + ); + $line++; + } else { + $objp = $this->db->fetch_object($result); + if ($objp->datenextrun < $cronstatic->datenextrun) { + $cronstatic->id = $objp->rowid; + $cronstatic->ref = $objp->rowid; + $cronstatic->status = $objp->status; + $cronstatic->datenextrun = $objp->datenextrun; + } + } + if ($obj->lastresult < 0) { + $nbjobsinerror++; + } + $i++; + } + $resultarray[$line] = array( + $langs->trans("NextScheduledJobExecute"), + $cronstatic->getNomUrl(1), + $this->db->jdate($cronstatic->datenextrun), + $cronstatic->status + ); + $line = 0; + while ($line < 2) { + $this->info_box_contents[$line][] = array( + 'td' => 'class="left"', + 'text' => $resultarray[$line][0] + ); + + $this->info_box_contents[$line][] = array( + 'td' => 'class="left"', + 'textnoformat' => $resultarray[$line][1] + ); + $this->info_box_contents[$line][] = array( + 'td' => 'class="right"', + 'textnoformat' => dol_print_date($resultarray[$line][2],"dayhoursec") + ); + $this->info_box_contents[$line][] = array( + 'td' => 'class="right" ', + 'textnoformat' => empty($resultarray[$line][3]) ? $langs->trans("Disabled") : $langs->trans("Scheduled") + ); + $line++; + } + $this->info_box_contents[$line][] = array( + 'td' => 'class="left" colspan="2"', + 'text' => $langs->trans("NumberScheduledJobError") + ); + $this->info_box_contents[$line][] = array( + 'td' => 'class="right"colspan="2"', + 'text' => $nbjobsinerror . "" + ); + } else { + $this->info_box_contents[0][0] = array( + 'td' => '', + 'maxlength' => 500, + 'text' => ($this->db->error() . ' sql=' . $sql) + ); + } + } else { + $this->info_box_contents[0][0] = array( + 'td' => 'class="nohover opacitymedium left"', + 'text' => $langs->trans("ReadPermissionNotAllowed") + ); + } + } + + /** + * Method to show box + * + * @param array $head Array with properties of box title + * @param array $contents Array with properties of box lines + * @param int $nooutput No print, only return string + * @return string + */ + public function showBox($head = null, $contents = null, $nooutput = 0) + { + return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput); + } +} diff --git a/htdocs/core/modules/modCron.class.php b/htdocs/core/modules/modCron.class.php index b8b281b79d1..deb140f76e4 100644 --- a/htdocs/core/modules/modCron.class.php +++ b/htdocs/core/modules/modCron.class.php @@ -91,7 +91,9 @@ class modCron extends DolibarrModules // Boxes //------ - $this->boxes = array(); + $this->boxes = array( + 0 => array('file' => 'box_scheduled_jobs.php', 'enabledbydefaulton' => 'Home') + ); // Cronjobs $this->cronjobs = array( diff --git a/htdocs/install/mysql/migration/12.0.0-13.0.0.sql b/htdocs/install/mysql/migration/12.0.0-13.0.0.sql index 1d8c7a37b95..3c7c6d2f8d3 100644 --- a/htdocs/install/mysql/migration/12.0.0-13.0.0.sql +++ b/htdocs/install/mysql/migration/12.0.0-13.0.0.sql @@ -551,4 +551,6 @@ INSERT INTO llx_boxes_def(file,entity) VALUES ('box_funnel_of_prospection.php',1 INSERT INTO llx_boxes_def(file, entity) VALUES ('box_customers_outstanding_bill_reached.php', 1); -ALTER TABLE llx_product_fournisseur_price ADD COLUMN packaging varchar(64); \ No newline at end of file +ALTER TABLE llx_product_fournisseur_price ADD COLUMN packaging varchar(64); + +INSERT INTO llx_boxes_def(file, entity) VALUES ('box_scheduled_jobs.php',1); \ No newline at end of file diff --git a/htdocs/langs/en_US/boxes.lang b/htdocs/langs/en_US/boxes.lang index 1e91080962e..470a8f5274e 100644 --- a/htdocs/langs/en_US/boxes.lang +++ b/htdocs/langs/en_US/boxes.lang @@ -51,6 +51,7 @@ BoxGlobalActivity=Global activity (invoices, proposals, orders) BoxGoodCustomers=Good customers BoxTitleGoodCustomers=%s Good customers BoxTitleFunnelOfProspection=Amount of prospection by status +BoxScheduledJobs=Schedules jobs FailedToRefreshDataInfoNotUpToDate=Failed to refresh RSS flux. Latest successful refresh date: %s LastRefreshDate=Latest refresh date NoRecordedBookmarks=No bookmarks defined. diff --git a/htdocs/langs/en_US/cron.lang b/htdocs/langs/en_US/cron.lang index 9bf918a73a3..2ebdda1e685 100644 --- a/htdocs/langs/en_US/cron.lang +++ b/htdocs/langs/en_US/cron.lang @@ -84,4 +84,8 @@ MakeLocalDatabaseDumpShort=Local database backup MakeLocalDatabaseDump=Create a local database dump. Parameters are: compression ('gz' or 'bz' or 'none'), backup type ('mysql', 'pgsql', 'auto'), 1, 'auto' or filename to build, number of backup files to keep WarningCronDelayed=Attention, for performance purpose, whatever is next date of execution of enabled jobs, your jobs may be delayed to a maximum of %s hours, before being run. DATAPOLICYJob=Data cleaner and anonymizer -JobXMustBeEnabled=Job %s must be enabled \ No newline at end of file +JobXMustBeEnabled=Job %s must be enabled +# Cron Boxes +LastExecutedScheduledJob=Last executed scheduled job +NextScheduledJobExecute=Next scheduled job to execute +NumberScheduledJobError=Number of scheduled jobs in error From 54228ad256322209677f8e3602c6b34f96aa3831 Mon Sep 17 00:00:00 2001 From: lmarcouiller Date: Fri, 11 Dec 2020 15:49:47 +0100 Subject: [PATCH 2/3] Close #15729 : fix bug with nb jobs --- htdocs/core/boxes/box_scheduled_jobs.php | 32 ++++++++++-------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/htdocs/core/boxes/box_scheduled_jobs.php b/htdocs/core/boxes/box_scheduled_jobs.php index 21c0019d525..ccb438c30f2 100644 --- a/htdocs/core/boxes/box_scheduled_jobs.php +++ b/htdocs/core/boxes/box_scheduled_jobs.php @@ -92,14 +92,16 @@ class box_scheduled_jobs extends ModeleBoxes $nbjobsinerror = 0; if ($result) { $num = $this->db->num_rows($result); - $i = 1; - $objp = $this->db->fetch_object($result); - $cronstatic->id = $objp->rowid; - $cronstatic->ref = $objp->rowid; - $cronstatic->status = $objp->status; - $cronstatic->datenextrun = $objp->datenextrun; - $cronstatic->datelastrun = $objp->datelastrun; + $i = 0; while ($i < $num) { + $objp = $this->db->fetch_object($result); + if ($line == 0 || $objp->datenextrun < $cronstatic->datenextrun) { + $cronstatic->id = $objp->rowid; + $cronstatic->ref = $objp->rowid; + $cronstatic->status = $objp->status; + $cronstatic->datenextrun = $objp->datenextrun; + $cronstatic->datelastrun = $objp->datelastrun; + } if ($line == 0) { $resultarray[$line] = array( $langs->trans("LastExecutedScheduledJob"), @@ -108,16 +110,8 @@ class box_scheduled_jobs extends ModeleBoxes $cronstatic->status ); $line++; - } else { - $objp = $this->db->fetch_object($result); - if ($objp->datenextrun < $cronstatic->datenextrun) { - $cronstatic->id = $objp->rowid; - $cronstatic->ref = $objp->rowid; - $cronstatic->status = $objp->status; - $cronstatic->datenextrun = $objp->datenextrun; - } - } - if ($obj->lastresult < 0) { + } + if (!empty($objp->lastresult)){ $nbjobsinerror++; } $i++; @@ -141,7 +135,7 @@ class box_scheduled_jobs extends ModeleBoxes ); $this->info_box_contents[$line][] = array( 'td' => 'class="right"', - 'textnoformat' => dol_print_date($resultarray[$line][2],"dayhoursec") + 'textnoformat' => dol_print_date($resultarray[$line][2], "dayhoursec") ); $this->info_box_contents[$line][] = array( 'td' => 'class="right" ', @@ -155,7 +149,7 @@ class box_scheduled_jobs extends ModeleBoxes ); $this->info_box_contents[$line][] = array( 'td' => 'class="right"colspan="2"', - 'text' => $nbjobsinerror . "" + 'text' => $nbjobsinerror ); } else { $this->info_box_contents[0][0] = array( From dea75e6158cf95dd7b2c8a2f893dac7d118a812b Mon Sep 17 00:00:00 2001 From: lmarcouiller Date: Fri, 11 Dec 2020 15:55:17 +0100 Subject: [PATCH 3/3] Close #15729 : fix error on box name and format --- htdocs/core/boxes/box_scheduled_jobs.php | 4 ++-- htdocs/langs/en_US/boxes.lang | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/htdocs/core/boxes/box_scheduled_jobs.php b/htdocs/core/boxes/box_scheduled_jobs.php index ccb438c30f2..1739c6ab8e9 100644 --- a/htdocs/core/boxes/box_scheduled_jobs.php +++ b/htdocs/core/boxes/box_scheduled_jobs.php @@ -110,8 +110,8 @@ class box_scheduled_jobs extends ModeleBoxes $cronstatic->status ); $line++; - } - if (!empty($objp->lastresult)){ + } + if (!empty($objp->lastresult)) { $nbjobsinerror++; } $i++; diff --git a/htdocs/langs/en_US/boxes.lang b/htdocs/langs/en_US/boxes.lang index 470a8f5274e..b5216e09df5 100644 --- a/htdocs/langs/en_US/boxes.lang +++ b/htdocs/langs/en_US/boxes.lang @@ -51,7 +51,7 @@ BoxGlobalActivity=Global activity (invoices, proposals, orders) BoxGoodCustomers=Good customers BoxTitleGoodCustomers=%s Good customers BoxTitleFunnelOfProspection=Amount of prospection by status -BoxScheduledJobs=Schedules jobs +BoxScheduledJobs=Scheduled jobs FailedToRefreshDataInfoNotUpToDate=Failed to refresh RSS flux. Latest successful refresh date: %s LastRefreshDate=Latest refresh date NoRecordedBookmarks=No bookmarks defined.