Skip to content

Commit

Permalink
Merge pull request #13 from utopia-php/feat-exception-handler
Browse files Browse the repository at this point in the history
error handler for console loop
  • Loading branch information
eldadfux authored Feb 18, 2022
2 parents 5ff128a + 186d564 commit 6d164b7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 7.4
- 8.0
- nightly

Expand Down
14 changes: 11 additions & 3 deletions src/CLI/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,24 @@ static public function isInteractive(): bool
/**
* @param callable $callback
* @param int $sleep in seconds
* @param callable $onError
*/
static public function loop(callable $callback, $sleep = 1 /* 1 second */): void
static public function loop(callable $callback, $sleep = 1 /* 1 second */, callable $onError = null): void
{
gc_enable();

$time = 0;

while (!connection_aborted() || PHP_SAPI == "cli") {

$callback();
try {
$callback();
} catch(\Exception $e) {
if($onError != null) {
$onError($e);
} else {
throw $e;
}
}

sleep($sleep);

Expand Down

0 comments on commit 6d164b7

Please sign in to comment.