Skip to content

Commit

Permalink
Merge pull request #414 from swoftcloud/master
Browse files Browse the repository at this point in the history
Fixed bug and Added feature
  • Loading branch information
stelin committed May 23, 2019
2 parents 1b210d4 + 475b534 commit d41211a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Connection/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,20 @@ public function create(): void
$this->createReadPdo();
}

/**
* Close connection
*/
public function close(): void
{
if(!empty($this->pdo)){
$this->pdo = null;
}

if(!empty($this->readPdo)){
$this->readPdo = null;
}
}

/**
* Reconnect
*/
Expand Down
54 changes: 54 additions & 0 deletions src/Listener/WorkerStopAndErrorListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php declare(strict_types=1);


namespace Swoft\Db\Listener;

use Swoft\Bean\BeanFactory;
use Swoft\Db\Pool;
use Swoft\Event\Annotation\Mapping\Subscriber;
use Swoft\Event\EventInterface;
use Swoft\Event\EventSubscriberInterface;
use Swoft\Log\Helper\CLog;
use Swoft\Server\Swoole\SwooleEvent;
use Swoft\SwoftEvent;
use Swoole\Event;

/**
* Class WorkerStopListener
*
* @since 2.0
*
* @Subscriber()
*/
class WorkerStopAndErrorListener implements EventSubscriberInterface
{
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
SwooleEvent::WORKER_STOP => 'handle',
SwoftEvent::WORKER_SHUTDOWN => 'handle',
];
}

/**
* @param EventInterface $event
*/
public function handle(EventInterface $event): void
{
go(function () use ($event){
$pools = BeanFactory::getBeans(Pool::class);

/* @var Pool $pool */
foreach ($pools as $pool) {
$count = $pool->close();

CLog::info('Close %d database connection on %s!', $count, $event->getName());
}
});

Event::wait();
}
}

0 comments on commit d41211a

Please sign in to comment.