diff --git a/src/Connection/Connection.php b/src/Connection/Connection.php index 2d4f532..6f3465d 100644 --- a/src/Connection/Connection.php +++ b/src/Connection/Connection.php @@ -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 */ diff --git a/src/Listener/WorkerStopAndErrorListener.php b/src/Listener/WorkerStopAndErrorListener.php new file mode 100644 index 0000000..a434470 --- /dev/null +++ b/src/Listener/WorkerStopAndErrorListener.php @@ -0,0 +1,54 @@ + '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(); + } +} \ No newline at end of file