From ca767699a7ecfec3fb26d31ee55e0d0407c2dc34 Mon Sep 17 00:00:00 2001 From: Loulier Guillaume Date: Tue, 8 Jun 2021 16:05:33 +0200 Subject: [PATCH 1/2] feat(core): SlowTaskMiddleware started --- .../SchedulerBundleConfiguration.php | 4 ++ src/Middleware/SlowTaskNotifierMiddleware.php | 48 +++++++++++++++++++ .../SchedulerBundleConfigurationTest.php | 31 ++++++++++++ .../SlowTaskNotifierMiddlewareTest.php | 14 ++++++ 4 files changed, 97 insertions(+) create mode 100644 src/Middleware/SlowTaskNotifierMiddleware.php create mode 100644 tests/Middleware/SlowTaskNotifierMiddlewareTest.php diff --git a/src/DependencyInjection/SchedulerBundleConfiguration.php b/src/DependencyInjection/SchedulerBundleConfiguration.php index c1f2baf4..854191bd 100644 --- a/src/DependencyInjection/SchedulerBundleConfiguration.php +++ b/src/DependencyInjection/SchedulerBundleConfiguration.php @@ -142,6 +142,10 @@ public function getConfigTreeBuilder(): TreeBuilder ->info('Define the jwt token') ->defaultNull() ->end() + ->scalarNode('on_slow_task') + ->info('Enable an update on slow task execution') + ->defaultFalse() + ->end() ->end() ->end() ->arrayNode('transport') diff --git a/src/Middleware/SlowTaskNotifierMiddleware.php b/src/Middleware/SlowTaskNotifierMiddleware.php new file mode 100644 index 00000000..0da6249a --- /dev/null +++ b/src/Middleware/SlowTaskNotifierMiddleware.php @@ -0,0 +1,48 @@ + + */ +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'), + ], + ]))); + } +} diff --git a/tests/DependencyInjection/SchedulerBundleConfigurationTest.php b/tests/DependencyInjection/SchedulerBundleConfigurationTest.php index d2d6f4c3..6b678640 100644 --- a/tests/DependencyInjection/SchedulerBundleConfigurationTest.php +++ b/tests/DependencyInjection/SchedulerBundleConfigurationTest.php @@ -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); } @@ -455,4 +456,34 @@ public function testMercureSupportCanBeEnabled(): void self::assertArrayHasKey('jwt_token', $configuration['mercure']); self::assertNull($configuration['mercure']['jwt_token']); } + + public function testMercureSlowDownTaskSupportCanBeEnabled(): void + { + $configuration = (new Processor())->processConfiguration(new SchedulerBundleConfiguration(), [ + 'scheduler_bundle' => [ + 'transport' => [ + 'dsn' => 'cache://app', + ], + 'mercure' => [ + 'enabled' => true, + 'hub_url' => 'https://www.foo.com', + 'update_url' => 'https://www.bar.com', + 'on_slow_task' => true, + ], + ], + ]); + + self::assertArrayHasKey('mercure', $configuration); + self::assertCount(4, $configuration['mercure']); + self::assertArrayHasKey('enabled', $configuration['mercure']); + self::assertTrue($configuration['mercure']['enabled']); + self::assertArrayHasKey('hub_url', $configuration['mercure']); + self::assertSame('https://www.foo.com', $configuration['mercure']['hub_url']); + self::assertArrayHasKey('update_url', $configuration['mercure']); + self::assertSame('https://www.bar.com', $configuration['mercure']['update_url']); + self::assertArrayHasKey('jwt_token', $configuration['mercure']); + self::assertNull($configuration['mercure']['jwt_token']); + self::assertArrayHasKey('on_slow_task', $configuration['mercure']); + self::assertTrue($configuration['mercure']['on_slow_task']); + } } diff --git a/tests/Middleware/SlowTaskNotifierMiddlewareTest.php b/tests/Middleware/SlowTaskNotifierMiddlewareTest.php new file mode 100644 index 00000000..efebb79b --- /dev/null +++ b/tests/Middleware/SlowTaskNotifierMiddlewareTest.php @@ -0,0 +1,14 @@ + + */ +final class SlowTaskNotifierMiddlewareTest extends TestCase +{ +} From 876aa5b98a77fd879a0686e5a81ed74ade5c6d51 Mon Sep 17 00:00:00 2001 From: Loulier Guillaume Date: Sun, 20 Jun 2021 18:24:17 +0200 Subject: [PATCH 2/2] refactor(DIC): rollback --- .../SchedulerBundleConfiguration.php | 4 --- .../SchedulerBundleConfigurationTest.php | 30 ------------------- 2 files changed, 34 deletions(-) diff --git a/src/DependencyInjection/SchedulerBundleConfiguration.php b/src/DependencyInjection/SchedulerBundleConfiguration.php index 854191bd..c1f2baf4 100644 --- a/src/DependencyInjection/SchedulerBundleConfiguration.php +++ b/src/DependencyInjection/SchedulerBundleConfiguration.php @@ -142,10 +142,6 @@ public function getConfigTreeBuilder(): TreeBuilder ->info('Define the jwt token') ->defaultNull() ->end() - ->scalarNode('on_slow_task') - ->info('Enable an update on slow task execution') - ->defaultFalse() - ->end() ->end() ->end() ->arrayNode('transport') diff --git a/tests/DependencyInjection/SchedulerBundleConfigurationTest.php b/tests/DependencyInjection/SchedulerBundleConfigurationTest.php index 6b678640..2d7bd543 100644 --- a/tests/DependencyInjection/SchedulerBundleConfigurationTest.php +++ b/tests/DependencyInjection/SchedulerBundleConfigurationTest.php @@ -456,34 +456,4 @@ public function testMercureSupportCanBeEnabled(): void self::assertArrayHasKey('jwt_token', $configuration['mercure']); self::assertNull($configuration['mercure']['jwt_token']); } - - public function testMercureSlowDownTaskSupportCanBeEnabled(): void - { - $configuration = (new Processor())->processConfiguration(new SchedulerBundleConfiguration(), [ - 'scheduler_bundle' => [ - 'transport' => [ - 'dsn' => 'cache://app', - ], - 'mercure' => [ - 'enabled' => true, - 'hub_url' => 'https://www.foo.com', - 'update_url' => 'https://www.bar.com', - 'on_slow_task' => true, - ], - ], - ]); - - self::assertArrayHasKey('mercure', $configuration); - self::assertCount(4, $configuration['mercure']); - self::assertArrayHasKey('enabled', $configuration['mercure']); - self::assertTrue($configuration['mercure']['enabled']); - self::assertArrayHasKey('hub_url', $configuration['mercure']); - self::assertSame('https://www.foo.com', $configuration['mercure']['hub_url']); - self::assertArrayHasKey('update_url', $configuration['mercure']); - self::assertSame('https://www.bar.com', $configuration['mercure']['update_url']); - self::assertArrayHasKey('jwt_token', $configuration['mercure']); - self::assertNull($configuration['mercure']['jwt_token']); - self::assertArrayHasKey('on_slow_task', $configuration['mercure']); - self::assertTrue($configuration['mercure']['on_slow_task']); - } }