From 9ad39bbed8a486e8813f527f050e5b3f7d544772 Mon Sep 17 00:00:00 2001 From: albert Date: Mon, 22 Jun 2020 13:59:04 +0800 Subject: [PATCH] add try catch --- src/core/Timer.php | 16 +++++++--------- src/core/TimerCo.php | 12 ++++++++++-- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/core/Timer.php b/src/core/Timer.php index 2eac441..716e749 100644 --- a/src/core/Timer.php +++ b/src/core/Timer.php @@ -51,16 +51,14 @@ public static function addTickTimer(string $name, float $time, callable $callbac { self::checkTimer($name); $channel = new Channel(1); - $tid = rgo(function () use ($name, $channel, $callback, $time, $params) { - while (true) { - if ($ret = $channel->pop($time / 1000)) { - continue; - } - rgo(function () use ($name, $callback, $params) { - self::$timers[$name]['count']++; - call_user_func($callback, ...$params); - }); + $tid = goloop(function () use ($name, $channel, $callback, $time, $params) { + if ($ret = $channel->pop($time / 1000)) { + return; } + rgo(function () use ($name, $callback, $params) { + self::$timers[$name]['count']++; + call_user_func($callback, ...$params); + }); }); self::$timers[$name] = ['name' => $name, 'chan' => $channel, 'tid' => $tid, 'type' => self::TYPE_TICKET, 'count' => 0]; return $tid; diff --git a/src/core/TimerCo.php b/src/core/TimerCo.php index 497f614..bd55610 100644 --- a/src/core/TimerCo.php +++ b/src/core/TimerCo.php @@ -3,7 +3,9 @@ namespace rabbit\core; use Co\System; +use rabbit\App; use rabbit\contract\AbstractTimer; +use rabbit\helper\ExceptionHelper; /** * Class TimerCo @@ -51,8 +53,14 @@ public static function addTickTimer(string $name, float $time, callable $callbac self::$timers[$name] = ['name' => $name, 'type' => self::TYPE_AFTER]; $tid = rgo(function () use ($name, $callback, $time, $params) { while (isset(self::$timers[$name])) { - call_user_func($callback, ...$params); - System::sleep($time / 1000); + try { + call_user_func($callback, ...$params); + } catch (\Throwable $exception) { + App::info($exception->getMessage()); + print_r(ExceptionHelper::convertExceptionToArray($throwable)); + } finally { + System::sleep($time / 1000); + } } }); self::$timers[$name]['tid'] = $tid;