Skip to content
Commits auf Quelle (5)
  • Henning Leutz's avatar
    feat: add cleanup cron history functionality · 524889db
    verfasst von Henning Leutz
    Added a new functionality to the QUI Cron system for cleaning up cron history. These changes
    include:
    - An additional cron task defined in `cron.xml` for housekeeping the cron history.
    - A new function called `cleanupCronHistory(array $params, Manager $CronManager)` in
    `QuiqqerCrons.php` that removes old cron history entries which are older than a defined amount of
    weeks.
    - Added a new field and additional indexes in the `cron_history` table in `database.xml` to support
    the new feature.
    - New localized messages are added for the cron cleanup task 'title' and 'description' in
    `locale.xml`.
    
    Related: #59
    524889db
  • Henning Leutz's avatar
    style: code style refactor · da43e001
    verfasst von Henning Leutz
    da43e001
  • Henning Leutz's avatar
    fix: status check not via cron history · ea5e8799
    verfasst von Henning Leutz
    Updated the EventHandler class to improve handling of cron jobs and exceptions. Changes include:
    - Refactored variable names for clarity
    - Adjusted method to check last cron job execution, replacing custom Cron Manager method with
    direct database query to limit potential errors
    - Used DateTime to calculate timestamps and format dates
    - Simplified array constructs and replaced string substitutions
    - Added exception declaration to methods
    - Improved experience when handling cron history during migration.
    
    Related: #58
    Related: #59
    ea5e8799
  • Henning Leutz's avatar
    fix(phpcs): remove unnecessary newline in QuiqqerCrons.php · c1245a36
    verfasst von Henning Leutz
    This commit removes an unnecessary newline from QuiqqerCrons.php to improve readability of the
    code. No functional changes were made.
    c1245a36
  • Henning Leutz's avatar
    Merge branch 'next-2.x' into 'main' · 964bd3d6
    verfasst von Henning Leutz
    feat: add cleanup cron history functionality
    
    See merge request !25
    964bd3d6
