Skip to content

Commit

Permalink
error handler for cli loop
Browse files Browse the repository at this point in the history
  • Loading branch information
lohanidamodar committed Feb 18, 2022
1 parent 5ff128a commit 0ab8f2f
Showing 1 changed file with 11 additions and 3 deletions.
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 0ab8f2f

Please sign in to comment.