Skip to content

Commit b79bcbd

Browse files
committed
Add memory usage info into jobs.
1 parent a18df10 commit b79bcbd

File tree

17 files changed

+143
-26
lines changed

17 files changed

+143
-26
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"require-dev": {
3434
"cakedc/cakephp-phpstan": "^4.0.0",
3535
"cakephp/bake": "^3.0.1",
36-
"cakephp/migrations": "^4.0.1",
36+
"cakephp/migrations": "^4.5.1",
3737
"dereuromark/cakephp-ide-helper": "^2.0.0",
3838
"dereuromark/cakephp-templating": "^0.2.1",
3939
"dereuromark/cakephp-tools": "^3.0.0",

config/Migrations/20240307154751_MigrationQueueInitV8.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
declare(strict_types=1);
33

44
use Cake\Datasource\ConnectionManager;
5-
use Migrations\AbstractMigration;
5+
use Migrations\BaseMigration;
66

7-
class MigrationQueueInitV8 extends AbstractMigration {
7+
class MigrationQueueInitV8 extends BaseMigration {
88

99
/**
1010
* Up Method.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Migrations\BaseMigration;
5+
6+
class MigrationQueueMemory extends BaseMigration {
7+
8+
/**
9+
* Change Method.
10+
*
11+
* More information on this method is available here:
12+
* https://book.cakephp.org/migrations/4/en/migrations.html#the-change-method
13+
*
14+
* @return void
15+
*/
16+
public function change(): void {
17+
$this->table('queued_jobs')
18+
->addColumn('memory', 'integer', [
19+
'default' => null,
20+
'null' => true,
21+
'comment' => 'MB',
22+
'signed' => false,
23+
])
24+
->update();
25+
}
26+
27+
}

src/Command/InfoCommand.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use Cake\I18n\Number;
1212
use Queue\Queue\TaskFinder;
1313

14+
/**
15+
* @property \Queue\Model\Table\QueuedJobsTable $QueuedJobs
16+
*/
1417
class InfoCommand extends Command {
1518

1619
/**

src/Controller/Admin/QueueProcessesController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
/**
1111
* @property \Queue\Model\Table\QueueProcessesTable $QueueProcesses
12-
* @method \Cake\Datasource\ResultSetInterface<\Queue\Model\Entity\QueueProcess> paginate($object = null, array $settings = [])
12+
* @method \Cake\Datasource\ResultSetInterface<\Queue\Model\Entity\QueueProcess> paginate(\Cake\Datasource\RepositoryInterface|\Cake\Datasource\QueryInterface|string|null $object = null, array $settings = [])
1313
* @property \Queue\Model\Table\QueuedJobsTable $QueuedJobs
1414
*/
1515
class QueueProcessesController extends AppController {

src/Controller/Admin/QueuedJobsController.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,23 @@ public function delete(?int $id = null) {
271271
return $this->redirect(['action' => 'index']);
272272
}
273273

274+
/**
275+
* @param int|null $id Queued Job id.
276+
*
277+
* @return \Cake\Http\Response|null|void Redirects to index.
278+
*/
279+
public function clone(?int $id = null) {
280+
$this->request->allowMethod(['post', 'put']);
281+
$queuedJob = $this->QueuedJobs->get($id);
282+
if ($this->QueuedJobs->clone($queuedJob)) {
283+
$this->Flash->success(__d('queue', 'The queued job has been cloned and will now run.'));
284+
} else {
285+
$this->Flash->error(__d('queue', 'The queued job could not be cloned. Please try again.'));
286+
}
287+
288+
return $this->redirect(['controller' => 'Queue', 'action' => 'index']);
289+
}
290+
274291
/**
275292
* @throws \Cake\Http\Exception\NotFoundException
276293
*

src/Model/Entity/QueuedJob.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
* @property int $id
1010
* @property string $job_task
11-
* @property array<string, mixed>|null $data
11+
* @property array|null $data
1212
* @property string|null $job_group
1313
* @property string|null $reference
1414
* @property \Cake\I18n\DateTime $created
@@ -22,6 +22,7 @@
2222
* @property string|null $status
2323
* @property int $priority
2424
* @property \Queue\Model\Entity\QueueProcess $worker_process
25+
* @property int|null $memory
2526
*/
2627
class QueuedJob extends Entity {
2728

src/Model/Table/QueueProcessesTable.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@
2323
* @method \Queue\Model\Entity\QueueProcess|false save(\Cake\Datasource\EntityInterface $entity, array $options = [])
2424
* @method \Queue\Model\Entity\QueueProcess patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
2525
* @method array<\Queue\Model\Entity\QueueProcess> patchEntities(iterable $entities, array $data, array $options = [])
26-
* @method \Queue\Model\Entity\QueueProcess findOrCreate($search, ?callable $callback = null, array $options = [])
26+
* @method \Queue\Model\Entity\QueueProcess findOrCreate(\Cake\ORM\Query\SelectQuery|callable|array $search, ?callable $callback = null, array $options = [])
2727
* @method \Queue\Model\Entity\QueueProcess saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = [])
2828
* @method \Queue\Model\Entity\QueueProcess newEmptyEntity()
2929
* @method \Cake\Datasource\ResultSetInterface<\Queue\Model\Entity\QueueProcess>|false saveMany(iterable $entities, array $options = [])
3030
* @method \Cake\Datasource\ResultSetInterface<\Queue\Model\Entity\QueueProcess> saveManyOrFail(iterable $entities, array $options = [])
3131
* @method \Cake\Datasource\ResultSetInterface<\Queue\Model\Entity\QueueProcess>|false deleteMany(iterable $entities, array $options = [])
3232
* @method \Cake\Datasource\ResultSetInterface<\Queue\Model\Entity\QueueProcess> deleteManyOrFail(iterable $entities, array $options = [])
3333
* @property \Queue\Model\Table\QueuedJobsTable&\Cake\ORM\Association\HasOne $CurrentQueuedJobs
34+
* @extends \Cake\ORM\Table<array{Timestamp: \Cake\ORM\Behavior\TimestampBehavior}>
3435
*/
3536
class QueueProcessesTable extends Table {
3637

src/Model/Table/QueuedJobsTable.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Queue\Model\Entity\QueuedJob;
1919
use Queue\Queue\Config;
2020
use Queue\Queue\TaskFinder;
21+
use Queue\Utility\Memory;
2122
use RuntimeException;
2223
use Search\Manager;
2324

@@ -30,7 +31,7 @@
3031
* @method \Queue\Model\Entity\QueuedJob|false save(\Cake\Datasource\EntityInterface $entity, array $options = [])
3132
* @method \Queue\Model\Entity\QueuedJob patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
3233
* @method array<\Queue\Model\Entity\QueuedJob> patchEntities(iterable $entities, array $data, array $options = [])
33-
* @method \Queue\Model\Entity\QueuedJob findOrCreate($search, ?callable $callback = null, array $options = [])
34+
* @method \Queue\Model\Entity\QueuedJob findOrCreate(\Cake\ORM\Query\SelectQuery|callable|array $search, ?callable $callback = null, array $options = [])
3435
* @mixin \Cake\ORM\Behavior\TimestampBehavior
3536
* @method \Queue\Model\Entity\QueuedJob saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = [])
3637
* @mixin \Search\Model\Behavior\SearchBehavior
@@ -40,6 +41,7 @@
4041
* @method \Cake\Datasource\ResultSetInterface<\Queue\Model\Entity\QueuedJob> saveManyOrFail(iterable $entities, array $options = [])
4142
* @method \Cake\Datasource\ResultSetInterface<\Queue\Model\Entity\QueuedJob>|false deleteMany(iterable $entities, array $options = [])
4243
* @method \Cake\Datasource\ResultSetInterface<\Queue\Model\Entity\QueuedJob> deleteManyOrFail(iterable $entities, array $options = [])
44+
* @extends \Cake\ORM\Table<array{Search: \Search\Model\Behavior\SearchBehavior, Timestamp: \Cake\ORM\Behavior\TimestampBehavior}>
4345
*/
4446
class QueuedJobsTable extends Table {
4547

@@ -720,6 +722,7 @@ public function updateProgress(int $id, float $progress, ?string $status = null)
720722

721723
$values = [
722724
'progress' => round($progress, 2),
725+
'memory' => Memory::usage(),
723726
];
724727
if ($status !== null) {
725728
$values['status'] = $status;
@@ -739,6 +742,7 @@ public function markJobDone(QueuedJob $job): bool {
739742
$fields = [
740743
'progress' => 1,
741744
'completed' => $this->getDateTime(),
745+
'memory' => Memory::usage(),
742746
];
743747
$job = $this->patchEntity($job, $fields);
744748

@@ -756,6 +760,7 @@ public function markJobDone(QueuedJob $job): bool {
756760
public function markJobFailed(QueuedJob $job, ?string $failureMessage = null): bool {
757761
$fields = [
758762
'failure_message' => $failureMessage,
763+
'memory' => Memory::usage(),
759764
];
760765
$job = $this->patchEntity($job, $fields);
761766

@@ -796,6 +801,7 @@ public function reset(?int $id = null, bool $full = false): int {
796801
'attempts' => 0,
797802
'workerkey' => null,
798803
'failure_message' => null,
804+
'memory' => null,
799805
];
800806
$conditions = [
801807
'completed IS' => null,
@@ -828,6 +834,7 @@ public function rerunByTask(string $task, ?string $reference = null): int {
828834
'attempts' => 0,
829835
'workerkey' => null,
830836
'failure_message' => null,
837+
'memory' => null,
831838
];
832839
$conditions = [
833840
'completed IS NOT' => null,
@@ -853,6 +860,7 @@ public function rerun(int $id): int {
853860
'attempts' => 0,
854861
'workerkey' => null,
855862
'failure_message' => null,
863+
'memory' => null,
856864
];
857865
$conditions = [
858866
'completed IS NOT' => null,
@@ -862,6 +870,28 @@ public function rerun(int $id): int {
862870
return $this->updateAll($fields, $conditions);
863871
}
864872

873+
/**
874+
* @param \Queue\Model\Entity\QueuedJob $queuedJob
875+
* @return \Queue\Model\Entity\QueuedJob|null
876+
*/
877+
public function clone(QueuedJob $queuedJob): ?QueuedJob
878+
{
879+
$defaults = [
880+
'completed' => null,
881+
'fetched' => null,
882+
'progress' => null,
883+
'attempts' => 0,
884+
'workerkey' => null,
885+
'failure_message' => null,
886+
'memory' => null,
887+
];
888+
$data = $defaults + $queuedJob->toArray();
889+
$data['created'] = new DateTime();
890+
$queuedJob = $this->newEntity($data);
891+
892+
return $this->save($queuedJob) ?: null;
893+
}
894+
865895
/**
866896
* Return some statistics about unfinished jobs still in the Database.
867897
*
@@ -881,6 +911,7 @@ public function getPendingStats(): SelectQuery {
881911
'notbefore',
882912
'attempts',
883913
'failure_message',
914+
'memory',
884915
],
885916
'conditions' => [
886917
'completed IS' => null,
@@ -911,6 +942,7 @@ public function getScheduledStats(): SelectQuery {
911942
'notbefore',
912943
'attempts',
913944
'failure_message',
945+
'memory',
914946
],
915947
'conditions' => [
916948
'completed IS' => null,

src/Queue/TaskFinder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ protected function getTasks(string $path, ?string $plugin = null): array {
129129
*/
130130
public function resolve(string $jobTask): string {
131131
if (Configure::read('Queue.skipExistenceCheck')) {
132-
if (strpos($jobTask, '\\') === false) {
132+
if (!str_contains($jobTask, '\\')) {
133133
return $jobTask;
134134
}
135135

@@ -143,10 +143,10 @@ public function resolve(string $jobTask): string {
143143
}
144144
}
145145

146-
if (strpos($jobTask, '\\') === false) {
146+
if (!str_contains($jobTask, '\\')) {
147147
// Let's try matching without plugin prefix
148148
foreach ($all as $name => $className) {
149-
if (strpos($name, '.') === false) {
149+
if (!str_contains($name, '.')) {
150150
continue;
151151
}
152152
[$plugin, $name] = explode('.', $name, 2);

0 commit comments

Comments
 (0)