Skip to content

Commit 52e6674

Browse files
committed
Separate queued from scheduled.
1 parent 0ddf40e commit 52e6674

File tree

6 files changed

+142
-15
lines changed

6 files changed

+142
-15
lines changed

src/Controller/Admin/QueueController.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,16 @@ public function index() {
5252
$new++;
5353
}
5454

55+
$scheduledDetails = $this->QueuedJobs->getScheduledStats()->toArray();
56+
5557
$data = $this->QueuedJobs->getStats();
5658

5759
$taskFinder = new TaskFinder();
5860
$tasks = $taskFinder->all();
5961
$addableTasks = $taskFinder->allAddable(AddFromBackendInterface::class);
6062

61-
$servers = $QueueProcesses->find()
62-
->distinct(['server'])
63-
->where(['server IS NOT' => null])
64-
->find(
65-
'list',
66-
keyField: 'server',
67-
valueField: 'server',
68-
)->toArray();
69-
$this->set(compact('new', 'current', 'data', 'pendingDetails', 'status', 'tasks', 'addableTasks', 'servers'));
63+
$servers = $QueueProcesses->serverList();
64+
$this->set(compact('new', 'current', 'data', 'pendingDetails', 'scheduledDetails', 'status', 'tasks', 'addableTasks', 'servers'));
7065
}
7166

7267
/**

src/Model/Entity/QueuedJob.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,15 @@ class QueuedJob extends Entity {
3333
'id' => false,
3434
];
3535

36+
/**
37+
* @return string[]
38+
*/
39+
public static function statusesForSearch(): array {
40+
return [
41+
'completed' => 'Completed',
42+
'in_progress' => 'In Progress',
43+
'scheduled' => 'Scheduled',
44+
];
45+
}
46+
3647
}

src/Model/Table/QueueProcessesTable.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,4 +348,18 @@ public function buildServerString(): ?string {
348348
return $serverName ?: null;
349349
}
350350

351+
/**
352+
* @return array<string, string>
353+
*/
354+
public function serverList(): array {
355+
return $this->find()
356+
->distinct(['server'])
357+
->where(['server IS NOT' => null])
358+
->find(
359+
'list',
360+
keyField: 'server',
361+
valueField: 'server',
362+
)->toArray();
363+
}
364+
351365
}

