-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #414 from swoftcloud/master
Fixed bug and Added feature
- Loading branch information
Showing
2 changed files
with
68 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
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,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(); | ||
} | ||
} |