Skip to content

Commit 69ef138

Browse files
committed
introduce Priority enum
1 parent 1bfd32c commit 69ef138

File tree

9 files changed

+51
-16
lines changed

9 files changed

+51
-16
lines changed

src/Command/JobCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ protected function view(ConsoleIo $io, QueuedJob $queuedJob): int {
249249
$io->out('Task: ' . $queuedJob->job_task);
250250
$io->out('Reference: ' . ($queuedJob->reference ?: '-'));
251251
$io->out('Group: ' . ($queuedJob->job_group ?: '-'));
252-
$io->out('Priority: ' . $queuedJob->priority);
252+
$io->out('Priority: ' . $queuedJob->priority_int);
253253
$io->out('Not before: ' . ($queuedJob->notbefore ?: '-'));
254254

255255
$io->out('Completed: ' . ($queuedJob->completed ?: '-'));

src/Config/JobConfig.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Queue\Config;
44

55
use InvalidArgumentException;
6+
use Queue\Model\Enum\Priority;
67
use RuntimeException;
78

89
/**
@@ -67,7 +68,7 @@ class JobConfig {
6768
public const TYPE_DASHED = 'dashed';
6869

6970
/**
70-
* @var int|null
71+
* @var \Queue\Model\Enum\Priority|int|null
7172
*/
7273
protected $priority;
7374

@@ -219,40 +220,40 @@ public function field(string $name, string $type): string {
219220
}
220221

221222
/**
222-
* @param int|null $priority
223+
* @param \Queue\Model\Enum\Priority|int|null $priority
223224
*
224225
* @return $this
225226
*/
226-
public function setPriority(?int $priority) {
227+
public function setPriority(Priority|int|null $priority) {
227228
$this->priority = $priority;
228229

229230
return $this;
230231
}
231232

232233
/**
233-
* @param int $priority
234+
* @param \Queue\Model\Enum\Priority|int $priority
234235
*
235236
* @return $this
236237
*/
237-
public function setPriorityOrFail(int $priority) {
238+
public function setPriorityOrFail(Priority|int $priority) {
238239
$this->priority = $priority;
239240

240241
return $this;
241242
}
242243

243244
/**
244-
* @return int|null
245+
* @return \Queue\Model\Enum\Priority|int|null
245246
*/
246-
public function getPriority(): ?int {
247+
public function getPriority(): Priority|int|null {
247248
return $this->priority;
248249
}
249250

250251
/**
251252
* @throws \RuntimeException If value is not set.
252253
*
253-
* @return int
254+
* @return \Queue\Model\Enum\Priority|int
254255
*/
255-
public function getPriorityOrFail(): int {
256+
public function getPriorityOrFail(): Priority|int {
256257
if ($this->priority === null) {
257258
throw new RuntimeException('Value not set for field `priority` (expected to be not null)');
258259
}

src/Model/Entity/QueuedJob.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Queue\Model\Entity;
55

66
use Cake\ORM\Entity;
7+
use Queue\Model\Enum\Priority;
78

89
/**
910
* @property int $id
@@ -20,11 +21,12 @@
2021
* @property string|null $failure_message
2122
* @property string|null $workerkey
2223
* @property string|null $status
23-
* @property int $priority
24+
* @property \Queue\Model\Enum\Priority|int $priority
2425
* @property \Queue\Model\Entity\QueueProcess $worker_process
2526
* @property int|null $memory
2627
* @property string|null $headers !
2728
* @property string|null $message !
29+
* @property-read int $priority_int
2830
*/
2931
class QueuedJob extends Entity {
3032

@@ -47,4 +49,15 @@ public static function statusesForSearch(): array {
4749
];
4850
}
4951

52+
/**
53+
* @return int
54+
*/
55+
protected function _getPriorityInt(): int {
56+
if ($this->priority instanceof Priority) {
57+
return $this->priority->value;
58+
}
59+
60+
return $this->priority;
61+
}
62+
5063
}

src/Model/Enum/Priority.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Queue\Model\Enum;
5+
6+
enum Priority: int {
7+
case Critical = 1;
8+
case Urgent = 2;
9+
case High = 3;
10+
case MediumHigh = 4;
11+
case Medium = 5;
12+
case MediumLow = 6;
13+
case Low = 7;
14+
case VeryLow = 8;
15+
case Deferred = 9;
16+
case Idle = 10;
17+
}

src/Model/Table/QueuedJobsTable.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use ArrayObject;
77
use Cake\Core\Configure;
88
use Cake\Core\Plugin;
9+
use Cake\Database\Type\EnumType;
910
use Cake\Event\EventInterface;
1011
use Cake\I18n\DateTime;
1112
use Cake\ORM\Query\SelectQuery;
@@ -15,6 +16,7 @@
1516
use InvalidArgumentException;
1617
use Queue\Config\JobConfig;
1718
use Queue\Model\Entity\QueuedJob;
19+
use Queue\Model\Enum\Priority;
1820
use Queue\Model\Filter\QueuedJobsCollection;
1921
use Queue\Queue\Config;
2022
use Queue\Queue\TaskFinder;
@@ -129,6 +131,7 @@ public function initialize(array $config): void {
129131
]);
130132

131133
$this->getSchema()->setColumnType('data', 'json');
134+
$this->getSchema()->setColumnType('priority', EnumType::from(Priority::class));
132135
}
133136

134137
/**

templates/Admin/Queue/index.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
<ol>
6161
<?php
6262
foreach ($pendingDetails as $pendingJob) {
63-
echo '<li>' . $this->Html->link($pendingJob->job_task, ['controller' => 'QueuedJobs', 'action' => 'view', $pendingJob->id]) . ' (ref <code>' . h($pendingJob->reference ?: '-') . '</code>, prio ' . $pendingJob->priority . '):';
63+
echo '<li>' . $this->Html->link($pendingJob->job_task, ['controller' => 'QueuedJobs', 'action' => 'view', $pendingJob->id]) . ' (ref <code>' . h($pendingJob->reference ?: '-') . '</code>, prio ' . $pendingJob->priority_int . '):';
6464
echo '<ul>';
6565

6666
$reset = '';
@@ -115,7 +115,7 @@
115115
<ol>
116116
<?php
117117
foreach ($scheduledDetails as $pendingJob) {
118-
echo '<li>' . $this->Html->link($pendingJob->job_task, ['controller' => 'QueuedJobs', 'action' => 'view', $pendingJob->id]) . ' (ref <code>' . h($pendingJob->reference ?: '-') . '</code>, prio ' . $pendingJob->priority . '):';
118+
echo '<li>' . $this->Html->link($pendingJob->job_task, ['controller' => 'QueuedJobs', 'action' => 'view', $pendingJob->id]) . ' (ref <code>' . h($pendingJob->reference ?: '-') . '</code>, prio ' . $pendingJob->priority_int . '):';
119119
echo '<ul>';
120120

121121
$reset = '';

templates/Admin/QueuedJobs/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
<div><small><?php echo $this->Number->format($queuedJob->memory); ?> MB</small></div>
131131
<?php } ?>
132132
</td>
133-
<td><?= $this->Number->format($queuedJob->priority) ?></td>
133+
<td><?= $this->Number->format($queuedJob->priority_int) ?></td>
134134
<td class="actions">
135135
<?= $this->Html->link($this->Icon->render('view'), ['action' => 'view', $queuedJob->id], ['escapeTitle' => false]); ?>
136136

templates/Admin/QueuedJobs/view.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
</tr>
130130
<tr>
131131
<th><?= __d('queue', 'Priority') ?></th>
132-
<td><?= $this->Number->format($queuedJob->priority) ?></td>
132+
<td><?= $this->Number->format($queuedJob->priority_int) ?></td>
133133
</tr>
134134
</table>
135135
<div class="row">

tests/TestCase/Controller/Admin/QueuedJobsControllerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Cake\I18n\DateTime;
99
use Cake\TestSuite\IntegrationTestTrait;
1010
use Laminas\Diactoros\UploadedFile;
11+
use Queue\Model\Enum\Priority;
1112
use Shim\TestSuite\TestCase;
1213

1314
/**
@@ -91,7 +92,7 @@ public function testEditPost() {
9192
$queuedJobs = $this->fetchTable('Queue.QueuedJobs');
9293
/** @var \Queue\Model\Entity\QueuedJob $modifiedJob */
9394
$modifiedJob = $queuedJobs->get($job->id);
94-
$this->assertSame(8, $modifiedJob->priority);
95+
$this->assertSame(Priority::VeryLow, $modifiedJob->priority);
9596
}
9697

9798
/**

0 commit comments

Comments
 (0)