......@@ -10,17 +10,14 @@ function adminInit() {
new QUIConfirm({
maxHeight: 400,
maxWidth: 600,
autoclose: true,
backgroundClosable: false,
titleCloseButton: false,
information: QUILocale.get(lg, 'message.cron.admin.info.24h'),
title: QUILocale.get(lg, 'message.cron.admin.info.24h.title'),
texticon: 'fa fa-exclamation-triangle',
text: QUILocale.get(lg, 'message.cron.admin.info.24h.text'),
icon: 'fa fa-exclamation-triangle',
cancel_button: false,
ok_button: {
text: QUILocale.get(lg, 'message.cron.admin.info.24h.btn.submit'),
......
......@@ -11,6 +11,25 @@
</description>
</cron>
<!-- region Cleanup Temp-Folder -->
<cron exec="\QUI\Cron\QuiqqerCrons::cleanupCronHistory">
<title>
<locale group="quiqqer/cron" var="cron.cleanup.history.title"/>
</title>
<description>
<locale group="quiqqer/cron" var="cron.cleanup.history.description"/>
</description>
<params>
<param name="deleteAfterWeeks" type="int"/>
</params>
<autocreate>
<interval>0 0 * * *</interval>
<active>1</active>
</autocreate>
</cron>
<!-- endregion -->
<!-- region Cleanup Temp-Folder -->
<cron exec="\QUI\Cron\QuiqqerCrons::clearTempFolder">
<title>
......
......@@ -19,12 +19,16 @@
</table>
<table name="cron_history">
<field type="INT NOT NULL AUTO_INCREMENT PRIMARY KEY">id</field>
<field type="INT(3) NOT NULL">cronid</field>
<field type="VARCHAR(50) NOT NULL">uid</field>
<field type="DATETIME NOT NULL">lastexec</field>
<field type="DATETIME NULL DEFAULT NULl">finish</field>
<primary>id</primary>
<index>cronid</index>
<index>lastexec</index>
<index>uid</index>
</table>
<table name="cron_cronservice">
......
......@@ -161,6 +161,17 @@
</locale>
<!-- endregion -->
<!-- region Cleanup History -->
<locale name="cron.cleanup.history.title">
<de><![CDATA[Cron-Historie bereinigen]]></de>
<en><![CDATA[Clean up Cron History]]></en>
</locale>
<locale name="cron.cleanup.history.description">
<de><![CDATA[Löscht alte Einträge aus der Cron-Historie, die älter als eine definierte Anzahl von Wochen sind.]]></de>
<en><![CDATA[Deletes old entries from the cron history that are older than a defined number of weeks.]]></en>
</locale>
<!-- endregion -->
<!-- region Cleanup Sessions -->
<locale name="cron.cleanup.sessions.title">
<de><![CDATA[System-Bereinigung: Sitzungen bereinigen]]></de>
......
......@@ -6,6 +6,7 @@
namespace QUI\Cron;
use DateTime;
use QUI;
use QUI\Exception;
use QUI\System\Console\Tools\MigrationV2;
......@@ -56,12 +57,13 @@ class EventHandler
return;
}
$Stmnt = QUI::getDataBase()->getPDO()->prepare("ALTER TABLE cron MODIFY `title` VARCHAR(1000)");
$Stmnt->execute();
$Statement = QUI::getDataBase()->getPDO()->prepare("ALTER TABLE cron MODIFY `title` VARCHAR(1000)");
$Statement->execute();
}
/**
* event : on admin header loaded
* @throws \Doctrine\DBAL\Exception
*/
public static function onAdminLoad(): void
{
......@@ -93,22 +95,26 @@ class EventHandler
}
// check last cron execution
$CronManager = new Manager();
$result = $CronManager->getHistoryList([
'page' => 1,
'perPage' => 1
$database = QUI::getDataBaseConnection();
$table = Manager::table();
// Zeitstempel 24 Stunden zurück
$thresholdDate = new DateTime('-24 hours');
$thresholdFormatted = $thresholdDate->format('Y-m-d H:i:s');
// Alle Crons mit lastexec NULL oder älter als 24h
$sql = "
SELECT id
FROM $table
WHERE lastexec >= :threshold
LIMIT 1
";
$result = $database->fetchOne($sql, [
'threshold' => $thresholdFormatted
]);
if (!isset($result[0])) {
self::sendAdminInfoCronError();
return;
}
$date = strtotime($result[0]['lastexec']);
// in 24h no cron??
if (time() - 86400 > $date) {
if ($result === false) {
self::sendAdminInfoCronError();
}
}
......@@ -239,12 +245,8 @@ class EventHandler
if (!$autocreate['active']) {
QUI::getDataBase()->update(
$CronManager::table(),
[
'active' => 0
],
[
'id' => $cronId
]
['active' => 0],
['id' => $cronId]
);
}
} catch (\Exception $Exception) {
......@@ -315,12 +317,8 @@ class EventHandler
foreach ($projectCronParams as $k => $v) {
$projectCronParams[$k] = str_replace(
[
'[lang]',
],
[
$language
],
['[lang]',],
[$language],
$v
);
}
......@@ -331,6 +329,9 @@ class EventHandler
return $createCrons;
}
/**
* @throws QUI\Database\Exception
*/
public static function onQuiqqerMigrationV2(MigrationV2 $Console): void
{
$Console->writeLn('- Migrate cron history');
......
......@@ -6,9 +6,11 @@
namespace QUI\Cron;
use DateTime;
use PDO;
use QUI;
use QUI\Database\Exception;
use Throwable;
use function date;
use function date_create;
......@@ -62,6 +64,44 @@ class QuiqqerCrons
QUI\Utils\System\File::unlink(VAR_DIR . 'cache/admin/media/');
}
public static function cleanupCronHistory(array $params, Manager $CronManager): void
{
$weeks = 8;
$table = Manager::tableHistory();
$database = QUI::getDataBaseConnection();
if (!empty($params['deleteAfterWeeks'])) {
$weeks = (int)$params['deleteAfterWeeks'];
}
try {
$thresholdDate = new DateTime();
$thresholdDate->modify("-{$weeks} weeks");
$thresholdFormatted = $thresholdDate->format('Y-m-d H:i:s');
$deleteSql = "
DELETE FROM $table
WHERE
lastexec < :threshold AND
id NOT IN (
SELECT max_id FROM (
SELECT MAX(id) AS max_id
FROM $table
GROUP BY cronid
) AS sub
)
";
$database->executeStatement($deleteSql, [
'threshold' => $thresholdFormatted
]);
} catch (Throwable $exception) {
QUI\System\Log::addError($exception->getMessage(), [
'error' => 'at cron execution cleanupCronHistory'
]);
}
}
/**
* delete all unwanted / unneeded sessions
* @throws Exception
......@@ -138,11 +178,11 @@ class QuiqqerCrons
/**
* alias -> because release was misspelled
*
* @param $params
* @param $CronManager
* @param array $params
* @param Manager $CronManager
* @throws QUI\Exception
*/
public static function realeaseDate($params, $CronManager): void
public static function realeaseDate(array $params, Manager $CronManager): void
{
self::releaseDate($params, $CronManager);
}
......