Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/single time msg conf #306

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
108 changes: 77 additions & 31 deletions src/Command/MonitorCommand.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,26 @@
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\ParameterBag;

use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;

#[AsCommand('frosh:monitor', 'Monitor your scheduled tasks and message queue and get notified via email.')]
class MonitorCommand extends Command
{
private const MONITOR_EMAIL_OPTION = 'email';
private const MONITOR_SALESCHANNEL_ARG = 'sales-channel';
private const MONITOR_CACHE_KEY = 'frosh_mail_send_once';

private bool $wasSentBefore = false;

public function __construct(
#[Autowire(service: MailService::class)]
private readonly AbstractMailService $mailService,
private readonly SystemConfigService $configService,
private readonly QueueChecker $queueChecker,
private readonly TaskChecker $taskChecker,
#[Autowire(service: 'cache.object')]
private readonly CacheInterface $cache,
) {
parent::__construct();
}
Expand Down Expand Up @@ -63,38 +71,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return self::INVALID;
}

if ($this->checksFailed()) {
$data = new ParameterBag();
$data->set(
'recipients',
[
$recipientMail => 'Admin',
],
);
$data->set('senderName', 'FroshTools | Admin');

$htmlMailContent = <<<'MAIL'
<div>
<p>
Dear Admin,<br/>
<br/>
your message queue or scheduled tasks are not working as expected.<br/>
<br/>
<br/>
Check your queues and tasks <a href="{{ salesChannel.domains|first.url }}/admin#/frosh/tools/index/index">here</a>
</p>
</div>
MAIL;
$plainMailContent = 'Dear Admin, your message queue or scheduled tasks are not working as expected. Check your queues and tasks {{ salesChannel.domains|first.url }}/admin#/frosh/tools/index/index';

$data->set('contentHtml', $htmlMailContent);
$data->set('contentPlain', $plainMailContent);
$data->set('salesChannelId', $input->getArgument(self::MONITOR_SALESCHANNEL_ARG));
$data->set('subject', 'FroshTools message queue and scheduled task | Warning');

$this->mailService->send($data->all(), $context);
if (!$this->checksFailed()) {
//Remove cache to reset "timer" for next E-Mail
$this->cache->delete(self::MONITOR_CACHE_KEY);
return Command::SUCCESS;
}

if ($this->mailWasSentBefore()) {
return Command::SUCCESS;
}

$this->sendMail($recipientMail, $input, $context);

return self::SUCCESS;
}

Expand All @@ -110,7 +98,65 @@ private function checksFailed(): bool
return true;
}
}

return false;
}

private function mailWasSentBefore(): bool
{
$this->wasSentBefore = true;
$sendOnce = $this->configService->getBool(
'FroshTools.config.monitorTaskSingleMail',
);

if (!$sendOnce) {
return false;
}

$sendLifeTime = $this->configService->getInt(
'FroshTools.config.monitorTaskSingleMailTime',
);

$sendLifeTime = $sendLifeTime * 60;

//send E-Mail on cache miss (No E-Mail was send before)
$this->cache->get(self::MONITOR_CACHE_KEY, function (ItemInterface $item) use ($sendLifeTime): void {
$item->expiresAfter($sendLifeTime);
$this->wasSentBefore = false;
});

return $this->wasSentBefore;
}

private function sendMail(string $recipientMail, InputInterface $input, Context $context): void
{
$data = new ParameterBag();
$data->set(
'recipients',
[
$recipientMail => 'Admin',
],
);
$data->set('senderName', 'FroshTools | Admin');

$htmlMailContent = <<<'MAIL'
<div>
<p>
Dear Admin,<br/>
<br/>
your message queue or scheduled tasks are not working as expected.<br/>
<br/>
<br/>
Check your queues and tasks <a href="{{ salesChannel.domains|first.url }}/admin#/frosh/tools/index/index">here</a>
</p>
</div>
MAIL;
$plainMailContent = 'Dear Admin, your message queue or scheduled tasks are not working as expected. Check your queues and tasks {{ salesChannel.domains|first.url }}/admin#/frosh/tools/index/index';

$data->set('contentHtml', $htmlMailContent);
$data->set('contentPlain', $plainMailContent);
$data->set('salesChannelId', $input->getArgument(self::MONITOR_SALESCHANNEL_ARG));
$data->set('subject', 'FroshTools message queue and scheduled task | Warning');

$this->mailService->send($data->all(), $context);
}
}
18 changes: 18 additions & 0 deletions src/Resources/config/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@
<helpText lang="de-DE">Nach X Minuten gilt eine geplante Aufgabe als festgefahren / fehlerhaft</helpText>
<defaultValue>10</defaultValue>
</input-field>

<input-field type="bool">
<name>monitorTaskSingleMail</name>
<label>Send E-Mail only once</label>
<label lang="de-DE">Mail nur einmal senden</label>
<helpText>Enable this to send only one email notification when the problem is still active.</helpText>
<helpText lang="de-DE">Aktiviere diese Option, um nur eine E-Mail-Benachrichtigung zu senden, wenn das Problem weiterhin besteht.</helpText>
<defaultValue>false</defaultValue>
</input-field>

<input-field type="int">
<name>monitorTaskSingleMailTime</name>
<label>When to allow to send e-mail again</label>
<label lang="de-DE">Wann das erneute Senden von E-Mails erlaubt ist</label>
<helpText>Specify the time interval (in minutes) to allow resending email notifications.</helpText>
<helpText lang="de-DE">Gib das Zeitintervall (in Minuten) an, um das erneute Senden von E-Mail-Benachrichtigungen zu ermöglichen.</helpText>
<defaultValue>60</defaultValue>
</input-field>
</card>

</config>