-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathExampleTask.php
More file actions
77 lines (67 loc) · 2.08 KB
/
ExampleTask.php
File metadata and controls
77 lines (67 loc) · 2.08 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
<?php
declare(strict_types=1);
/**
* @author MGriesbach@gmail.com
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace Queue\Queue\Task;
use Queue\Queue\AddFromBackendInterface;
use Queue\Queue\AddInterface;
use Queue\Queue\Task;
/**
* A Simple QueueTask example.
*/
class ExampleTask extends Task implements AddInterface, AddFromBackendInterface {
/**
* Timeout for run, after which the Task is reassigned to a new worker.
*/
public ?int $timeout = 10;
/**
* 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.Example
*
* @param string|null $data Optional data for the task, make sure to "quote multi words"
*
* @return void
*/
public function add(?string $data): void {
$this->io->out('CakePHP Queue Example task.');
$this->io->hr();
$this->io->out($this->description() ?? '');
$this->io->out('I will now add an example 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(' ');
$this->QueuedJobs->createJob('Queue.Example');
$this->io->success('OK, job created, now run the worker');
}
/**
* @return string|null
*/
public function description(): ?string {
return __d('queue', 'Simple task that outputs to console');
}
/**
* 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.
*
* @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 Example task.');
$this->io->hr();
$this->io->success(' -> Success, the Example Job was run. <-');
}
}