Skip to content

Commit

Permalink
add try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
amuluowin committed Jun 22, 2020
1 parent c1f38b8 commit 9ad39bb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
16 changes: 7 additions & 9 deletions src/core/Timer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 10 additions & 2 deletions src/core/TimerCo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace rabbit\core;

use Co\System;
use rabbit\App;
use rabbit\contract\AbstractTimer;
use rabbit\helper\ExceptionHelper;

/**
* Class TimerCo
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 9ad39bb

Please sign in to comment.