1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* A Simple QueueTask example. |
4
|
|
|
* |
5
|
|
|
* @property Queue.QueuedTask $QueuedTask |
6
|
|
|
*/ |
|
|
|
|
7
|
|
|
class QueueExampleTask extends Shell { |
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* An array of names of models to load. |
11
|
|
|
* |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
public $uses = ['Queue.QueuedTask']; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Timeout for run, after which the task is reassigned to a new worker. |
18
|
|
|
* |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
public $timeout = 10; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Timeout for cleanup, after which completed jobs are deleted (in seconds). |
25
|
|
|
* |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
public $cleanupTimeout = 600; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Number of times a failed instance of this task should be restarted before giving up. |
32
|
|
|
* |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
public $retries = 0; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Stores any failure messages triggered during run(). |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
public $failureMessage = ''; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Example add functionality. |
46
|
|
|
* |
47
|
|
|
* Will create one example job in the queue, which later will be executed using run(). |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
public function add() { |
52
|
|
|
$this->out(__d('queue', 'CakePHP Queue Example task.')); |
|
|
|
|
53
|
|
|
$this->hr(); |
54
|
|
|
$this->out(__d('queue', 'This is a very simple example of a queueTask.')); |
55
|
|
|
$this->out(__d('queue', 'Now adding an example Task Job into the Queue.')); |
56
|
|
|
$this->out(__d('queue', 'This task will only produce some console output on the worker that it runs on.')); |
57
|
|
|
$this->out(' '); |
58
|
|
|
$this->out(__d('queue', 'To run a Worker use:')); |
59
|
|
|
$this->out(__d('queue', ' cake queue runworker')); |
60
|
|
|
$this->out(' '); |
61
|
|
|
$this->out(__d('queue', 'You can find the sourcecode of this task in: ')); |
62
|
|
|
$this->out(__FILE__); |
63
|
|
|
$this->out(' '); |
64
|
|
|
|
65
|
|
|
// Adding a task of type 'example' with no additionally passed data |
66
|
|
|
if ($this->QueuedTask->createJob('Example', [])) { |
67
|
|
|
$this->out(__d('queue', 'OK, job created, now run the worker')); |
68
|
|
|
} else { |
69
|
|
|
$this->err(__d('queue', 'Could not create Job')); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Example run function. |
75
|
|
|
* |
76
|
|
|
* This function is executed, when a worker is executing a task. |
77
|
|
|
* The return parameter will determine, if the task will be marked completed, or be requeued. |
78
|
|
|
* |
79
|
|
|
* @param array $data Job data (passed on creation) |
80
|
|
|
* @return bool Success |
81
|
|
|
*/ |
82
|
|
|
public function run(array $data) : bool { |
83
|
|
|
$this->hr(); |
84
|
|
|
$this->out(__d('queue', 'CakePHP Queue Example task.')); |
|
|
|
|
85
|
|
|
$this->hr(); |
86
|
|
|
$this->out(__d('queue', ' ->Success, the Example Task was run.<-')); |
87
|
|
|
$this->out(' '); |
88
|
|
|
$this->out(' '); |
89
|
|
|
|
90
|
|
|
return true; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|