-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathProgressExampleTask.php
More file actions
91 lines (79 loc) · 2.53 KB
/
ProgressExampleTask.php
File metadata and controls
91 lines (79 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
declare(strict_types=1);
namespace Queue\Queue\Task;
use Queue\Queue\AddFromBackendInterface;
use Queue\Queue\AddInterface;
use Queue\Queue\Task;
/**
* A Simple QueueTask example that runs for a while and updates the progress field.
*/
class ProgressExampleTask extends Task implements AddInterface, AddFromBackendInterface {
/**
* Timeout for run, after which the Task is reassigned to a new worker.
*/
public ?int $timeout = 120;
/**
* @var int
*/
public const MINUTE = 60;
/**
* Example add functionality.
* Will create one example job in the queue, which later will be executed using run();
*
* To invoke from CLI execute:
* - bin/cake queue add Queue.ProgressExample
*
* @param string|null $data
*
* @return void
*/
public function add(?string $data): void {
$this->io->out('CakePHP Queue ProgressExample task.');
$this->io->hr();
$this->io->out($this->description() ?? '');
$this->io->out('I will now add the Job into the Queue.');
$this->io->out(' ');
$this->io->out('To run a Worker use:');
$this->io->out(' bin/cake queue run');
$this->io->out(' ');
$this->io->out('You can find the sourcecode of this task in:');
$this->io->out(__FILE__);
$this->io->out(' ');
$data = [
'duration' => 2 * static::MINUTE,
];
$this->QueuedJobs->createJob('Queue.ProgressExample', $data);
$this->io->success('OK, job created, now run the worker');
}
/**
* @return string|null
*/
public function description(): ?string {
return __d('queue', 'Long-running task (~2 min) with progress bar updates');
}
/**
* Example run function.
* This function is executed, when a worker is executing a task.
* The return parameter will determine, if the task will be marked completed, or be requeued.
*
* Defaults to 120 seconds
*
* @param array<string, mixed> $data The array passed to QueuedJobsTable::createJob()
* @param int $jobId The id of the QueuedJob entity
*
* @return void
*/
public function run(array $data, int $jobId): void {
$this->io->hr();
$this->io->out('CakePHP Queue ProgressExample task.');
$seconds = !empty($data['duration']) ? (int)$data['duration'] : 2 * static::MINUTE;
$this->io->out('A total of ' . $seconds . ' seconds need to pass...');
for ($i = 0; $i < $seconds; $i++) {
sleep(1);
$this->QueuedJobs->updateProgress($jobId, ($i + 1) / $seconds, 'Status Test ' . ($i + 1) . 's');
}
$this->QueuedJobs->updateProgress($jobId, 1, 'Status Test Done');
$this->io->hr();
$this->io->success(' -> Success, the ProgressExample Job was run. <-');
}
}