Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/Middleware/SlowTaskNotifierMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace SchedulerBundle\Middleware;

use SchedulerBundle\Task\TaskInterface;
use Symfony\Component\Mercure\HubInterface;
use Symfony\Component\Mercure\Update;
use Symfony\Component\Serializer\SerializerInterface;
use function json_encode;

/**
* @author Guillaume Loulier <[email protected]>
*/
final class SlowTaskNotifierMiddleware implements PostExecutionMiddlewareInterface
{
private HubInterface $hub;
private string $updateUrl;
private SerializerInterface $serializer;

public function __construct(
HubInterface $hub,
string $updateUrl,
SerializerInterface $serializer
) {
$this->hub = $hub;
$this->updateUrl = $updateUrl;
$this->serializer = $serializer;
}

/**
* {@inheritdoc}
*/
public function postExecute(TaskInterface $task): void
{
if (!$this->hub instanceof HubInterface) {
return;
}

$this->hub->publish(new Update($this->updateUrl, json_encode([
'event' => 'task.slow_execution',
'body' => [
'task' => $this->serializer->serialize($task, 'json'),
],
])));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function testConfigurationCanBeEmpty(): void
self::assertArrayHasKey('timezone', $configuration);
self::assertArrayHasKey('tasks', $configuration);
self::assertArrayNotHasKey('probe', $configuration);
self::assertArrayNotHasKey('notifier', $configuration);
self::assertArrayHasKey('lock_store', $configuration);
}

Expand Down
14 changes: 14 additions & 0 deletions tests/Middleware/SlowTaskNotifierMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Tests\SchedulerBundle\Middleware;

use PHPUnit\Framework\TestCase;

/**
* @author Guillaume Loulier <[email protected]>
*/
final class SlowTaskNotifierMiddlewareTest extends TestCase
{
}