来自CRMEB的案例代码:
/**
* 开启定时器
*/
protected function timer(App $app)
{
$last = time();
$task = [6 => $last, 10 => $last, 30 => $last, 60 => $last, 180 => $last, 300 => $last];
$this->timer = Timer::tick($this->interval, function () use (&$task, $app) {
try {
$now = time();
event('Task_2');
foreach ($task as $sec => $time) {
if ($now - $time >= $sec) {
$task[$sec] = $now;
event('Task_' . $sec);
}
}
} catch (\Throwable $e) {
$app->log->error($e->getMessage());
}
});
}
已在www/addons/app/event.php里面添加订阅事件:
'subscribe' => [
app\subscribes\TaskSubscribe::class
],
定时任务运行一段时间挂了,报错:
The app object has not been initialized
/www/wwwroot/zsff.com/addons/vendor/topthink/think-swoole/src/Sandbox.php 135
#0 /www/wwwroot/zsff.com/addons/vendor/topthink/think-swoole/src/Sandbox.php(69): think\\swoole\\Sandbox->getApplication()
#1 /www/wwwroot/zsff.com/addons/vendor/topthink/framework/src/think/Container.php(72): think\\swoole\\Sandbox->think\\swoole\\{closure}()
#2 /www/wwwroot/zsff.com/addons/vendor/topthink/framework/src/think/Facade.php(47): think\\Container::getInstance()
#3 /www/wwwroot/zsff.com/addons/vendor/topthink/framework/src/think/Facade.php(96): think\\Facade::createFacade()
#4 /www/wwwroot/zsff.com/addons/vendor/topthink/framework/src/helper.php(228): think\\Facade::__callStatic(\'trigger\', Array)
#5 /www/wwwroot/zsff.com/addons/app/webscoket/SwooleWorkerStart.php(97): event(\'Task_2\')
#6 {main}
订阅的事件居然不见了,所以导致调用不到。
日志文件里面还有:WARNING Worker_reactor_try_to_exit() (ERRNO 9101): worker exit timeout, forced termination
以下配置没见有效果:
'max_request' => 100000, // 控制每个Worker进程的最大请求次数 https://wiki.swoole.com/#/server/setting?id=max_request
找了不少资料也没能解决,最后!!!
我在catch里面又重新订阅一次事件:
Event::subscribe('app\subscribes\TaskSubscribe');
目前运行还算稳定