-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pdo ping as periodic timer to avoid server gone error
- Loading branch information
prophet777
committed
May 3, 2015
1 parent
1e797ba
commit acbf88a
Showing
6 changed files
with
121 additions
and
2 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
DependencyInjection/CompilerPass/PingableDriverCompilerPass.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,32 @@ | ||
<?php | ||
|
||
namespace Gos\Bundle\WebSocketBundle\DependencyInjection\CompilerPass; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\Container; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler; | ||
|
||
class PingableDriverCompilerPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* @param ContainerBuilder $container | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$sessionHandler = $container->get('gos_web_socket.session_handler', Container::NULL_ON_INVALID_REFERENCE); | ||
|
||
if(null === $sessionHandler){ | ||
return; | ||
} | ||
|
||
$periodicRegistryDef = $container->getDefinition('gos_web_socket.periodic.registry'); | ||
|
||
if($sessionHandler instanceof PdoSessionHandler){ | ||
$periodicRegistryDef->addMethodCall( | ||
'addPeriodic', [new Reference('gos_web_socket.pdo.periodic_ping')] | ||
); | ||
} | ||
} | ||
} |
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
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
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,77 @@ | ||
<?php | ||
|
||
namespace Gos\Bundle\WebSocketBundle\Periodic; | ||
|
||
use Gos\Bundle\WebSocketBundle\Server\App\Registry\PeriodicRegistry; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\NullLogger; | ||
|
||
class PdoPeriodicPing implements PeriodicInterface | ||
{ | ||
/** | ||
* @var \PDO | ||
*/ | ||
protected $pdo; | ||
|
||
/** | ||
* @var LoggerInterface|NullLogger | ||
*/ | ||
protected $logger; | ||
|
||
/** | ||
* @var int|float | ||
*/ | ||
protected $timeout; | ||
|
||
/** | ||
* @param PeriodicRegistry $periodicRegistry | ||
* @param \PDO $pdo | ||
* @param LoggerInterface $logger | ||
*/ | ||
public function __construct(\PDO $pdo = null, LoggerInterface $logger = null) | ||
{ | ||
$this->pdo = $pdo; | ||
$this->logger = null === $logger ? new NullLogger() : $logger; | ||
$this->timeout = 20; | ||
} | ||
|
||
/** | ||
* @param int|float $timeout | ||
*/ | ||
public function setTimeout($timeout) | ||
{ | ||
$this->timeout = $timeout; | ||
} | ||
|
||
public function tick() | ||
{ | ||
if(null === $this->pdo){ | ||
$this->logger->warning('Unable to ping sql server, service pdo is unavailable'); | ||
return; | ||
} | ||
|
||
//if connection is persistent we don't need to ping | ||
if(true === $this->pdo->getAttribute(\PDO::ATTR_PERSISTENT)){ | ||
return; | ||
} | ||
|
||
try{ | ||
$this->pdo->query('SELECT 1'); | ||
$this->logger->notice('Successfully ping sql server'); | ||
}catch (\PDOException $e){ | ||
$this->logger->emergency('Sql server is gone, and unable to reconnect'); | ||
|
||
throw $e; | ||
} | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getTimeout() | ||
{ | ||
return $this->timeout; | ||
} | ||
|
||
|
||
} |
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
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