Skip to content

Commit

Permalink
API Deprecate API that will be removed (#447)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored Sep 13, 2024
1 parent 55c66e1 commit b6c1c4f
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/Services/AbstractQueuedJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
16 changes: 16 additions & 0 deletions src/Tasks/ProcessJobQueueChildTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand All @@ -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
*/
Expand Down
7 changes: 6 additions & 1 deletion src/Tasks/ProcessJobQueueTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

Expand All @@ -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) {
Expand Down
55 changes: 55 additions & 0 deletions tests/QueuedJobsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}

0 comments on commit b6c1c4f

Please sign in to comment.