Final Class Yiisoft\Queue\Middleware\FailureHandling\Implementation\ExponentialDelayMiddleware
| Inheritance | Yiisoft\Queue\Middleware\FailureHandling\Implementation\ExponentialDelayMiddleware |
|---|---|
| Implements | Yiisoft\Queue\Middleware\FailureHandling\MiddlewareFailureInterface |
Failure strategy which resends the given message to a queue with an exponentially increasing delay.
The delay mechanism must be implemented by the used {@see \Yiisoft\Queue\Middleware\FailureHandling\Implementation\AdapterInterface} implementation.
Public Methods
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| META_KEY_ATTEMPTS | 'failure-strategy-exponential-delay-attempts' | Yiisoft\Queue\Middleware\FailureHandling\Implementation\ExponentialDelayMiddleware | |
| META_KEY_DELAY | 'failure-strategy-exponential-delay-delay' | Yiisoft\Queue\Middleware\FailureHandling\Implementation\ExponentialDelayMiddleware |
Method Details
| public mixed __construct ( string $id, integer $maxAttempts, float $delayInitial, float $delayMaximum, float $exponent, Yiisoft\Queue\Middleware\Push\Implementation\DelayMiddlewareInterface $delayMiddleware, Yiisoft\Queue\QueueInterface|null $queue = null ) | ||
| $id | string |
A unique id to differentiate two and more instances of this class |
| $maxAttempts | integer |
Maximum attempts count for this strategy with the given $id before it will give up |
| $delayInitial | float |
The first delay period |
| $delayMaximum | float |
The maximum delay period |
| $exponent | float |
Message handling delay will be increased by this multiplication each time it fails |
| $delayMiddleware | Yiisoft\Queue\Middleware\Push\Implementation\DelayMiddlewareInterface |
A middleware for message delaying. |
| $queue | Yiisoft\Queue\QueueInterface|null | |
public function __construct(
private readonly string $id,
private readonly int $maxAttempts,
private readonly float $delayInitial,
private readonly float $delayMaximum,
private readonly float $exponent,
private readonly DelayMiddlewareInterface $delayMiddleware,
private readonly ?QueueInterface $queue = null,
) {
if ($maxAttempts <= 0) {
throw new InvalidArgumentException("maxAttempts parameter must be a positive integer, $this->maxAttempts given.");
}
if ($delayInitial <= 0) {
throw new InvalidArgumentException("delayInitial parameter must be a positive float, $this->delayInitial given.");
}
if ($delayMaximum < $delayInitial) {
throw new InvalidArgumentException("delayMaximum parameter must not be less then delayInitial, , $this->delayMaximum given.");
}
if ($exponent <= 0) {
throw new InvalidArgumentException("exponent parameter must not be zero or less, $this->exponent given.");
}
}
public function processFailure(
FailureHandlingRequest $request,
MessageFailureHandlerInterface $handler
): FailureHandlingRequest {
$message = $request->getMessage();
if ($this->suites($message)) {
$envelope = new FailureEnvelope($message, $this->createNewMeta($message));
$queue = $this->queue ?? $request->getQueue();
$middlewareDefinitions = $this->delayMiddleware->withDelay($this->getDelay($envelope));
$messageNew = $queue->push(
$envelope,
$middlewareDefinitions
);
return $request->withMessage($messageNew);
}
return $handler->handleFailure($request);
}
Signup or Login in order to comment.