Runner
in package
A task runner with exponential backoff support.
Tags
Table of Contents
Constants
- TASK_RETRY_ALWAYS = -1
- TASK_RETRY_NEVER = 0
- TASK_RETRY_ONCE = 1
Properties
- $retryMap : array<string|int, mixed>
- $action : callable
- $arguments : array<string|int, mixed>
- $attempts : int
- $delay : int
- $factor : int
- $jitter : float
- $maxAttempts : int
- $maxDelay : int
Methods
- __construct() : mixed
- Creates a new task runner with exponential backoff support.
- allowedRetries() : int
- Gets the number of times the associated task can be retried.
- attempt() : bool
- Runs a task once, if possible. This is useful for bypassing the `run()` loop.
- canAttempt() : bool
- Checks if a retry can be attempted.
- run() : mixed
- Runs the task and (if applicable) automatically retries when errors occur.
- setRetryMap() : mixed
- backOff() : mixed
- Sleeps in accordance to the backoff configurations.
- getDelay() : float
- Gets the delay (in seconds) for the current backoff period.
- getJitter() : float
- Gets the current jitter (random number between -$this->jitter and $this->jitter).
Constants
TASK_RETRY_ALWAYS
public
mixed
TASK_RETRY_ALWAYS
= -1
TASK_RETRY_NEVER
public
mixed
TASK_RETRY_NEVER
= 0
TASK_RETRY_ONCE
public
mixed
TASK_RETRY_ONCE
= 1
Properties
$retryMap
protected
array<string|int, mixed>
$retryMap
= [
'500' => self::TASK_RETRY_ALWAYS,
'503' => self::TASK_RETRY_ALWAYS,
'rateLimitExceeded' => self::TASK_RETRY_ALWAYS,
'userRateLimitExceeded' => self::TASK_RETRY_ALWAYS,
6 => self::TASK_RETRY_ALWAYS,
// CURLE_COULDNT_RESOLVE_HOST
7 => self::TASK_RETRY_ALWAYS,
// CURLE_COULDNT_CONNECT
28 => self::TASK_RETRY_ALWAYS,
// CURLE_OPERATION_TIMEOUTED
35 => self::TASK_RETRY_ALWAYS,
// CURLE_SSL_CONNECT_ERROR
52 => self::TASK_RETRY_ALWAYS,
// CURLE_GOT_NOTHING
'lighthouseError' => self::TASK_RETRY_NEVER,
]
Map of errors with retry counts.
$action
private
callable
$action
The task to run and possibly retry.
$arguments
private
array<string|int, mixed>
$arguments
The task arguments.
$attempts
private
int
$attempts
= 0
The number of attempts that have been tried so far.
$delay
private
int
$delay
= 1
The previous delay from which the next is calculated.
$factor
private
int
$factor
= 2
The base number for the exponential back off.
$jitter
private
float
$jitter
= 0.5
A random number between -$jitter and $jitter will be added to $factor on each iteration to allow for a better distribution of retries.
$maxAttempts
private
int
$maxAttempts
= 1
The max number of attempts allowed.
$maxDelay
private
int
$maxDelay
= 60
The max time (in seconds) to wait before a retry.
Methods
__construct()
Creates a new task runner with exponential backoff support.
public
__construct(array<string|int, mixed> $config, string $name, callable $action[, array<string|int, mixed> $arguments = array() ]) : mixed
Parameters
- $config : array<string|int, mixed>
-
The task runner config
- $name : string
-
The name of the current task (used for logging)
- $action : callable
-
The task to run and possibly retry
- $arguments : array<string|int, mixed> = array()
-
The task arguments
Tags
allowedRetries()
Gets the number of times the associated task can be retried.
public
allowedRetries(mixed $code[, mixed $errors = array() ]) : int
NOTE: -1 is returned if the task can be retried indefinitely
Parameters
- $code : mixed
- $errors : mixed = array()
Return values
intattempt()
Runs a task once, if possible. This is useful for bypassing the `run()` loop.
public
attempt() : bool
NOTE: If this is not the first attempt, this function will sleep in accordance to the backoff configurations before running the task.
Return values
boolcanAttempt()
Checks if a retry can be attempted.
public
canAttempt() : bool
Return values
boolrun()
Runs the task and (if applicable) automatically retries when errors occur.
public
run() : mixed
Tags
setRetryMap()
public
setRetryMap(mixed $retryMap) : mixed
Parameters
- $retryMap : mixed
backOff()
Sleeps in accordance to the backoff configurations.
private
backOff() : mixed
getDelay()
Gets the delay (in seconds) for the current backoff period.
private
getDelay() : float
Return values
floatgetJitter()
Gets the current jitter (random number between -$this->jitter and $this->jitter).
private
getJitter() : float