From b34b1ad5bbc8d75db927116b517e6a23761e42bb Mon Sep 17 00:00:00 2001
From: Till Brehm <tbrehm@ispconfig.org>
Date: Thu, 4 Jun 2020 13:53:35 +0200
Subject: [PATCH] Improved cron debug script

---
 server/cron_debug.php | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)

diff --git a/server/cron_debug.php b/server/cron_debug.php
index 74065f02c4..fbf59ebc26 100644
--- a/server/cron_debug.php
+++ b/server/cron_debug.php
@@ -39,20 +39,25 @@ ini_set('error_reporting', E_ALL & ~E_NOTICE);
 $conf['server_id'] = intval($conf['server_id']);
 
 // Load required base-classes
-$app->uses('modules,plugins,ini_parser,file,services,getconf,system,cron,functions');
+$app->uses('ini_parser,file,services,getconf,system,cron,functions');
 $app->load('libdatetime,cronjob');
 
 // Path settings
 $path = SCRIPT_PATH . '/lib/classes/cron.d';
 
 //** Get commandline options
-$cmd_opt = getopt('', array('cronjob::'));
+$cmd_opt = getopt('', array('cronjob::', 'force', 'firstrun'));
 
 if(isset($cmd_opt['cronjob']) && is_file($path.'/'.$cmd_opt['cronjob'])) {
 	// Cronjob that shell be run
 	$cronjob_file = $cmd_opt['cronjob'];
 } else {
-	die('Usage example: php cron_debug.php --cronjob=100-mailbox_stats.inc.php');
+	echo "Usage example: php cron_debug.php --cronjob=100-mailbox_stats.inc.php [--force] [--firstrun]\n" ;
+	echo "Available cronjobs:\n";
+	foreach(glob($path.'/*-*.inc.php') as $cronjob) {
+		echo basename($cronjob)."\n";
+	}
+	die();
 }
 
 // Load and run the cronjob
@@ -61,8 +66,20 @@ if(preg_match('/^\d+\-(.*)$/', $name, $match)) $name = $match[1]; // strip numer
 include $path . '/' . $cronjob_file;
 $class_name = 'cronjob_' . $name;
 $cronjob = new $class_name();
-$cronjob->run(true);
+
+if(isset($cmd_opt['force'])) {
+	$app->db->query("UPDATE `sys_cron` SET `running` = 0 WHERE `name` = ?", $class_name);
+}
+
+$cronjob->onPrepare();
+$cronjob->onBeforeRun(isset($cmd_opt['firstrun']));
+if(!$cronjob->isRunning()) {
+	$app->db->query("UPDATE `sys_cron` SET `running` = ? WHERE `name` = ?", ($cronjob->canBeRunInParallel() !== true ? "1" : "0"), $class_name);
+	$cronjob->onRunJob();
+	$cronjob->onAfterRun();
+	$cronjob->onCompleted();
+}
 
 die("finished.\n");
 
-?>
+?>
\ No newline at end of file
-- 
GitLab