1
0
mirror of https://github.com/danog/loop.git synced 2024-11-26 20:04:44 +01:00
loop/lib/Loop.php

265 lines
7.2 KiB
PHP
Raw Permalink Normal View History

2022-12-24 14:36:39 +01:00
<?php declare(strict_types=1);
2023-01-23 00:24:18 +01:00
2020-07-21 18:06:19 +02:00
/**
2023-01-23 00:24:18 +01:00
* Generic loop.
2020-07-21 18:06:19 +02:00
*
* @author Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2020 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/MIT MIT
*/
2020-07-21 21:45:22 +02:00
namespace danog\Loop;
2020-07-21 18:06:19 +02:00
2023-01-24 15:49:37 +01:00
use Amp\DeferredFuture;
2023-01-24 12:31:57 +01:00
use AssertionError;
2023-01-22 21:59:13 +01:00
use Revolt\EventLoop;
2023-01-23 00:24:18 +01:00
use Stringable;
2020-07-21 18:06:19 +02:00
/**
2023-01-23 00:24:18 +01:00
* Generic loop, runs single callable.
2020-07-21 18:06:19 +02:00
*
2023-09-30 14:26:17 +02:00
* @api
*
2020-07-21 18:06:19 +02:00
* @author Daniil Gentili <daniil@daniil.it>
*/
2023-01-23 00:24:18 +01:00
abstract class Loop implements Stringable
2020-07-21 18:06:19 +02:00
{
2023-01-22 21:59:13 +01:00
/**
2023-01-23 00:24:18 +01:00
* Stop the loop.
*/
2023-01-23 22:18:52 +01:00
public const STOP = -1.0;
2023-01-23 00:24:18 +01:00
/**
* Pause the loop.
*/
2023-01-23 22:18:52 +01:00
public const PAUSE = null;
2023-01-23 00:24:18 +01:00
/**
* Rerun the loop.
*/
2023-01-23 22:18:52 +01:00
public const CONTINUE = 0.0;
2023-01-23 00:24:18 +01:00
/**
* Whether the loop is running.
*/
private bool $running = false;
/**
* Resume timer ID.
*/
private ?string $resumeTimer = null;
/**
* Resume deferred ID.
*/
2023-01-23 00:38:49 +01:00
private ?string $resumeImmediate = null;
2023-01-24 15:49:37 +01:00
/**
* Shutdown deferred.
*/
private ?DeferredFuture $shutdownDeferred = null;
2023-01-23 00:24:18 +01:00
/**
* Report pause, can be overriden for logging.
*
2023-09-30 14:26:17 +02:00
* @psalm-suppress PossiblyUnusedParam
*
2023-01-23 00:24:18 +01:00
* @param float $timeout Pause duration, 0 = forever
2023-01-22 21:59:13 +01:00
*/
2023-01-23 00:24:18 +01:00
protected function reportPause(float $timeout): void
{
}
2023-01-22 21:59:13 +01:00
/**
* Start the loop.
*
* Returns false if the loop is already running.
*/
public function start(): bool
{
2023-01-24 18:58:51 +01:00
while ($this->shutdownDeferred !== null) {
2023-01-24 15:49:37 +01:00
$this->shutdownDeferred->getFuture()->await();
}
2023-01-24 16:17:38 +01:00
if ($this->running) {
return false;
}
2023-01-23 00:24:18 +01:00
$this->running = true;
2023-09-30 15:31:35 +02:00
/** @infection-ignore-all */
2023-01-24 12:31:57 +01:00
if (!$this->resume()) {
2023-01-24 12:33:59 +01:00
// @codeCoverageIgnoreStart
2023-01-24 12:31:57 +01:00
throw new AssertionError("Could not resume!");
2023-01-24 12:33:59 +01:00
// @codeCoverageIgnoreEnd
2023-01-24 12:31:57 +01:00
}
2023-01-23 00:24:18 +01:00
$this->startedLoop();
return true;
}
/**
* Stops loop.
*
* Returns false if the loop is not running.
*/
public function stop(): bool
{
if (!$this->running) {
return false;
}
2023-01-23 00:38:49 +01:00
$this->running = false;
2023-01-23 13:11:46 +01:00
if ($this->resumeTimer) {
$storedWatcherId = $this->resumeTimer;
EventLoop::cancel($storedWatcherId);
$this->resumeTimer = null;
}
if ($this->resumeImmediate) {
$storedWatcherId = $this->resumeImmediate;
EventLoop::cancel($storedWatcherId);
2023-01-23 16:05:36 +01:00
$this->resumeImmediate = null;
2023-01-23 13:11:46 +01:00
}
if ($this->paused) {
$this->exitedLoop();
2023-01-24 15:49:37 +01:00
} else {
2023-09-30 15:31:35 +02:00
/** @infection-ignore-all */
2023-01-24 15:49:37 +01:00
if ($this->shutdownDeferred !== null) {
// @codeCoverageIgnoreStart
throw new AssertionError("Shutdown deferred is not null!");
// @codeCoverageIgnoreEnd
}
$this->shutdownDeferred = new DeferredFuture;
2023-01-23 13:11:46 +01:00
}
2023-01-22 21:59:13 +01:00
return true;
}
2023-01-23 00:24:18 +01:00
abstract protected function loop(): ?float;
private bool $paused = true;
private function loopInternal(): void
{
2023-09-30 15:31:35 +02:00
/** @infection-ignore-all */
2023-01-24 12:31:57 +01:00
if (!$this->running) {
2023-01-24 12:33:59 +01:00
// @codeCoverageIgnoreStart
2023-01-24 12:31:57 +01:00
throw new AssertionError("Already running!");
2023-01-24 12:33:59 +01:00
// @codeCoverageIgnoreEnd
2023-01-24 12:31:57 +01:00
}
2023-09-30 15:31:35 +02:00
/** @infection-ignore-all */
2023-01-24 12:31:57 +01:00
if (!$this->paused) {
2023-01-24 12:33:59 +01:00
// @codeCoverageIgnoreStart
2023-01-24 12:31:57 +01:00
throw new AssertionError("Already paused!");
2023-01-24 12:33:59 +01:00
// @codeCoverageIgnoreEnd
2023-01-24 12:31:57 +01:00
}
2023-01-24 02:24:28 +01:00
$this->paused = false;
2023-01-23 00:24:18 +01:00
try {
$timeout = $this->loop();
} catch (\Throwable $e) {
$this->exitedLoopInternal();
throw $e;
}
2023-01-23 22:18:52 +01:00
/** @var bool $this->running */
2023-01-23 16:05:36 +01:00
if (!$this->running) {
$this->exitedLoopInternal();
return;
}
if ($timeout === self::STOP) {
2023-01-23 00:24:18 +01:00
$this->exitedLoopInternal();
return;
}
$this->paused = true;
if ($timeout === self::PAUSE) {
$this->reportPause(0.0);
} else {
2023-01-23 00:38:49 +01:00
if (!$this->resumeImmediate) {
2023-09-30 15:31:35 +02:00
/** @infection-ignore-all */
2023-01-24 12:31:57 +01:00
if ($this->resumeTimer !== null) {
2023-01-24 12:33:59 +01:00
// @codeCoverageIgnoreStart
2023-01-24 12:31:57 +01:00
throw new AssertionError("Already have a resume timer!");
2023-01-24 12:33:59 +01:00
// @codeCoverageIgnoreEnd
2023-01-24 12:31:57 +01:00
}
2023-01-23 00:24:18 +01:00
$this->resumeTimer = EventLoop::delay($timeout, function (): void {
$this->resumeTimer = null;
$this->loopInternal();
});
}
if ($timeout !== self::CONTINUE) {
$this->reportPause($timeout);
}
2023-01-23 00:24:18 +01:00
}
}
2023-01-22 21:22:34 +01:00
2023-01-23 00:24:18 +01:00
private function exitedLoopInternal(): void
{
$this->running = false;
2023-01-24 12:31:57 +01:00
$this->paused = true;
2023-09-30 15:31:35 +02:00
/** @infection-ignore-all */
2023-01-24 12:31:57 +01:00
if ($this->resumeTimer !== null) {
2023-01-24 12:33:59 +01:00
// @codeCoverageIgnoreStart
2023-01-24 12:31:57 +01:00
throw new AssertionError("Already have a resume timer!");
2023-01-24 12:33:59 +01:00
// @codeCoverageIgnoreEnd
2023-01-24 12:31:57 +01:00
}
2023-09-30 15:31:35 +02:00
/** @infection-ignore-all */
2023-01-24 12:34:29 +01:00
if ($this->resumeImmediate !== null) {
2023-01-24 12:33:59 +01:00
// @codeCoverageIgnoreStart
2023-01-24 12:31:57 +01:00
throw new AssertionError("Already have a resume immediate timer!");
2023-01-24 12:33:59 +01:00
// @codeCoverageIgnoreEnd
2023-01-24 12:31:57 +01:00
}
2023-01-23 00:24:18 +01:00
$this->exitedLoop();
2023-01-24 15:49:37 +01:00
if ($this->shutdownDeferred !== null) {
$d = $this->shutdownDeferred;
$this->shutdownDeferred = null;
EventLoop::queue($d->complete(...));
}
2023-01-23 00:24:18 +01:00
}
2023-01-22 21:59:13 +01:00
/**
2023-01-23 22:18:52 +01:00
* Signal that loop was started.
2023-01-22 21:59:13 +01:00
*/
protected function startedLoop(): void
{
}
/**
* Signal that loop has exited.
*/
protected function exitedLoop(): void
{
}
/**
* Check whether loop is running.
*/
public function isRunning(): bool
{
2023-01-23 00:24:18 +01:00
return $this->running;
}
2023-01-23 22:18:52 +01:00
/**
* Check whether loop is paused (different from isRunning, a loop may be running but paused).
*/
public function isPaused(): bool
{
return $this->paused;
}
2023-01-23 00:24:18 +01:00
/**
* Resume the loop.
2023-01-23 00:38:49 +01:00
*
2023-01-24 15:54:36 +01:00
* If resume is called multiple times, and the event loop hasn't resumed the loop yet,
* the loop will be resumed only once, not N times for every call.
*
2023-09-30 14:20:49 +02:00
* @param bool $postpone If true, multiple resumes will postpone the resuming to the end of the callback queue instead of leaving its position unchanged.
*
2023-01-23 00:38:49 +01:00
* @return bool Returns false if the loop is not paused.
2023-01-23 00:24:18 +01:00
*/
2023-09-30 14:20:49 +02:00
public function resume(bool $postpone = false): bool
2023-01-23 00:24:18 +01:00
{
2023-09-30 14:20:49 +02:00
if ($this->running && $this->paused) {
if ($this->resumeImmediate) {
if (!$postpone) {
return true;
}
$resumeImmediate = $this->resumeImmediate;
$this->resumeImmediate = null;
EventLoop::cancel($resumeImmediate);
}
2023-01-23 00:24:18 +01:00
if ($this->resumeTimer) {
$timer = $this->resumeTimer;
$this->resumeTimer = null;
EventLoop::cancel($timer);
}
2023-01-23 00:38:49 +01:00
$this->resumeImmediate = EventLoop::defer(function (): void {
$this->resumeImmediate = null;
2023-01-23 00:24:18 +01:00
$this->loopInternal();
});
2023-01-23 00:38:49 +01:00
return true;
2023-01-23 00:24:18 +01:00
}
2023-01-23 00:38:49 +01:00
return false;
2023-01-22 21:22:34 +01:00
}
2020-07-21 18:06:19 +02:00
}