Files
piratepoet/mailpoet/lib/AdminPages/Pages/Help.php
Rostislav Wolny b02aa1e1a6 Add Action scheduler status into on help page
[MAILPOET-4274]
2022-08-03 10:36:57 +02:00

114 lines
3.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace MailPoet\AdminPages\Pages;
use MailPoet\AdminPages\PageRenderer;
use MailPoet\Cron\CronHelper;
use MailPoet\Cron\DaemonActionSchedulerRunner;
use MailPoet\Helpscout\Beacon;
use MailPoet\Mailer\MailerLog;
use MailPoet\Router\Endpoints\CronDaemon;
use MailPoet\Services\Bridge;
use MailPoet\Tasks\Sending;
use MailPoet\Tasks\State;
class Help {
/** @var PageRenderer */
private $pageRenderer;
/** @var State */
private $tasksState;
/** @var CronHelper */
private $cronHelper;
/** @var Beacon */
private $helpscoutBeacon;
/** @var Bridge $bridge */
private $bridge;
public function __construct(
PageRenderer $pageRenderer,
State $tasksState,
CronHelper $cronHelper,
Beacon $helpscoutBeacon,
Bridge $bridge
) {
$this->pageRenderer = $pageRenderer;
$this->tasksState = $tasksState;
$this->cronHelper = $cronHelper;
$this->helpscoutBeacon = $helpscoutBeacon;
$this->bridge = $bridge;
}
public function render() {
$systemInfoData = $this->helpscoutBeacon->getData(true);
try {
$cronPingUrl = $this->cronHelper->getCronUrl(CronDaemon::ACTION_PING);
$cronPingResponse = $this->cronHelper->pingDaemon();
} catch (\Exception $e) {
$cronPingResponse = __('Cant generate cron URL.', 'mailpoet') . ' (' . $e->getMessage() . ')';
$cronPingUrl = $cronPingResponse;
}
$mailerLog = MailerLog::getMailerLog();
$mailerLog['sent'] = MailerLog::sentSince();
$systemStatusData = [
'cron' => [
'url' => $cronPingUrl,
'isReachable' => $this->cronHelper->validatePingResponse($cronPingResponse),
'pingResponse' => $cronPingResponse,
],
'mss' => [
'enabled' => $this->bridge->isMailpoetSendingServiceEnabled(),
'isReachable' => $this->bridge->pingBridge(),
],
'cronStatus' => $this->cronHelper->getDaemon(),
'queueStatus' => $mailerLog,
];
$systemStatusData['cronStatus']['accessible'] = $this->cronHelper->isDaemonAccessible();
$systemStatusData['queueStatus']['tasksStatusCounts'] = $this->tasksState->getCountsPerStatus();
$systemStatusData['queueStatus']['latestTasks'] = $this->tasksState->getLatestTasks(Sending::TASK_TYPE);
$this->pageRenderer->displayPage(
'help.html',
[
'systemInfoData' => $systemInfoData,
'systemStatusData' => $systemStatusData,
'actionSchedulerData' => $this->getActionSchedulerData(),
]
);
}
private function getActionSchedulerData(): ?array {
if (!class_exists(\ActionScheduler_Store::class)) {
return null;
}
$actionSchedulerData = [];
$actionSchedulerData['version'] = \ActionScheduler_Versions::instance()->latest_version();
$actionSchedulerData['storage'] = str_replace('ActionScheduler_', '', get_class(\ActionScheduler_Store::instance()));
$actionSchedulerData['latestTrigger'] = $this->getLatestActionSchedulerActionDate(DaemonActionSchedulerRunner::DAEMON_TRIGGER_SCHEDULER_ACTION);
$actionSchedulerData['latestCompletedTrigger'] = $this->getLatestActionSchedulerActionDate(DaemonActionSchedulerRunner::DAEMON_TRIGGER_SCHEDULER_ACTION, 'complete');
$actionSchedulerData['latestCompletedRun'] = $this->getLatestActionSchedulerActionDate(DaemonActionSchedulerRunner::DAEMON_RUN_SCHEDULER_ACTION, 'complete');
return $actionSchedulerData;
}
private function getLatestActionSchedulerActionDate(string $hook, string $status = null): ?string {
$query = [
'per_page' => 1,
'order' => 'DESC',
'hook' => $hook,
];
if ($status) {
$query['status'] = $status;
}
$store = \ActionScheduler_Store::instance();
$action = $store->query_actions($query);
if (!empty($action)) {
$dateObject = $store->get_date( $action[0] );
return $dateObject->format('Y-m-d H:i:s');
}
return null;
}
}