-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathDoctrinePingConnectionExtension.php
46 lines (37 loc) · 1.21 KB
/
DoctrinePingConnectionExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
namespace Enqueue\Bundle\Consumption\Extension;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\DBAL\Connection;
use Enqueue\Consumption\Context\MessageReceived;
use Enqueue\Consumption\MessageReceivedExtensionInterface;
class DoctrinePingConnectionExtension implements MessageReceivedExtensionInterface
{
/**
* @var ManagerRegistry
*/
protected $registry;
public function __construct(ManagerRegistry $registry)
{
$this->registry = $registry;
}
public function onMessageReceived(MessageReceived $context): void
{
/** @var Connection $connection */
foreach ($this->registry->getConnections() as $connection) {
if (!$connection->isConnected()) {
continue;
}
if ($connection->ping()) {
continue;
}
$context->getLogger()->debug(
'[DoctrinePingConnectionExtension] Connection is not active trying to reconnect.'
);
$connection->close();
$connection->connect();
$context->getLogger()->debug(
'[DoctrinePingConnectionExtension] Connection is active now.'
);
}
}
}