Last active
June 15, 2023 03:50
-
-
Save ganyicz/35175bf45377a87eeeb9c9dcfd537575 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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