From b5a589d4da794b534ad1875d207109bae9d12ae7 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Sat, 8 Aug 2020 20:43:58 +0800 Subject: [PATCH] =?UTF-8?q?update:=E6=96=B0=E5=A2=9E=E8=BF=9B=E7=A8=8B?= =?UTF-8?q?=E6=9C=80=E5=A4=A7=E5=86=85=E5=AD=98=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extend/catcher/library/crontab/Process.php | 32 +++++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/extend/catcher/library/crontab/Process.php b/extend/catcher/library/crontab/Process.php index 3cd18d7..1fec663 100644 --- a/extend/catcher/library/crontab/Process.php +++ b/extend/catcher/library/crontab/Process.php @@ -16,16 +16,34 @@ use think\facade\Log; trait Process { + /** + * quit 退出 + * + * @var boolean + */ + protected $quit =false; + + /** + * 设置最大内存/256M + * + * @var [type] + */ + protected $maxMemory = 256 * 1024 * 1024; + + /** + * 创建进程 + * + * @return void + */ protected function createProcessCallback() { return function (\Swoole\Process $process) { - $quit = false; // 必须使用 pcntl signal 注册捕获 // Swoole\Process::signal ignalfd 和 EventLoop 是异步 IO,不能用于阻塞的程序中,会导致注册的监听回调函数得不到调度 // 同步阻塞的程序可以使用 pcntl 扩展提供的 pcntl_signal // 安全退出进程 - pcntl_signal(SIGTERM, function() use (&$quit){ - $quit = true; + pcntl_signal(SIGTERM, function() { + $this->quit = true; }); pcntl_signal(SIGUSR1, function () use ($process){ @@ -49,8 +67,14 @@ trait Process } pcntl_signal_dispatch(); sleep(1); + + // 超过最大内存 + if (memory_get_usage() > $this->maxMemory) { + $this->quit = true; + } + // 如果收到安全退出的信号,需要在最后任务处理完成之后退出 - if ($quit) { + if ($this->quit) { Log::info('worker quit'); $process->exit(0); }