diff --git a/src/Services/AbstractQueuedJob.php b/src/Services/AbstractQueuedJob.php index f47604bb..b1814c10 100644 --- a/src/Services/AbstractQueuedJob.php +++ b/src/Services/AbstractQueuedJob.php @@ -271,4 +271,31 @@ public function __get($name) { return isset($this->jobData->$name) ? $this->jobData->$name : null; } + + /** + * Resolves a queue name to one of the queue constants. + * If $queue is already the value of one of the constants, it will be returned. + * If the queue is unknown, `null` will be returned. + */ + public static function getQueue(string|int $queue): ?string + { + switch (strtolower($queue)) { + case 'immediate': + $queue = QueuedJob::IMMEDIATE; + break; + case 'queued': + $queue = QueuedJob::QUEUED; + break; + case 'large': + $queue = QueuedJob::LARGE; + break; + default: + $queue = (string) $queue; + $queues = [QueuedJob::IMMEDIATE, QueuedJob::QUEUED, QueuedJob::LARGE]; + if (!ctype_digit($queue) || !in_array($queue, $queues)) { + return null; + } + } + return $queue; + } } diff --git a/src/Tasks/ProcessJobQueueChildTask.php b/src/Tasks/ProcessJobQueueChildTask.php index e6c5e51f..cd28d2ac 100644 --- a/src/Tasks/ProcessJobQueueChildTask.php +++ b/src/Tasks/ProcessJobQueueChildTask.php @@ -4,8 +4,12 @@ use SilverStripe\Control\HTTPRequest; use SilverStripe\Dev\BuildTask; +use SilverStripe\Dev\Deprecation; use Symbiote\QueuedJobs\Services\QueuedJobService; +/** + * @deprecated 5.3.0 Will be replaced with Symbiote\QueuedJobs\Cli\ProcessJobQueueChildCommand + */ class ProcessJobQueueChildTask extends BuildTask { /** @@ -14,6 +18,18 @@ class ProcessJobQueueChildTask extends BuildTask */ private static $segment = 'ProcessJobQueueChildTask'; + public function __construct() + { + parent::__construct(); + Deprecation::withNoReplacement(function () { + Deprecation::notice( + '5.3.0', + 'Will be replaced with Symbiote\QueuedJobs\Cli\ProcessJobQueueChildCommand', + Deprecation::SCOPE_CLASS + ); + }); + } + /** * @param HTTPRequest $request */ diff --git a/src/Tasks/ProcessJobQueueTask.php b/src/Tasks/ProcessJobQueueTask.php index 84df6ac8..b50cc04c 100644 --- a/src/Tasks/ProcessJobQueueTask.php +++ b/src/Tasks/ProcessJobQueueTask.php @@ -8,6 +8,8 @@ use SilverStripe\Control\HTTPRequest; use SilverStripe\Core\Environment; use SilverStripe\Dev\BuildTask; +use SilverStripe\Dev\Deprecation; +use Symbiote\QueuedJobs\Services\AbstractQueuedJob; use Symbiote\QueuedJobs\Services\QueuedJob; use Symbiote\QueuedJobs\Services\QueuedJobService; @@ -83,7 +85,7 @@ public function run($request) } // Run the queue - $queue = $this->getQueue($request); + $queue = AbstractQueuedJob::getQueue($request->getVar('queue') ?? 'Queued'); $service->runQueue($queue); } @@ -94,9 +96,12 @@ public function run($request) * * @param HTTPRequest $request * @return string + * @deprecated 5.3.0 Use Symbiote\QueuedJobs\Services\AbstractQueuedJob::getQueue() instead */ protected function getQueue($request) { + Deprecation::notice('5.3.0', 'Use ' . AbstractQueuedJob::class . '::getQueue() instead'); + $queue = $request->getVar('queue'); if (!$queue) { diff --git a/tests/QueuedJobsTest.php b/tests/QueuedJobsTest.php index 492ce41a..0ff5a251 100644 --- a/tests/QueuedJobsTest.php +++ b/tests/QueuedJobsTest.php @@ -13,6 +13,7 @@ use SilverStripe\ORM\ValidationException; use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor; use Symbiote\QueuedJobs\Jobs\RunBuildTaskJob; +use Symbiote\QueuedJobs\Services\AbstractQueuedJob; use Symbiote\QueuedJobs\Services\QueuedJob; use Symbiote\QueuedJobs\Services\QueuedJobService; use Symbiote\QueuedJobs\Tests\QueuedJobsTest\TestExceptingJob; @@ -830,4 +831,58 @@ public function healthCheckProvider(): array [RunBuildTaskJob::class, 0], ]; } + + public function provideGetQueue(): array + { + return [ + 'immediate const' => [ + 'queue' => QueuedJob::IMMEDIATE, + 'expected' => QueuedJob::IMMEDIATE, + ], + 'queued const' => [ + 'queue' => QueuedJob::QUEUED, + 'expected' => QueuedJob::QUEUED, + ], + 'large const' => [ + 'queue' => QueuedJob::LARGE, + 'expected' => QueuedJob::LARGE, + ], + 'immediate string' => [ + 'queue' => 'iMmEdiAte', + 'expected' => QueuedJob::IMMEDIATE, + ], + 'immediate as int' => [ + 'queue' => 1, + 'expected' => QueuedJob::IMMEDIATE, + ], + 'queued string' => [ + 'queue' => 'queued', + 'expected' => QueuedJob::QUEUED, + ], + 'large string' => [ + 'queue' => 'large', + 'expected' => QueuedJob::LARGE, + ], + 'random string' => [ + 'queue' => 'bongos', + 'expected' => null, + ], + 'negative int' => [ + 'queue' => -1, + 'expected' => null, + ], + 'random int' => [ + 'queue' => 5, + 'expected' => null, + ], + ]; + } + + /** + * @dataProvider provideGetQueue + */ + public function testGetQueue(int|string $queue, ?string $expected): void + { + $this->assertSame($expected, AbstractQueuedJob::getQueue($queue)); + } }