Skip to content

Commit

Permalink
feat: remove old part with cronjob interval
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Neyman committed Mar 7, 2023
1 parent 1c0dc53 commit f332377
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 259 deletions.
137 changes: 2 additions & 135 deletions Classes/Module/MailerEngineController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,11 @@ public function indexAction(ServerRequestInterface $request): ResponseInterface

// Direct mail module
if (($this->pageinfo['doktype'] ?? 0) == 254) {
$cronMonitor = $this->cronMonitor();
$mailerEngine = $this->mailerengine();

$this->view->assignMultiple(
[
'schedulerTable' => $cronMonitor,
'schedulerTable' => $this->getSchedulerTable(),
'data' => $mailerEngine['data'],
'id' => $this->id,
'invoke' => $mailerEngine['invoke'],
Expand Down Expand Up @@ -101,145 +100,13 @@ public function indexAction(ServerRequestInterface $request): ResponseInterface
return new HtmlResponse($this->moduleTemplate->renderContent());
}

/**
* Monitor the cronjob.
*/
protected function cronMonitor(): array
protected function getSchedulerTable(): array
{
$schedulerTable = [];
if (ExtensionManagementUtility::isLoaded('scheduler')) {
$this->getLanguageService()->includeLLFile('EXT:scheduler/Resources/Private/Language/locallang.xlf');

$schedulerTable = SchedulerUtility::getDMTable($this->getLanguageService());

/**
if ((int) $task['disable'] === 1) {
$message = $this->createFlashMessage(
'disabled', //@TODO
'Scheduled task', //@TODO
1,
false
);
$this->messageQueue->addMessage($message);
}
else {
$message = $this->createFlashMessage(
'enabled', //@TODO
'Scheduled task', //@TODO
0,
false
);
$this->messageQueue->addMessage($message);
}
}
else {
$message = $this->createFlashMessage(
'Scheduled task', //@TODO
'Not found', //@TODO
1,
false
);
$this->messageQueue->addMessage($message);
}
}
else {
$message = $this->createFlashMessage(
'Scheduler extension', //@TODO
'Not installed', //@TODO
1,
false
);
$this->messageQueue->addMessage($message);
}
*/
}



$mailerStatus = 0;
$lastExecutionTime = 0;
$logContent = '';
$error = '';

// seconds
$cronInterval = Typo3ConfVarsUtility::getDMConfigCronInt() * 60;
$lastCronjobShouldBeNewThan = (time() - $cronInterval);
$filename = $this->getDmailerLogFilePath();
if (file_exists($filename)) {
$logContent = file_get_contents($filename);
$lastExecutionTime = substr($logContent, 0, 10);
}

/*
* status:
* 1 = ok
* 0 = check
* -1 = cron stopped
*
* cron running or error (die function in dmailer_log)
*/
if (file_exists($this->getDmailerLockFilePath())) {
$res = GeneralUtility::makeInstance(SysDmailMaillogRepository::class)->selectByResponseType(0);
if (is_array($res)) {
foreach ($res as $lastSend) {
if (($lastSend['tstamp'] < time()) && ($lastSend['tstamp'] > $lastCronjobShouldBeNewThan)) {
// cron is sending
$mailerStatus = 1;
} else {
// there's lock file but cron is not sending
$mailerStatus = -1;
}
}
}
// cron is idle or no cron
} elseif (strpos($logContent, 'error')) {
// error in log file
$mailerStatus = -1;
$error = substr($logContent, strpos($logContent, 'error') + 7);
} elseif (!strlen($logContent) || ($lastExecutionTime < $lastCronjobShouldBeNewThan)) {
// cron is not set or not running
$mailerStatus = 0;
} else {
// last run of cron is in the interval
$mailerStatus = 1;
}

$currentDate = ' / ' . $this->getLanguageService()->getLL('dmail_mailerengine_current_time') . ' ' . BackendUtility::datetime(time()) . '. ';
$lastRun = ' ' . $this->getLanguageService()->getLL('dmail_mailerengine_cron_lastrun') . ($lastExecutionTime ? BackendUtility::datetime($lastExecutionTime) : '-') . $currentDate;
switch ($mailerStatus) {
case -1:
$message = $this->createFlashMessage(
$this->getLanguageService()->getLL('dmail_mailerengine_cron_warning') . ': ' . ($error ? $error : $this->getLanguageService()->getLL('dmail_mailerengine_cron_warning_msg')) . $lastRun,
$this->getLanguageService()->getLL('dmail_mailerengine_cron_status'),
2,
false
);
$this->messageQueue->addMessage($message);
break;
case 0:
$message = $this->createFlashMessage(
$this->getLanguageService()->getLL('dmail_mailerengine_cron_caution') . ': ' . $this->getLanguageService()->getLL('dmail_mailerengine_cron_caution_msg') . $lastRun,
$this->getLanguageService()->getLL('dmail_mailerengine_cron_status'),
1,
false
);
$this->messageQueue->addMessage($message);
break;
case 1:
$message = $this->createFlashMessage(
$this->getLanguageService()->getLL('dmail_mailerengine_cron_ok') . ': ' . $this->getLanguageService()->getLL('dmail_mailerengine_cron_ok_msg') . $lastRun,
$this->getLanguageService()->getLL('dmail_mailerengine_cron_status'),
0,
false
);
$this->messageQueue->addMessage($message);
break;
default:
}

return $schedulerTable;
}

Expand Down
7 changes: 1 addition & 6 deletions Classes/Module/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ protected function getDMPages(string $moduleName): array
return $dmLinks;
}

protected function getFieldList(): array
protected function getFieldList(): array
{
return [
'uid',
Expand Down Expand Up @@ -284,11 +284,6 @@ protected function getTempPath(): string
return Environment::getPublicPath() . '/typo3temp/';
}

protected function getDmailerLogFilePath(): string
{
return $this->getTempPath() . 'tx_directmail_dmailer_log.txt';
}

protected function getDmailerLockFilePath(): string
{
return $this->getTempPath() . 'tx_directmail_cron.lock';
Expand Down
21 changes: 0 additions & 21 deletions Classes/Repository/SysDmailMaillogRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,27 +154,6 @@ public function countSysDmailMaillogPingByMid(int $mid) //: array|bool
->fetchAllAssociative();
}

/**
* @return array|bool
*/
public function selectByResponseType(int $responseType) //: array|bool
{
$queryBuilder = $this->getQueryBuilder($this->table);

return $queryBuilder
->select('uid', 'tstamp')
->from($this->table)
->where(
$queryBuilder->expr()->eq(
'response_type',
$queryBuilder->createNamedParameter($responseType, Connection::PARAM_INT)
)
)
->orderBy('tstamp', 'DESC')
->executeQuery()
->fetchAllAssociative();
}

/**
* @return array|bool
*/
Expand Down
3 changes: 1 addition & 2 deletions Classes/Utility/SchedulerUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace DirectMailTeam\DirectMail\Utility;

/**
* from TYPO3\CMS\Scheduler\Controller\SchedulerModuleController listTasksAction
* Code from TYPO3\CMS\Scheduler\Controller\SchedulerModuleController listTasksAction
*/

use DirectMailTeam\DirectMail\Repository\TempRepository;
Expand Down Expand Up @@ -40,7 +40,6 @@ public static function getDMTable(LanguageService $languageService): array
$registeredClasses = self::getRegisteredClasses($languageService);
$dateFormat = Typo3ConfVarsUtility::getDateFormat();
foreach ($tasks as $task) {
// s. SchedulerModuleController listTasksAction
$isRunning = false;
$showAsDisabled = false;

Expand Down
6 changes: 0 additions & 6 deletions Classes/Utility/Typo3ConfVarsUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ public static function getDMConfigUseHttpToFetch(): bool
return (bool)$dmConfig['UseHttpToFetch'];
}

public static function getDMConfigCronInt(): int
{
$dmConfig = self::getDMConfig();
return (int)$dmConfig['cronInt'];
}

public static function getDateFormat()
{
return $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] . ' ' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'];
Expand Down
4 changes: 0 additions & 4 deletions Documentation/Configuration/InstallingTheExtension/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ extension configuration variables:
the site if the table can't be written (Direct Mail creates a lot of
log records while sending the email).

- **Interval of the cronjob:** Time interval of the cron task you set in
minute. This is used to control if the cron task is still running or
if there is an error while sending the email.

- **Enable notification email:** Allow Direct Mail to send notification
emails about start and end of mailing job.

Expand Down
28 changes: 2 additions & 26 deletions Resources/Private/Language/locallang_mod2-6.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -184,32 +184,8 @@
<trans-unit id="dmail_frames_not allowed" resname="dmail_frames_not allowed">
<source>Pages with HTML frames may not be fetched.</source>
</trans-unit>
<trans-unit id="dmail_mailerengine_cron_caution" resname="dmail_mailerengine_cron_caution">
<source>Caution</source>
</trans-unit>
<trans-unit id="dmail_mailerengine_cron_caution_msg" resname="dmail_mailerengine_cron_caution_msg">
<source>Please check the cronjob or cronjob is not set.</source>
</trans-unit>
<trans-unit id="dmail_mailerengine_cron_lastrun" resname="dmail_mailerengine_cron_lastrun">
<source>Last run: </source>
</trans-unit>
<trans-unit id="dmail_mailerengine_cron_ok" resname="dmail_mailerengine_cron_ok">
<source>OK</source>
</trans-unit>
<trans-unit id="dmail_mailerengine_cron_ok_msg" resname="dmail_mailerengine_cron_ok_msg">
<source>Cronjob is running.</source>
</trans-unit>
<trans-unit id="dmail_mailerengine_cron_status" resname="dmail_mailerengine_cron_status">
<source>Cron job status</source>
</trans-unit>
<trans-unit id="dmail_mailerengine_cron_warning" resname="dmail_mailerengine_cron_warning">
<source>Warning</source>
</trans-unit>
<trans-unit id="dmail_mailerengine_cron_warning_msg" resname="dmail_mailerengine_cron_warning_msg">
<source>Please check the cronjob.</source>
</trans-unit>
<trans-unit id="dmail_mailerengine_current_time" resname="dmail_mailerengine_current_time">
<source>Current time:</source>
<trans-unit id="dmail_mailerengine_tasknotfound" resname="dmail_mailerengine_tasknotfound">
<source>Direct Mail scheduled task was not found!</source>
</trans-unit>
<trans-unit id="dmail_mailerengine_delete" resname="dmail_mailerengine_delete">
<source>Delete</source>
Expand Down
105 changes: 55 additions & 50 deletions Resources/Private/Templates/MailerEngine.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,56 +56,61 @@ <h3><f:translate key="{lll}dmail_mailerengine_manual_invoke" /></h3>


<f:if condition="{schedulerTable -> f:count()}">
<table class="table table-striped table-hover">
<thead>
<tr class="t3-row-header">
<th><f:translate key="LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.id"/></th>
<th><f:translate key="LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:task"/></th>
<th><f:translate key="LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.status"/></th>
<th><f:translate key="LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.type"/></th>
<th><f:translate key="LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.frequency"/></th>
<th><f:translate key="LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.parallel"/></th>
<th><f:translate key="LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.lastExecution"/></th>
<th><f:translate key="LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.nextExecution"/></th>
</tr>
</thead>
<tbody>
<f:for each="{schedulerTable}" as="task" iteration="taskIterator">
<f:if condition="{task.validClass}">
<tr class="{task.showAsDisabled}">
<td class="right">{task.uid}</td>
<td class="nowrap-disabled">
<div class="name">
<strong>{task.classTitle} ({task.classExtension})</strong>
<f:if condition="{task.progress}">
<div class="progress">
<div class="progress-bar progress-bar-striped" role="progressbar" aria-valuenow="{task.progress}" aria-valuemin="0" aria-valuemax="100" style="width: {task.progress}%;">{task.progress}%</div>
</div>
<f:then>
<table class="table table-striped table-hover">
<thead>
<tr class="t3-row-header">
<th><f:translate key="LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.id"/></th>
<th><f:translate key="LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:task"/></th>
<th><f:translate key="LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.status"/></th>
<th><f:translate key="LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.type"/></th>
<th><f:translate key="LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.frequency"/></th>
<th><f:translate key="LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.parallel"/></th>
<th><f:translate key="LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.lastExecution"/></th>
<th><f:translate key="LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.nextExecution"/></th>
</tr>
</thead>
<tbody>
<f:for each="{schedulerTable}" as="task" iteration="taskIterator">
<f:if condition="{task.validClass}">
<tr class="{task.showAsDisabled}">
<td class="right">{task.uid}</td>
<td class="nowrap-disabled">
<div class="name">
<strong>{task.classTitle} ({task.classExtension})</strong>
<f:if condition="{task.progress}">
<div class="progress">
<div class="progress-bar progress-bar-striped" role="progressbar" aria-valuenow="{task.progress}" aria-valuemin="0" aria-valuemax="100" style="width: {task.progress}%;">{task.progress}%</div>
</div>
</f:if>
<f:if condition="{task.additionalInformation}">
<div class="additional-information"><f:format.nl2br>{task.additionalInformation}</f:format.nl2br></div>
</f:if>
</div>
<f:if condition="{task.description} != ''">
<div class="text-muted" style="margin-top: 0.5em;"><f:format.nl2br>{task.description}</f:format.nl2br></div>
</f:if>
<f:if condition="{task.additionalInformation}">
<div class="additional-information"><f:format.nl2br>{task.additionalInformation}</f:format.nl2br></div>
</f:if>
</div>
<f:if condition="{task.description} != ''">
<div class="text-muted" style="margin-top: 0.5em;"><f:format.nl2br>{task.description}</f:format.nl2br></div>
</f:if>
</td>
<td>
<f:for each="{task.labels}" as="label">
<f:if condition="{task.validClass}">
<span class="label label-{label.class}" title="{label.description}">{label.text}</span>
</f:if>
</f:for>
</td>
<td>{task.execType}</td>
<td>{task.frequency}</td>
<td>{task.multiple}</td>
<td>{task.lastExecution}</td>
<td><span class="{task.labels.nextexecution.class}">{task.nextExecution}</span></td>
</tr>
</f:if>
</f:for>
</tbody>
</table>
</td>
<td>
<f:for each="{task.labels}" as="label">
<f:if condition="{task.validClass}">
<span class="label label-{label.class}" title="{label.description}">{label.text}</span>
</f:if>
</f:for>
</td>
<td>{task.execType}</td>
<td>{task.frequency}</td>
<td>{task.multiple}</td>
<td>{task.lastExecution}</td>
<td><span class="{task.labels.nextexecution.class}">{task.nextExecution}</span></td>
</tr>
</f:if>
</f:for>
</tbody>
</table>
</f:then>
<f:else>
<p><strong><f:translate key="LLL:EXT:direct_mail/Resources/Private/Language/locallang_mod2-6.xlf:dmail_mailerengine_tasknotfound" /></strong></p>
</f:else>
</f:if>
</f:section>
Loading

0 comments on commit f332377

Please sign in to comment.