Skip to content

Commit e2c6a19

Browse files
committed
Register with the loop
1 parent dfa6e76 commit e2c6a19

7 files changed

Lines changed: 9 additions & 65 deletions

File tree

docs/periodic-manager.md

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,9 @@ Periodic managers are initialized during the `BabDev\WebSocketBundle\Event\Befor
1010

1111
A manager must provide a unique name, these names are used in conjunction with the `BabDev\WebSocketBundle\PeriodicManager\PeriodicManagerRegistry`
1212

13-
### `setLoop()`
14-
15-
When managers are registered, they will be provided the event loop that is being used for the server process.
16-
1713
### `register()`
1814

19-
The `register()` method is used to initialize the periodic manager.
15+
The `register()` method is used to initialize the periodic manager using the provided event loop.
2016

2117
### `cancelTimers()`
2218

@@ -29,7 +25,6 @@ The `cancelTimers()` method is called during the `BabDev\WebSocketBundle\Event\A
2925

3026
namespace App\WebSocket\PeriodicManager;
3127

32-
use BabDev\WebSocketBundle\Exception\MissingLoop;
3328
use BabDev\WebSocketBundle\PeriodicManager\PeriodicManager;
3429
use Psr\Log\LoggerAwareInterface;
3530
use Psr\Log\LoggerAwareTrait;
@@ -46,33 +41,19 @@ final class EchoPeriodicManager implements PeriodicManager, LoggerAwareInterface
4641
return 'echo'
4742
}
4843

49-
public function setLoop(LoopInterface $loop): void
44+
public function register(LoopInterface $loop): void
5045
{
5146
$this->loop = $loop;
52-
}
5347

54-
public function register(): void
55-
{
5648
// Wrap the entire loop in try/catch to prevent fatal errors crashing the websocket server
5749
try {
58-
if (null === $this->loop) {
59-
throw new MissingLoop(sprintf('The event loop has not been registered in %s', self::class));
60-
}
61-
6250
// Register the timer to run every 15 seconds
6351
$this->loop->addPeriodicTimer(
6452
15,
6553
static function (): void {
6654
echo 'This is a demo';
6755
},
6856
);
69-
} catch (MissingLoopException $exception) {
70-
$this->logger->error(
71-
$exception->getMessage(),
72-
[
73-
'exception' => $exception,
74-
],
75-
);
7657
} catch (\Throwable $exception) {
7758
$this->logger->error(
7859
'Uncaught Throwable in the echo manager.',

src/EventListener/PeriodicManagerSubscriber.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@ public function onAfterServerClosed(AfterServerClosed $event): void
3131

3232
public function onBeforeRunServer(BeforeRunServer $event): void
3333
{
34-
$loop = $event->loop;
35-
3634
foreach ($this->registry->getManagers() as $manager) {
37-
$manager->setLoop($loop);
38-
$manager->register();
35+
$manager->register($event->loop);
3936
}
4037
}
4138
}

src/Exception/MissingLoop.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/PeriodicManager/PeriodicManager.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,13 @@
22

33
namespace BabDev\WebSocketBundle\PeriodicManager;
44

5-
use BabDev\WebSocketBundle\Exception\MissingLoop;
65
use React\EventLoop\LoopInterface;
76

87
interface PeriodicManager
98
{
109
public function getName(): string;
1110

12-
/**
13-
* @throws MissingLoop if called before setting the event loop
14-
*/
15-
public function register(): void;
11+
public function register(LoopInterface $loop): void;
1612

1713
public function cancelTimers(): void;
18-
19-
public function setLoop(LoopInterface $loop): void;
2014
}

src/PeriodicManager/PingDoctrineDBALConnectionsPeriodicManager.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace BabDev\WebSocketBundle\PeriodicManager;
44

5-
use BabDev\WebSocketBundle\Exception\MissingLoop;
65
use Doctrine\DBAL\Connection;
76
use Doctrine\DBAL\Exception as DBALException;
87
use Psr\Log\LoggerAwareInterface;
@@ -30,22 +29,18 @@ public function getName(): string
3029
return 'ping_doctrine_dbal_connections';
3130
}
3231

33-
public function register(): void
32+
public function register(LoopInterface $loop): void
3433
{
34+
$this->loop = $loop;
35+
3536
// Wrap the entire loop in try/catch to prevent fatal errors crashing the websocket server
3637
try {
3738
$this->logger?->info('Registering ping doctrine/dbal connections manager.');
3839

39-
if (!$this->loop instanceof LoopInterface) {
40-
throw new MissingLoop(sprintf('The event loop has not been registered in %s', self::class));
41-
}
42-
4340
$this->timer = $this->loop->addPeriodicTimer(
4441
$this->interval,
4542
$this->pingConnections(...),
4643
);
47-
} catch (MissingLoop $exception) {
48-
$this->logger?->error($exception->getMessage(), ['exception' => $exception]);
4944
} catch (\Throwable $exception) {
5045
$this->logger?->error('Uncaught Throwable in the ping doctrine/dbal connections loop.', ['exception' => $exception]);
5146
}
@@ -56,11 +51,6 @@ public function cancelTimers(): void
5651
$this->reset();
5752
}
5853

59-
public function setLoop(LoopInterface $loop): void
60-
{
61-
$this->loop = $loop;
62-
}
63-
6454
/**
6555
* @internal
6656
*/

tests/EventListener/PeriodicManagerSubscriberTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,9 @@ public function testInitializesPeriodicManagers(): void
5757
/** @var MockObject&PeriodicManager $manager */
5858
$manager = $this->createMock(PeriodicManager::class);
5959
$manager->expects(self::once())
60-
->method('setLoop')
60+
->method('register')
6161
->with($loop);
6262

63-
$manager->expects(self::once())
64-
->method('register');
65-
6663
$this->registry->method('getManagers')
6764
->willReturn(['test' => $manager]);
6865

tests/PeriodicManager/PingDoctrineDBALConnectionsPeriodicManagerTest.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,6 @@ protected function tearDown(): void
4343
parent::tearDown();
4444
}
4545

46-
public function testTheManagerIsNotRegisteredWhenTheLoopIsMissing(): void
47-
{
48-
$this->manager->register();
49-
50-
self::assertTrue($this->logger->hasErrorThatContains(sprintf('The event loop has not been registered in %s', PingDoctrineDBALConnectionsPeriodicManager::class)));
51-
}
52-
5346
public function testTheManagerIsRegistered(): void
5447
{
5548
/** @var MockObject&LoopInterface $loop */
@@ -59,8 +52,7 @@ public function testTheManagerIsRegistered(): void
5952
->method('addPeriodicTimer')
6053
->willReturn($this->createMock(TimerInterface::class));
6154

62-
$this->manager->setLoop($loop);
63-
$this->manager->register();
55+
$this->manager->register($loop);
6456
}
6557

6658
public function testTheManagerPingsAllConnections(): void

0 commit comments

Comments
 (0)