Skip to content

Instantly share code, notes, and snippets.

@ganyicz
Last active June 15, 2023 03:50
Show Gist options
  • Save ganyicz/35175bf45377a87eeeb9c9dcfd537575 to your computer and use it in GitHub Desktop.
Save ganyicz/35175bf45377a87eeeb9c9dcfd537575 to your computer and use it in GitHub Desktop.
<?php
namespace App\Console\Commands\Concerns;
use React\EventLoop\Loop;
trait RunsLoop
{
protected function loop(callable $callback, float $interval, float $lifetime): void
{
if (app()->runningUnitTests()) {
$callback();
return;
}
$loop = Loop::get();
$timer = $loop->addPeriodicTimer($interval, function () use ($callback, $loop) {
if (app()->isDownForMaintenance() === false) {
rescue($callback);
}
});
$loop->addTimer($lifetime, function () use ($loop, $timer) {
$loop->cancelTimer($timer);
});
$loop->run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment