[FEATURE] Add scheduler information to system information toolbar 09/57609/19
authorJosef Glatz <josefglatz@gmail.com>
Mon, 16 Jul 2018 09:21:43 +0000 (11:21 +0200)
committerGeorg Ringer <georg.ringer@gmail.com>
Mon, 7 Jan 2019 13:56:29 +0000 (14:56 +0100)
The system information toolbar now shows useful information
about the (automatic) execution/configuration of the TYPO3
scheduler if the system extension is activated and if there
are any tasks configured at all.

- Show a warning when the scheduler execution seems not
  to be configured correctly
- Show start date, start time, duration and execution type
  of the last sucessfully executed scheduler run
  - Highlight information if it was started manually

Releases: master
Resolves: #85569
Change-Id: Ic1b8a1c52678b60d40edcb77296f51f58ec01a4b
Reviewed-on: https://review.typo3.org/57609
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Jan Helke <typo3@helke.de>
Tested-by: Jan Helke <typo3@helke.de>
Reviewed-by: Stefanos Karasavvidis <sk@karasavvidis.gr>
Tested-by: Stefanos Karasavvidis <sk@karasavvidis.gr>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
typo3/sysext/core/Documentation/Changelog/master/Feature-85569-ShowSchedulerInformationsInTheSystemInformationToolbar.rst [new file with mode: 0644]
typo3/sysext/scheduler/Classes/SystemInformation/ToolbarItemProvider.php [new file with mode: 0644]
typo3/sysext/scheduler/Resources/Private/Language/locallang.xlf
typo3/sysext/scheduler/ext_localconf.php

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-85569-ShowSchedulerInformationsInTheSystemInformationToolbar.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-85569-ShowSchedulerInformationsInTheSystemInformationToolbar.rst
new file mode 100644 (file)
index 0000000..1c46860
--- /dev/null
@@ -0,0 +1,29 @@
+.. include:: ../../Includes.txt
+
+==============================================================================
+Feature: #85569 - Show scheduler information in the system information toolbar
+==============================================================================
+
+See :issue:`85569`
+
+Description
+===========
+
+The system information toolbar now shows useful information about the TYPO3 scheduler system extension.
+
+The following information can be gathered quickly via the toolbar:
+- Warning if the scheduler execution seems not to be configured correctly
+- Important information about the last successfully executed scheduler run
+  - the start date
+  - the start time
+  - the duration (in minutes)
+  - the execution type (automatically/CLI or manually/via backend)
+    - a manual execution highlights the text
+
+Impact
+======
+
+The mentioned scheduler specific information is added to the system information toolbar, if the system extension
+`scheduler` is active and if there are any tasks configured at all.
+
+.. index:: Backend, ext:scheduler
diff --git a/typo3/sysext/scheduler/Classes/SystemInformation/ToolbarItemProvider.php b/typo3/sysext/scheduler/Classes/SystemInformation/ToolbarItemProvider.php
new file mode 100644 (file)
index 0000000..f123441
--- /dev/null
@@ -0,0 +1,164 @@
+<?php
+declare(strict_types = 1);
+namespace TYPO3\CMS\Scheduler\SystemInformation;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem;
+use TYPO3\CMS\Backend\Routing\UriBuilder;
+use TYPO3\CMS\Backend\Toolbar\Enumeration\InformationStatus;
+use TYPO3\CMS\Backend\Utility\BackendUtility;
+use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Localization\LanguageService;
+use TYPO3\CMS\Core\Registry;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+/**
+ * Add information about the scheduler configuration to the system information toolbar
+ */
+class ToolbarItemProvider
+{
+    /**
+     * Scheduler last run registry information
+     *
+     * @var array
+     */
+    protected $lastRunInformation = [];
+
+    /**
+     * Gather initial information
+     */
+    public function __construct()
+    {
+        $this->lastRunInformation = GeneralUtility::makeInstance(Registry::class)->get('tx_scheduler', 'lastRun', []);
+    }
+
+    /**
+     * Display information about last automated run, as stored in the system registry
+     *
+     * @param SystemInformationToolbarItem $systemInformationToolbarItem
+     */
+    public function getItem(SystemInformationToolbarItem $systemInformationToolbarItem): void
+    {
+        // No tasks configured, so nothing is shown at all
+        if (!$this->hasConfiguredTasks()) {
+            return;
+        }
+        $languageService = $this->getLanguageService();
+        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
+
+        if (!$this->schedulerWasExecuted()) {
+            // Display system message if the Scheduler has never yet run
+            $systemInformationToolbarItem->addSystemMessage(
+                sprintf(
+                    $languageService->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:systemmessage.noLastRun'),
+                    (string)$uriBuilder->buildUriFromRoute(
+                        'system_txschedulerM1',
+                        [
+                            'id' => 0,
+                            'SET' => [
+                                'function' => 'check'
+                            ]
+                        ]
+                    )
+                ),
+                InformationStatus::STATUS_WARNING,
+                0,
+                'system_txschedulerM1',
+                http_build_query([
+                    'id' => 0,
+                    'SET' => [
+                        'function' => 'check'
+                    ]
+                ])
+            );
+        } else {
+            // Display information about the last Scheduler execution
+            if (!$this->lastRunInfoExists()) {
+                // Show warning if the information of the last run is incomplete
+                $message = $languageService->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.incompleteLastRun');
+                $severity = InformationStatus::STATUS_WARNING;
+            } else {
+                $startDate = date($GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'], $this->lastRunInformation['start']);
+                $startTime = date($GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'], $this->lastRunInformation['start']);
+                $duration = BackendUtility::calcAge(
+                    $this->lastRunInformation['end'] - $this->lastRunInformation['start'],
+                    $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.minutesHoursDaysYears')
+                );
+                $severity = '';
+                $label = 'automatically';
+                if ($this->lastRunInformation['type'] === 'manual') {
+                    $label = 'manually';
+                    $severity = InformationStatus::STATUS_INFO;
+                }
+                $type = $languageService->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.' . $label);
+                $message = sprintf($languageService->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:systeminformation.lastRunValue'), $startDate, $startTime, $duration, $type);
+            }
+            $systemInformationToolbarItem->addSystemInformation(
+                $languageService->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:systeminformation.lastRunLabel'),
+                $message,
+                'extensions-scheduler-run-task',
+                $severity
+            );
+        }
+    }
+
+    /**
+     * Check whether the scheduler was already executed
+     *
+     * @return bool
+     */
+    protected function schedulerWasExecuted(): bool
+    {
+        return !empty($this->lastRunInformation);
+    }
+
+    /**
+     * Check if the last scheduler run array contains all information
+     *
+     * @return bool
+     */
+    protected function lastRunInfoExists(): bool
+    {
+        return !empty($this->lastRunInformation['end'])
+            || !empty($this->lastRunInformation['start'])
+            || !empty($this->lastRunInformation['type']);
+    }
+
+    /**
+     * See if there are any tasks configured at all.
+     *
+     * @return bool
+     */
+    protected function hasConfiguredTasks(): bool
+    {
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_scheduler_task');
+        $queryBuilder->getRestrictions()->removeAll();
+        $queryBuilder
+            ->count('uid')
+            ->from('tx_scheduler_task')
+            ->where(
+                $queryBuilder->expr()->eq('deleted', 0)
+            );
+        return $queryBuilder->execute()->fetchColumn() > 0;
+    }
+
+    /**
+     * @return LanguageService
+     */
+    protected function getLanguageService(): LanguageService
+    {
+        return $GLOBALS['LANG'];
+    }
+}
index aa3c5b4..6e9e9ff 100644 (file)
                        <trans-unit id="command.example4">
                                <source>Every Tuesday at 07:00</source>
                        </trans-unit>
+                       <trans-unit id="systemmessage.noLastRun">
+                               <source><![CDATA[It seems the Scheduler has never yet run. Please check your <a href="%1$s">configuration</a>.]]></source>
+                       </trans-unit>
+                       <trans-unit id="systeminformation.lastRunLabel">
+                               <source>Last Scheduler run</source>
+                       </trans-unit>
+                       <trans-unit id="systeminformation.lastRunValue">
+                               <source>%1$s at %2$s, Duration %3$s, (started %4$s)</source>
+                       </trans-unit>
                </body>
        </file>
 </xliff>
index 1f8ab51..ed3f42a 100644 (file)
@@ -132,3 +132,12 @@ $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['frequencyOptions'] = [
     '*/20 * * * *' =>  'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:command.example3',
     '0 7 * * 2' =>  'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:command.example4',
 ];
+
+// Add system information toolbar item
+\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class)
+    ->connect(
+        \TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem::class,
+        'getSystemInformation',
+        \TYPO3\CMS\Scheduler\SystemInformation\ToolbarItemProvider::class,
+        'getItem'
+    );