-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
169 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
||
use Liip\Monitor\Event\PostRunCheckSuiteEvent; | ||
use Liip\Monitor\EventListener\MailerSubscriber; | ||
|
||
return static function (ContainerConfigurator $container): void { | ||
$container->services() | ||
->set('.liip_monitor.mailer_subscriber', MailerSubscriber::class) | ||
->args([service('mailer')]) | ||
->tag('kernel.event_listener', ['event' => PostRunCheckSuiteEvent::class, 'method' => 'afterSuite']) | ||
; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the liip/monitor-bundle package. | ||
* | ||
* (c) Liip | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Liip\Monitor\EventListener; | ||
|
||
use Liip\Monitor\Event\PostRunCheckSuiteEvent; | ||
use Liip\Monitor\Result\ResultContext; | ||
use Liip\Monitor\Result\Status; | ||
use Symfony\Component\Mailer\MailerInterface; | ||
use Symfony\Component\Mime\Email; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class MailerSubscriber | ||
{ | ||
/** | ||
* @param array{ | ||
* recipient: string[], | ||
* sender: ?string, | ||
* subject: string, | ||
* send_on_warning: bool, | ||
* send_on_skip: bool, | ||
* send_on_unknown: bool, | ||
* } $config | ||
*/ | ||
public function __construct(private MailerInterface $mailer, private array $config) | ||
{ | ||
} | ||
|
||
public function afterSuite(PostRunCheckSuiteEvent $event): void | ||
{ | ||
$results = $event->results(); | ||
$failureStatuses = []; | ||
|
||
if ($this->config['send_on_warning']) { | ||
$failureStatuses[] = Status::WARNING; | ||
} | ||
|
||
if ($this->config['send_on_skip']) { | ||
$failureStatuses[] = Status::SKIP; | ||
} | ||
|
||
if ($this->config['send_on_unknown']) { | ||
$failureStatuses[] = Status::UNKNOWN; | ||
} | ||
|
||
$defects = $results->defects(...$failureStatuses); | ||
|
||
if (0 === $defects->count()) { | ||
return; | ||
} | ||
|
||
$body = \implode( | ||
"\n", | ||
\array_map( | ||
static fn(ResultContext $result) => \sprintf( | ||
'[%s] %s', | ||
$result->check(), | ||
$result, | ||
), | ||
$defects->all(), | ||
), | ||
); | ||
|
||
$message = (new Email()) | ||
->to(...$this->config['recipient']) | ||
->subject($this->config['subject']) | ||
->text($body) | ||
; | ||
|
||
if ($this->config['sender']) { | ||
$message->from($this->config['sender']); | ||
} | ||
|
||
$this->mailer->send($message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\HttpKernel\Kernel; | ||
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; | ||
use Zenstruck\Mailer\Test\ZenstruckMailerTestBundle; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
|
@@ -32,6 +33,7 @@ public function registerBundles(): iterable | |
{ | ||
yield new FrameworkBundle(); | ||
yield new DoctrineBundle(); | ||
yield new ZenstruckMailerTestBundle(); | ||
yield new LiipMonitorBundle(); | ||
} | ||
|
||
|
@@ -43,6 +45,9 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load | |
'router' => ['utf8' => true], | ||
'test' => true, | ||
'messenger' => true, | ||
'mailer' => [ | ||
'dsn' => 'null://null', | ||
], | ||
]); | ||
|
||
$c->loadFromExtension('doctrine', [ | ||
|
@@ -56,6 +61,10 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load | |
|
||
$c->loadFromExtension('liip_monitor', [ | ||
'logging' => true, | ||
'mailer' => [ | ||
'sender' => '[email protected]', | ||
'recipient' => '[email protected]', | ||
], | ||
'checks' => [ | ||
'system_memory_usage' => true, | ||
'system_disk_usage' => true, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,13 +23,15 @@ | |
use Symfony\Component\Messenger\HandleTrait; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Zenstruck\Console\Test\InteractsWithConsole; | ||
use Zenstruck\Mailer\Test\InteractsWithMailer; | ||
use Zenstruck\Mailer\Test\TestEmail; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
*/ | ||
final class LiipMonitorBundleTest extends KernelTestCase | ||
{ | ||
use HandleTrait, InteractsWithConsole; | ||
use HandleTrait, InteractsWithConsole, InteractsWithMailer; | ||
|
||
/** | ||
* @test | ||
|
@@ -73,6 +75,18 @@ public function execute_health_command(): void | |
->assertOutputContains('OK Check Service 1: Success') | ||
; | ||
|
||
$this->mailer() | ||
->assertEmailSentTo('[email protected]', function(TestEmail $email) { | ||
->assertFrom('[email protected]') | ||
->assertSubjectContains('Health Check Failed') | ||
->assertTextContains('[Custom Check Service 2] failed') | ||
; | ||
}) | ||
->sentEmails() | ||
->assertCount(1) | ||
; | ||
|
||
$this->executeConsoleCommand('monitor:health -v') // verbose | ||
->assertOutputContains('19 check executed') | ||
; | ||
|