Passed
Pull Request — master (#44)
by Mischa ter
02:24
created

QueueExampleTask::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 9
rs 10
1
<?php
2
/**
3
 * A Simple QueueTask example.
4
 *
5
 * @property Queue.QueuedTask $QueuedTask
6
 */
0 ignored issues
show
Documentation Bug introduced by
The doc comment Queue.QueuedTask at position 0 could not be parsed: Unknown type name 'Queue.QueuedTask' at position 0 in Queue.QueuedTask.
Loading history...
7
class QueueExampleTask extends Shell {
0 ignored issues
show
Bug introduced by
The type Shell was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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.'));
0 ignored issues
show
Bug introduced by
The function __d was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
		$this->out(/** @scrutinizer ignore-call */ __d('queue', 'CakePHP Queue Example task.'));
Loading history...
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.'));
0 ignored issues
show
Bug introduced by
The function __d was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
		$this->out(/** @scrutinizer ignore-call */ __d('queue', 'CakePHP Queue Example task.'));
Loading history...
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