diff --git a/Broadcaster/Scheduler.php b/Broadcaster/Scheduler.php index 2340bec..0477dff 100644 --- a/Broadcaster/Scheduler.php +++ b/Broadcaster/Scheduler.php @@ -65,6 +65,7 @@ public function __construct(BroadcastStarter $starter, BroadcastManager $broadca */ public function applySchedule(): void { + $this->broadcastManager->keepConnectionAlive(); $this->stopExpiredBroadcasts(); $this->startPlannedBroadcasts(); $this->sendEndSignals(); diff --git a/Service/BroadcastManager.php b/Service/BroadcastManager.php index 63339d0..1d470b4 100644 --- a/Service/BroadcastManager.php +++ b/Service/BroadcastManager.php @@ -47,6 +47,18 @@ public function __construct(EntityManager $entityManager, ChannelApiStack $apiSt $this->apiStack = $apiStack; } + /** + * Keep a remote connection alive + */ + public function keepConnectionAlive(): void + { + $connection = $this->entityManager->getConnection(); + if (!$connection->ping()) { + $connection->close(); + $connection->connect(); + } + } + /** * Get a broadcast by it's id * diff --git a/Tests/Service/BroadcastManagerTest.php b/Tests/Service/BroadcastManagerTest.php index 3e18c64..67555c7 100644 --- a/Tests/Service/BroadcastManagerTest.php +++ b/Tests/Service/BroadcastManagerTest.php @@ -8,6 +8,7 @@ namespace Martin1982\LiveBroadcastBundle\Tests\Service; use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\DBAL\Connection; use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityRepository; use Doctrine\ORM\OptimisticLockException; @@ -39,6 +40,32 @@ class BroadcastManagerTest extends TestCase */ protected $stack; + /** + * Test connection keepalive method + */ + public function testKeepConnectionAlive(): void + { + $connection = $this->createMock(Connection::class); + $connection->expects(self::atLeastOnce()) + ->method('ping') + ->willReturn(false); + $connection->expects(self::atLeastOnce()) + ->method('close') + ->willReturn(true); + $connection->expects(self::atLeastOnce()) + ->method('connect') + ->willReturn(true); + + /** @var \PHPUnit_Framework_MockObject_MockObject|EntityManager $entityManager */ + $entityManager = $this->createMock(EntityManager::class); + $entityManager->expects(self::atLeastOnce()) + ->method('getConnection') + ->willReturn($connection); + + $manager = new BroadcastManager($entityManager, $this->stack); + $manager->keepConnectionAlive(); + } + /** * Test getting a broadcast entity by id */