From 9658229834d6187f8ab47a1873ecb4c0a133c91b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E7=A7=91?= <997861461@qq.com> Date: Wed, 25 Sep 2024 11:03:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=BF=83=E8=B7=B3=EF=BC=8C=E9=81=BF=E5=85=8D=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E8=BF=9E=E6=8E=A5=E8=B6=85=E6=97=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Webworker.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/Webworker.php b/src/Webworker.php index e56c603..884d879 100644 --- a/src/Webworker.php +++ b/src/Webworker.php @@ -16,8 +16,10 @@ use think\webworker\support\think\App; use think\webworker\support\workerman\Response; use Workerman\Worker; +use Workerman\Timer; use Workerman\Connection\TcpConnection; use Workerman\Protocols\Http\Request; +use Throwable; class Webworker { @@ -173,6 +175,30 @@ public function onWorkerStart(Worker $worker): void $this->app = new App(); // 初始化 $this->app->initialize(); + + $this->db_hart(); + } + + /** + * 避免数据库连接超时 + * @access public + * @return void + */ + public function db_heart(): void + { + $db = $this->app->db; + Timer::add(55, function() use($db) { + $intances = $db->getInstance(); + if (count($intances) <= 0) { + return; + } + + foreach($intances as $connection) { + try { + $connection->query('select 1'); + } catch (Throwable $e) {} + } + }); } /**