src/Model/Table/QueuedJobsTable.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,17 @@ public function searchManager(): Manager {
167167
return true;
168168
}
169169
if ($status === 'in_progress') {
170-
$query->where(['completed IS' => null]);
170+
$query->where([
171+
'completed IS' => null,
172+
'OR' => [
173+
'notbefore <=' => new DateTime(),
174+
'notbefore IS' => null,
175+
]]);
176+
177+
return true;
178+
}
179+
if ($status === 'scheduled') {
180+
$query->where(['completed IS' => null, 'notbefore >' => new DateTime()]);
171181

172182
return true;
173183
}
@@ -304,6 +314,10 @@ public function getLength(?string $type = null): int {
304314
$findConf = [
305315
'conditions' => [
306316
'completed IS' => null,
317+
'OR' => [
318+
'notbefore <=' => new DateTime(),
319+
'notbefore IS' => null,
320+
],
307321
],
308322
];
309323
if ($type !== null) {
@@ -794,6 +808,10 @@ public function reset(?int $id = null, bool $full = false): int {
794808
];
795809
$conditions = [
796810
'completed IS' => null,
811+
'OR' => [
812+
'notbefore <=' => new DateTime(),
813+
'notbefore IS' => null,
814+
],
797815
];
798816
if ($id) {
799817
$conditions['id'] = $id;
@@ -875,6 +893,37 @@ public function getPendingStats(): SelectQuery {
875893
],
876894
'conditions' => [
877895
'completed IS' => null,
896+
'OR' => [
897+
'notbefore <=' => new DateTime(),
898+
'notbefore IS' => null,
899+
],
900+
],
901+
];
902+
903+
return $this->find('all', ...$findCond);
904+
}
905+
906+
/**
907+
* @return \Cake\ORM\Query\SelectQuery
908+
*/
909+
public function getScheduledStats(): SelectQuery {
910+
$findCond = [
911+
'fields' => [
912+
'id',
913+
'job_task',
914+
'created',
915+
'status',
916+
'priority',
917+
'fetched',
918+
'progress',
919+
'reference',
920+
'notbefore',
921+
'attempts',
922+
'failure_message',
923+
],
924+
'conditions' => [
925+
'completed IS' => null,
926+
'notbefore >' => new DateTime(),
878927
],
879928
];
880929

templates/Admin/Queue/index.php

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
2+
23
/**
34
* @var \App\View\AppView $this
45
* @var \Queue\Model\Entity\QueuedJob[] $pendingDetails
6+
* @var \Queue\Model\Entity\QueuedJob[] $scheduledDetails
57
* @var string[] $tasks
68
* @var string[] $addableTasks
79
* @var string[] $servers
@@ -11,6 +13,7 @@
1113
* @var array $data
1214
*/
1315
use Cake\Core\Configure;
16+
1417
?>
1518

1619
<nav class="col-md-3 col-12 large-3 medium-4 columns" id="actions-sidebar">
@@ -43,7 +46,7 @@
4346
echo '<div><small>Currently ' . $this->Html->link($status['workers'] . ' worker(s)', ['action' => 'processes']) . ' total.</small></div>';
4447
?>
4548
<?php
46-
echo '<div><small>' . count($servers) . ' CLI server(s): ' . implode(', ', $servers) .'</small></div>';
49+
echo '<div><small>' . count($servers) . ' CLI server(s): ' . implode(', ', $servers) . '</small></div>';
4750
?>
4851

4952
<?php } else { ?>
@@ -106,6 +109,61 @@
106109
?>
107110
</ol>
108111

112+
<p>
113+
<?php echo __d('queue', '{0} task(s) are scheduled to run in the future.', count($scheduledDetails)); ?>
114+
</p>
115+
<ol>
116+
<?php
117+
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 . '):';
119+
echo '<ul>';
120+
121+
$reset = '';
122+
if ($this->Queue->hasFailed($pendingJob)) {
123+
$reset = ' ' . $this->Form->postLink(__d('queue', 'Soft reset'), ['action' => 'resetJob', $pendingJob->id], ['confirm' => 'Sure?', 'class' => 'button primary btn margin btn-primary']);
124+
$reset .= ' ' . $this->Form->postLink(__d('queue', 'Remove'), ['action' => 'removeJob', $pendingJob->id], ['confirm' => 'Sure?', 'class' => 'button secondary btn margin btn-secondary']);
125+
} elseif ($pendingJob->fetched) {
126+
$reset .= ' ' . $this->Form->postLink(__d('queue', 'Remove'), ['action' => 'removeJob', $pendingJob->id], ['confirm' => 'Sure?', 'class' => 'button secondary btn margin btn-secondary']);
127+
}
128+
129+
$notBefore = '';
130+
if ($pendingJob->notbefore) {
131+
$notBefore = ' (' . __d('queue', 'scheduled {0}', $this->Time->nice($pendingJob->notbefore)) . ')';
132+
}
133+
134+
echo '<li>' . __d('queue', 'Created') . ': ' . $this->Time->nice($pendingJob->created) . $notBefore . '</li>';
135+
136+
if ($pendingJob->fetched) {
137+
echo '<li>' . __d('queue', 'Fetched') . ': ' . $this->Time->nice($pendingJob->fetched) . '</li>';
138+
139+
$status = '';
140+
if ($pendingJob->status) {
141+
$status = ' (' . __d('queue', 'status') . ': ' . h($pendingJob->status) . ')';
142+
}
143+
144+
if (!$pendingJob->failure_message) {
145+
echo '<li>';
146+
echo __d('queue', 'Progress') . ': ';
147+
echo $this->QueueProgress->progress($pendingJob) . $status;
148+
$textProgressBar = $this->QueueProgress->progressBar($pendingJob, 18);
149+
echo '<br>' . $this->QueueProgress->htmlProgressBar($pendingJob, $textProgressBar);
150+
echo '</li>';
151+
} else {
152+
echo '<li><i>' . $this->Queue->failureStatus($pendingJob) . '</i>';
153+
echo '<div>' . __d('queue', 'Attempts') . ': ' . $this->Queue->attempts($pendingJob) . $reset . '</div>';
154+
echo '</li>';
155+
if ($pendingJob->failure_message) {
156+
echo '<li>' . __d('queue', 'Failure Message') . ': ' . $this->Text->truncate($pendingJob->failure_message, 200) . '</li>';
157+
}
158+
}
159+
}
160+
161+
echo '</ul>';
162+
echo '</li>';
163+
}
164+
?>
165+
</ol>
166+
109167
<h2><?php echo __d('queue', 'Statistics'); ?></h2>
110168
<ul>
111169
<?php
@@ -156,8 +214,8 @@
156214
} elseif (is_bool($configuration)) {
157215
$configuration = $configuration ? 'true' : 'false';
158216
} elseif (is_array($configuration)) {
159-
$configuration = implode(', ', $configuration);
160-
}
217+
$configuration = implode(', ', $configuration);
218+
}
161219
echo h($key) . ': ' . h($configuration);
162220
echo '</li>';
163221
}
@@ -170,7 +228,7 @@
170228
<ul>
171229
<?php
172230
foreach ($addableTasks as $task => $className) {
173-
if (substr($task, 0, 6) === 'Queue.' && (substr($task, -7) === 'Example' || $task ==='Queue.Execute')) {
231+
if (substr($task, 0, 6) === 'Queue.' && (substr($task, -7) === 'Example' || $task === 'Queue.Execute')) {
174232
continue;
175233
}
176234

templates/element/search.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
echo $this->Form->create(null, ['valueSources' => 'query']);
1010
echo $this->Form->control('search', ['placeholder' => 'Auto-wildcard mode', 'label' => 'Search (Jobgroup/Reference)']);
1111
echo $this->Form->control('job_task', ['empty' => ' - no filter - ']);
12-
echo $this->Form->control('status', ['options' => ['completed' => 'Completed', 'in_progress' => 'In Progress'], 'empty' => ' - no filter - ']);
12+
echo $this->Form->control('status', ['options' => \Queue\Model\Entity\QueuedJob::statusesForSearch(), 'empty' => ' - no filter - ']);
1313
echo $this->Form->button('Filter', ['type' => 'submit']);
1414
if (!empty($_isSearch)) {
1515
echo $this->Html->link('Reset', ['action' => 'index'], ['class' => 'button']);

0 commit comments

Comments
 (0)