Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[spiral/queue] Adding RetryPolicyInterceptor #980

Merged
merged 5 commits into from
Sep 9, 2023

Conversation

msmakouz
Copy link
Member

@msmakouz msmakouz commented Sep 4, 2023

Q A
Bugfix?
Breaks BC?
New feature? ✔️
Issues #911

What was changed

Added Spiral\Queue\Interceptor\Consume\RetryPolicyInterceptor to enable automatic job retries with a configurable retry policy. To use it, need to add the Spiral\Queue\Attribute\RetryPolicy attribute to the job class:

use Spiral\Queue\Attribute\RetryPolicy;
use Spiral\Queue\JobHandler;

#[RetryPolicy(maxAttempts: 3, delay: 5, multiplier: 2)]
final class Ping extends JobHandler
{
    public function invoke(array $payload): void
    {
        // ...
    }
}

Create an exception that implements interface Spiral\Queue\Exception\RetryableExceptionInterface:

use Spiral\Queue\Exception\RetryableExceptionInterface;
use Spiral\Queue\RetryPolicyInterface;

class RetryException extends \DomainException implements RetryableExceptionInterface
{
    public function isRetryable(): bool
    {
        return true;
    }

    public function getRetryPolicy(): ?RetryPolicyInterface
    {
        return null;
    }
}

The exception must implement the two methods isRetryable and getRetryPolicy. These methods can override the retry behavior and cancel the re-queue- or change the retry policy.

If a RetryException is thrown while a job runs, the job will be re-queued according to the retry policy.

@msmakouz msmakouz added this to the 3.9 milestone Sep 4, 2023
@msmakouz msmakouz self-assigned this Sep 4, 2023
@codecov
Copy link

codecov bot commented Sep 4, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.06% 🎉

Comparison is base (0c4ba5e) 83.84% compared to head (4e4803a) 83.91%.

❗ Current head 4e4803a differs from pull request most recent head fc3c61c. Consider uploading reports for the commit fc3c61c to get more accurate results

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #980      +/-   ##
============================================
+ Coverage     83.84%   83.91%   +0.06%     
- Complexity     6048     6068      +20     
============================================
  Files           793      796       +3     
  Lines         17046    17099      +53     
============================================
+ Hits          14292    14348      +56     
+ Misses         2754     2751       -3     
Files Changed Coverage Δ
src/Queue/src/Attribute/RetryPolicy.php 100.00% <100.00%> (ø)
src/Queue/src/Bootloader/QueueBootloader.php 94.20% <100.00%> (+0.08%) ⬆️
...src/Interceptor/Consume/RetryPolicyInterceptor.php 100.00% <100.00%> (ø)
src/Queue/src/RetryPolicy.php 100.00% <100.00%> (ø)

... and 3 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@butschster butschster merged commit 7386638 into spiral:master Sep 9, 2023
7 checks passed
@msmakouz msmakouz deleted the feature/retry branch September 11, 2023 11:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

[spiral/queue] Add Retry Strategy Management to the Spiral Queue Component
2 participants