Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/queue/src/Exception/RetryAfterException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Windwalker\Queue\Exception;

class RetryAfterException extends \RuntimeException
{
public function __construct(public int $delay)
{
parent::__construct();
}
}
9 changes: 6 additions & 3 deletions packages/queue/src/Job/JobController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Windwalker\Queue\Attributes\JobMiddlewaresProvider;
use Windwalker\Queue\Exception\AbandonedException;
use Windwalker\Queue\Exception\DeferredException;
use Windwalker\Queue\Exception\RetryAfterException;
use Windwalker\Queue\Middleware\QueueMiddlewareInterface;
use Windwalker\Queue\QueueMessage;
use Windwalker\Utilities\Attributes\AttributesAccessor;
Expand Down Expand Up @@ -121,11 +122,11 @@ public function defer(int $delay = 0, string $reason = ''): DeferredException
return $this->defer = new DeferredException($delay, $reason);
}

public function retryAfter(int $delay): static
public function retryAfter(int $delay): RetryAfterException
{
$this->retryAfter = max(0, $delay);
$this->retryAfter = $delay = max(0, $delay);

return $this;
return new RetryAfterException($delay);
}

public function resume(): static
Expand Down Expand Up @@ -370,6 +371,8 @@ protected function invokeMiddleware(mixed $middleware): mixed
}

return $result;
} catch (RetryAfterException $e) {
$this->retryAfter = $e->delay;
} catch (DeferredException $e) {
$this->defer = $e;

Expand Down
23 changes: 22 additions & 1 deletion packages/queue/test/Job/JobControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Windwalker\Queue\Attributes\JobMiddlewaresProvider;
use Windwalker\Queue\Exception\AbandonedException;
use Windwalker\Queue\Exception\DeferredException;
use Windwalker\Queue\Exception\RetryAfterException;
use Windwalker\Queue\Job\JobController;
use Windwalker\Queue\Middleware\QueueMiddlewareInterface;
use Windwalker\Queue\QueueMessage;
Expand Down Expand Up @@ -169,14 +170,34 @@ public function retryAfterStoresDelayAndResumeClearsIt(): void
$controller = new QueueMessage(new TestJob())->makeJobController();

self::assertNull($controller->retryAfter);
self::assertSame($controller, $controller->retryAfter(30));
self::assertInstanceOf(RetryAfterException::class, $controller->retryAfter(30));
self::assertSame(30, $controller->retryAfter);
self::assertFalse($controller->failed);

$controller->resume();
self::assertNull($controller->retryAfter);
}

#[Test]
public function retryAfterCanBeThrownToStopJobExecution(): void
{
$job = new class () {
public bool $continued = false;

public function __invoke(JobController $controller): void
{
throw $controller->retryAfter(30);

$this->continued = true;
}
};
$controller = new QueueMessage($job)->makeJobController()->run();

self::assertFalse($job->continued);
self::assertSame(30, $controller->retryAfter);
self::assertFalse($controller->failed);
}

#[Test]
public function logWritesEachMessageToLogger(): void
{
Expand Down