|
1
|
|
|
<?php namespace Comodojo\Extender\Tasks; |
|
2
|
|
|
|
|
3
|
|
|
use \Comodojo\Extender\Components\Parameters; |
|
4
|
|
|
use \Comodojo\Dispatcher\Components\Timestamp as TimestampTrait; |
|
5
|
|
|
use \Psr\Log\LoggerInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Task object |
|
9
|
|
|
* |
|
10
|
|
|
* @package Comodojo extender |
|
11
|
|
|
* @author Marco Giovinazzi <[email protected]> |
|
12
|
|
|
* @license GPL-3.0+ |
|
13
|
|
|
* |
|
14
|
|
|
* LICENSE: |
|
15
|
|
|
* |
|
16
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
17
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
18
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
19
|
|
|
* License, or (at your option) any later version. |
|
20
|
|
|
* |
|
21
|
|
|
* This program is distributed in the hope that it will be useful, |
|
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
24
|
|
|
* GNU Affero General Public License for more details. |
|
25
|
|
|
* |
|
26
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
27
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
28
|
|
|
*/ |
|
29
|
|
|
|
|
30
|
|
|
abstract class Task { |
|
31
|
|
|
|
|
32
|
|
|
use TimestampTrait; |
|
33
|
|
|
|
|
34
|
|
|
public $parameters; |
|
35
|
|
|
|
|
36
|
|
|
public $name; |
|
37
|
|
|
|
|
38
|
|
|
public $jobid; |
|
39
|
|
|
|
|
40
|
|
|
public $pid; |
|
41
|
|
|
|
|
42
|
|
|
protected $configuration; |
|
43
|
|
|
|
|
44
|
|
|
protected $logger; |
|
45
|
|
|
|
|
46
|
|
|
private $worklog; |
|
47
|
|
|
|
|
48
|
|
|
private $worklog_id; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Task constructor. |
|
52
|
|
|
* |
|
53
|
|
|
* @param array $parameters Array of parameters (if any) |
|
54
|
|
|
* @param \Monolog\Logger $logger |
|
55
|
|
|
* @param int $pid Task PID (if any) |
|
|
|
|
|
|
56
|
|
|
* @param string $name Task Name |
|
57
|
|
|
* @param int $timestamp Start timestamp (if null will be retrieved directly) |
|
58
|
|
|
* @param bool $multithread Multithread switch |
|
|
|
|
|
|
59
|
|
|
* |
|
60
|
|
|
* @return Object $this |
|
|
|
|
|
|
61
|
|
|
*/ |
|
62
|
|
|
final public function __construct( |
|
63
|
|
|
Configuration $configuration, |
|
64
|
|
|
LoggerInterface $logger, |
|
65
|
|
|
$name = 'EXTENDERTASK', |
|
66
|
|
|
$timestamp = null, |
|
67
|
|
|
$jobid = null, |
|
|
|
|
|
|
68
|
|
|
$parameters = array() |
|
69
|
|
|
) { |
|
70
|
|
|
|
|
71
|
|
|
// Setup task |
|
72
|
|
|
$this->configuration = $configuration; |
|
73
|
|
|
$this->logger = $logger; |
|
74
|
|
|
$this->parameters = new Parameters($parameters); |
|
75
|
|
|
$this->worklog = new Worklog($configuration, $logger); |
|
76
|
|
|
|
|
77
|
|
|
$this->name = $name; |
|
78
|
|
|
$this->setTimestamp($timestamp); |
|
79
|
|
|
$this->pid = getmypid(); |
|
80
|
|
|
|
|
81
|
|
|
// Setup an exit strategy if multithread enabled (parent may kill child process if timeout exceeded) |
|
82
|
|
|
|
|
83
|
|
View Code Duplication |
pcntl_signal(SIGTERM, function() { |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
$end = microtime(true); |
|
86
|
|
|
|
|
87
|
|
|
if ( !is_null($this->worklog_id) ) $this->worklog->close($this->worklog_id, false, 'Job killed (timeout exceeded?)', $end); |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
exit(1); |
|
90
|
|
|
|
|
91
|
|
|
}); |
|
92
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* The run method; SHOULD be implemented by each task |
|
97
|
|
|
*/ |
|
98
|
|
|
abstract public function run(); |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Start task! |
|
103
|
|
|
* |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
|
|
final public function start() { |
|
107
|
|
|
|
|
108
|
|
|
try { |
|
109
|
|
|
|
|
110
|
|
|
return $this->execTask(); |
|
111
|
|
|
|
|
112
|
|
|
} catch (Exception $e) { |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
throw new TaskException($e->getMessage(), $e->getCode(), $e, $this->worklog_id); |
|
115
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Execute task. |
|
122
|
|
|
* |
|
123
|
|
|
* This method provides to: |
|
124
|
|
|
* - setup worklog |
|
125
|
|
|
* - invoke method "run", that should be defined in task implementation |
|
126
|
|
|
*/ |
|
127
|
|
View Code Duplication |
private function execTask() { |
|
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
try { |
|
130
|
|
|
|
|
131
|
|
|
// open worklog |
|
132
|
|
|
|
|
133
|
|
|
$this->worklog_id = $this->worklog->create($this->pid, $this->name, $this->class, $this->start_timestamp); |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
$this->result = $this->run(); |
|
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
$this->end_timestamp = microtime(true); |
|
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
$this->closeWorklog(true); |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
} catch (Exception $e) { |
|
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
$this->result = $e->getMessage(); |
|
144
|
|
|
|
|
145
|
|
|
if ( !is_null($this->worklog_id) ) { |
|
146
|
|
|
|
|
147
|
|
|
if ( is_null($this->end_timestamp) ) $this->end_timestamp = microtime(true); |
|
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
$this->closeWorklog(false); |
|
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
throw $e; |
|
154
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return array( |
|
158
|
|
|
"success" => true, |
|
159
|
|
|
"timestamp" => $this->end_timestamp, |
|
|
|
|
|
|
160
|
|
|
"result" => $this->result, |
|
161
|
|
|
"worklogid" => $this->worklog_id |
|
162
|
|
|
); |
|
163
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.