Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BridgeDoctrineMessenger] Doctrine ping connection middleware
- Loading branch information
1 parent
713aab7
commit 6fd9f6a
Showing
5 changed files
with
248 additions
and
0 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
57 changes: 57 additions & 0 deletions
57
src/Symfony/Bridge/Doctrine/Messenger/DoctrineCloseConnectionMiddleware.php
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,57 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Messenger; | ||
|
||
use Doctrine\Common\Persistence\ManagerRegistry; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Middleware\MiddlewareInterface; | ||
use Symfony\Component\Messenger\Middleware\StackInterface; | ||
|
||
/** | ||
* Closes connection and therefore saves number of connections. | ||
* | ||
* @author Fuong <insidestyles@gmail.com> | ||
* | ||
* @experimental in 4.3 | ||
*/ | ||
class DoctrineCloseConnectionMiddleware implements MiddlewareInterface | ||
{ | ||
private $managerRegistry; | ||
private $entityManagerName; | ||
|
||
public function __construct(ManagerRegistry $managerRegistry, string $entityManagerName = null) | ||
{ | ||
$this->managerRegistry = $managerRegistry; | ||
$this->entityManagerName = $entityManagerName; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handle(Envelope $envelope, StackInterface $stack): Envelope | ||
{ | ||
$entityManager = $this->managerRegistry->getManager($this->entityManagerName); | ||
|
||
if (!$entityManager instanceof EntityManagerInterface) { | ||
throw new \InvalidArgumentException(sprintf('The ObjectManager with name "%s" must be an instance of EntityManagerInterface', $this->entityManagerName)); | ||
} | ||
|
||
try { | ||
$connection = $entityManager->getConnection(); | ||
|
||
return $stack->next()->handle($envelope, $stack); | ||
} finally { | ||
$connection->close(); | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/Symfony/Bridge/Doctrine/Messenger/DoctrinePingConnectionMiddleware.php
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,62 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Messenger; | ||
|
||
use Doctrine\Common\Persistence\ManagerRegistry; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Middleware\MiddlewareInterface; | ||
use Symfony\Component\Messenger\Middleware\StackInterface; | ||
|
||
/** | ||
* Checks whether the connection is still open or reconnects otherwise. | ||
* | ||
* @author Fuong <insidestyles@gmail.com> | ||
* | ||
* @experimental in 4.3 | ||
*/ | ||
class DoctrinePingConnectionMiddleware implements MiddlewareInterface | ||
{ | ||
private $managerRegistry; | ||
private $entityManagerName; | ||
|
||
public function __construct(ManagerRegistry $managerRegistry, string $entityManagerName = null) | ||
{ | ||
$this->managerRegistry = $managerRegistry; | ||
$this->entityManagerName = $entityManagerName; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handle(Envelope $envelope, StackInterface $stack): Envelope | ||
{ | ||
$entityManager = $this->managerRegistry->getManager($this->entityManagerName); | ||
|
||
if (!$entityManager instanceof EntityManagerInterface) { | ||
throw new \InvalidArgumentException(sprintf('The ObjectManager with name "%s" must be an instance of EntityManagerInterface', $this->entityManagerName)); | ||
} | ||
|
||
$connection = $entityManager->getConnection(); | ||
|
||
if (!$connection->ping()) { | ||
$connection->close(); | ||
$connection->connect(); | ||
} | ||
|
||
if (!$entityManager->isOpen()) { | ||
$this->managerRegistry->resetManager($this->entityManagerName); | ||
} | ||
|
||
return $stack->next()->handle($envelope, $stack); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrineCloseConnectionMiddlewareTest.php
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,53 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Tests\Messenger; | ||
|
||
use Doctrine\Common\Persistence\ManagerRegistry; | ||
use Doctrine\DBAL\Connection; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Symfony\Bridge\Doctrine\Messenger\DoctrineCloseConnectionMiddleware; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase; | ||
|
||
class DoctrineCloseConnectionMiddlewareTest extends MiddlewareTestCase | ||
{ | ||
private $connection; | ||
private $entityManager; | ||
private $managerRegistry; | ||
private $middleware; | ||
private $entityManagerName = 'default'; | ||
|
||
protected function setUp() | ||
{ | ||
$this->connection = $this->createMock(Connection::class); | ||
|
||
$this->entityManager = $this->createMock(EntityManagerInterface::class); | ||
$this->entityManager->method('getConnection')->willReturn($this->connection); | ||
|
||
$this->managerRegistry = $this->createMock(ManagerRegistry::class); | ||
$this->managerRegistry->method('getManager')->willReturn($this->entityManager); | ||
|
||
$this->middleware = new DoctrineCloseConnectionMiddleware( | ||
$this->managerRegistry, | ||
$this->entityManagerName | ||
); | ||
} | ||
|
||
public function testMiddlewareCloseConnection() | ||
{ | ||
$this->connection->expects($this->once()) | ||
->method('close') | ||
; | ||
|
||
$this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock()); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrinePingConnectionMiddlewareTest.php
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,74 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Tests\Messenger; | ||
|
||
use Doctrine\Common\Persistence\ManagerRegistry; | ||
use Doctrine\DBAL\Connection; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Symfony\Bridge\Doctrine\Messenger\DoctrinePingConnectionMiddleware; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase; | ||
|
||
class DoctrinePingConnectionMiddlewareTest extends MiddlewareTestCase | ||
{ | ||
private $connection; | ||
private $entityManager; | ||
private $managerRegistry; | ||
private $middleware; | ||
private $entityManagerName = 'default'; | ||
|
||
protected function setUp() | ||
{ | ||
$this->connection = $this->createMock(Connection::class); | ||
|
||
$this->entityManager = $this->createMock(EntityManagerInterface::class); | ||
$this->entityManager->method('getConnection')->willReturn($this->connection); | ||
|
||
$this->managerRegistry = $this->createMock(ManagerRegistry::class); | ||
$this->managerRegistry->method('getManager')->willReturn($this->entityManager); | ||
|
||
$this->middleware = new DoctrinePingConnectionMiddleware( | ||
$this->managerRegistry, | ||
$this->entityManagerName | ||
); | ||
} | ||
|
||
public function testMiddlewarePingOk() | ||
{ | ||
$this->connection->expects($this->once()) | ||
->method('ping') | ||
->willReturn(false); | ||
|
||
$this->connection->expects($this->once()) | ||
->method('close') | ||
; | ||
$this->connection->expects($this->once()) | ||
->method('connect') | ||
; | ||
|
||
$this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock()); | ||
} | ||
|
||
public function testMiddlewarePingResetEntityManager() | ||
{ | ||
$this->entityManager->expects($this->once()) | ||
->method('isOpen') | ||
->willReturn(false) | ||
; | ||
$this->managerRegistry->expects($this->once()) | ||
->method('resetManager') | ||
->with($this->entityManagerName) | ||
; | ||
|
||
$this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock()); | ||
} | ||
} |