Completed
Pull Request — master (#35)
by
unknown
02:56
created

QueueExampleTask::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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