2020-07-31 15:00:59 +02:00
# Loop
2020-07-21 22:12:34 +02:00
2020-07-23 14:30:56 +02:00
![Build status ](https://github.com/danog/loop/workflows/build/badge.svg )
2020-07-29 23:33:31 +02:00
[![codecov ](https://codecov.io/gh/danog/loop/branch/master/graph/badge.svg )](https://codecov.io/gh/danog/loop)
2020-08-13 16:54:42 +02:00
[![Psalm coverage ](https://shepherd.dev/github/danog/loop/coverage.svg )](https://shepherd.dev/github/vimeo/shepherd)
2020-07-29 23:33:31 +02:00
![License ](https://img.shields.io/badge/license-MIT-blue.svg )
2020-07-21 22:12:34 +02:00
2020-07-24 19:29:14 +02:00
`danog/loop` provides a set of powerful async loop APIs for executing operations periodically or on demand, in background loops a-la threads.
2020-07-21 22:12:34 +02:00
## Installation
```bash
composer require danog/loop
```
2020-07-24 19:24:04 +02:00
## API
* Basic
2020-08-18 21:13:18 +02:00
* [GenericLoop ](#genericloop )
* [PeriodicLoop ](#periodicloop )
2020-07-24 19:24:04 +02:00
* Advanced
2020-08-18 21:13:18 +02:00
* [Loop ](#loop )
2020-07-21 22:12:34 +02:00
### Loop
2023-01-23 22:18:52 +01:00
[Class ](https://github.com/danog/loop/blob/master/lib/Loop.php ) - [Example ](https://github.com/danog/loop/blob/master/examples/Loop.php )
2020-07-24 19:24:04 +02:00
2023-01-23 22:18:52 +01:00
A loop capable of running in background (asynchronously) the code contained in the `loop` function.
Implements pause and resume functionality, and can be stopped from the outside or from the inside.
2020-07-21 22:12:34 +02:00
2020-07-24 19:24:04 +02:00
API:
2023-01-23 22:18:52 +01:00
2020-07-21 22:12:34 +02:00
```php
2020-07-24 19:24:04 +02:00
namespace danog\Loop;
2020-07-21 22:12:34 +02:00
2020-07-24 19:24:04 +02:00
abstract class Loop
2020-07-21 22:12:34 +02:00
{
2023-01-23 22:18:52 +01:00
/**
* Stop the loop.
*/
public const STOP;
/**
* Pause the loop.
*/
public const PAUSE;
/**
* Rerun the loop.
*/
public const CONTINUE;
/**
* Loop body.
*
* The return value can be:
* A number - the loop will be paused for the specified number of seconds
* Loop::STOP - The loop will stop
* Loop::PAUSE - The loop will pause forever (or until loop is `resume()` 'd
* from outside the loop)
* Loop::CONTINUE - Return this if you want to rerun the loop immediately
*
* The loop can be stopped from the outside by using stop().
* @return float|Loop::STOP|Loop::PAUSE|Loop::CONTINUE
*/
abstract protected function loop(): ?float;
/**
* Loop name, useful for logging.
*/
2020-07-24 19:24:04 +02:00
abstract public function __toString(): string;
2023-01-23 22:18:52 +01:00
/**
* Start the loop.
*
* Returns false if the loop is already running.
*/
2020-07-21 22:12:34 +02:00
public function start(): bool;
2023-01-23 22:18:52 +01:00
/**
* Resume the loop.
*
* @return bool Returns false if the loop is not paused.
*/
public function resume(): bool;
/**
* Stops loop.
*
* Returns false if the loop is not running.
*/
public function stop(): bool;
/**
* Check whether loop is running.
*/
2020-07-21 22:12:34 +02:00
public function isRunning(): bool;
2023-01-23 22:18:52 +01:00
/**
* Check whether loop is paused.
*/
public function isPaused(): bool;
2020-07-24 19:24:04 +02:00
2023-01-23 22:18:52 +01:00
/**
* Report pause, can be overriden for logging.
*
* @param float $timeout Pause duration, 0 = forever
*/
protected function reportPause(float $timeout): void;
/**
* Signal that loop was started.
*/
2020-07-24 19:24:04 +02:00
protected function startedLoop(): void;
2023-01-23 22:18:52 +01:00
/**
* Signal that loop has exited.
*/
2020-07-24 19:24:04 +02:00
protected function exitedLoop(): void;
2020-07-21 22:12:34 +02:00
}
```
2023-01-23 22:18:52 +01:00
### GenericLoop
2020-07-21 22:12:34 +02:00
2023-01-23 22:18:52 +01:00
[Class ](https://github.com/danog/loop/blob/master/lib/GenericLoop.php ) - [Example ](https://github.com/danog/loop/blob/master/examples/GenericLoop.php )
2020-07-24 19:24:04 +02:00
2023-01-23 22:18:52 +01:00
If you want a simpler way to use the `Loop` , you can use the GenericLoop.
2020-07-21 22:12:34 +02:00
```php
namespace danog\Loop;
2023-01-23 22:18:52 +01:00
class GenericLoop extends Loop
2020-07-24 19:24:04 +02:00
{
/**
* Constructor.
*
2023-01-23 22:18:52 +01:00
* The return value of the callable can be:
* * A number - the loop will be paused for the specified number of seconds
* * GenericLoop::STOP - The loop will stop
* * GenericLoop::PAUSE - The loop will pause forever (or until loop is `resume()` 'd
* from outside the loop)
* * GenericLoop::CONTINUE - Return this if you want to rerun the loop immediately
*
* If the callable does not return anything,
* the loop will behave is if GenericLoop::PAUSE was returned.
2020-07-21 22:12:34 +02:00
*
2023-01-23 22:18:52 +01:00
* The loop can be stopped from the outside by using stop().
2020-07-24 19:24:04 +02:00
*
2023-01-23 22:18:52 +01:00
* @param callable(static):?float $callable Callable to run
* @param string $name Loop name
2020-07-21 22:12:34 +02:00
*/
2023-01-23 22:18:52 +01:00
public function __construct(callable $callable, private string $name);
2020-07-24 19:24:04 +02:00
/**
* Get loop name, provided to constructor.
*/
public function __toString(): string;
}
2020-07-21 22:12:34 +02:00
```
### PeriodicLoop
2023-01-23 22:18:52 +01:00
[Class ](https://github.com/danog/loop/blob/master/lib/PeriodicLoop.php ) - [Example ](https://github.com/danog/loop/blob/master/examples/PeriodicLoop.php )
2020-07-24 19:24:04 +02:00
2020-07-21 22:12:34 +02:00
If you simply want to execute an action every N seconds, [PeriodicLoop ](https://github.com/danog/MadelineProto/blob/master/src/danog/MadelineProto/Loop/Generic/PeriodicLoop.php ) is the way to go.
2023-01-23 22:18:52 +01:00
2020-07-21 22:12:34 +02:00
```php
2023-01-23 22:18:52 +01:00
namespace danog\Loop;
2020-07-24 19:24:04 +02:00
2023-01-23 22:18:52 +01:00
class PeriodicLoop extends GenericLoop
2020-07-24 19:24:04 +02:00
{
2020-07-21 22:12:34 +02:00
/**
* Constructor.
*
2023-01-23 22:18:52 +01:00
* Runs a callback at a periodic interval.
*
* The loop can be stopped from the outside by calling stop()
* and from the inside by returning `true` .
*
* @param callable(static):bool $callback Callable to run
2020-07-24 19:24:04 +02:00
* @param string $name Loop name
2023-01-23 22:18:52 +01:00
* @param ?float $interval Loop interval; if null, pauses indefinitely or until `resume()` is called.
2020-07-21 22:12:34 +02:00
*/
2023-01-23 22:18:52 +01:00
public function __construct(callable $callback, string $name, ?float $interval)
2020-07-24 19:24:04 +02:00
/**
* Get name of the loop, passed to the constructor.
*
* @return string
*/
2020-07-24 19:28:17 +02:00
public function __toString(): string;
2020-07-24 19:24:04 +02:00
}
2020-07-21 22:12:34 +02:00
```