1
|
|
|
<?php namespace Comodojo\Extender\Task; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Foundation\Utils\UniqueId; |
4
|
|
|
use \Comodojo\Extender\Traits\TasksRequestTrait; |
5
|
|
|
use \Comodojo\Extender\Socket\Messages\Task\Request as TaskRequestMessage; |
6
|
|
|
/** |
7
|
|
|
* @package Comodojo Extender |
8
|
|
|
* @author Marco Giovinazzi <[email protected]> |
9
|
|
|
* @license MIT |
10
|
|
|
* |
11
|
|
|
* LICENSE: |
12
|
|
|
* |
13
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
14
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
15
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
16
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
17
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
18
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
19
|
|
|
* THE SOFTWARE. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
class Request { |
23
|
|
|
|
24
|
|
|
use TasksRequestTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
protected $uid; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
protected $parent_uid; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
protected $jid; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var int |
43
|
|
|
*/ |
44
|
|
|
protected $start_timestamp; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
protected $pid; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var Request |
53
|
|
|
*/ |
54
|
|
|
protected $done; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var Request |
58
|
|
|
*/ |
59
|
|
|
protected $fail; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var Request |
63
|
|
|
*/ |
64
|
|
|
protected $pipe; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var TaskParameters |
68
|
|
|
*/ |
69
|
|
|
protected $parameters = null; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Class constructor |
73
|
|
|
* |
74
|
|
|
* @param string $name |
75
|
|
|
* @param string $task |
76
|
|
|
* @param TaskParameters $parameters |
77
|
|
|
*/ |
78
|
|
|
public function __construct($name, $task, TaskParameters $parameters = null) { |
79
|
|
|
|
80
|
|
|
$this->uid = UniqueId::generate(); |
|
|
|
|
81
|
|
|
|
82
|
|
|
$this->setName($name); |
83
|
|
|
$this->setTask($task); |
84
|
|
|
$this->setParameters($parameters); |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get current unique id |
90
|
|
|
* |
91
|
|
|
* @return int |
92
|
|
|
*/ |
93
|
|
|
public function getUid() { |
94
|
|
|
|
95
|
|
|
return $this->uid; |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function setUid($uid) { |
100
|
|
|
|
101
|
|
|
$this->uid = $uid; |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get parent unique id |
109
|
|
|
* |
110
|
|
|
* @return int |
111
|
|
|
*/ |
112
|
|
|
public function getParentUid() { |
113
|
|
|
|
114
|
|
|
return $this->parent_uid; |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function setParentUid($uid) { |
119
|
|
|
|
120
|
|
|
$this->parent_uid = $uid; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get current job id |
128
|
|
|
* |
129
|
|
|
* @return int |
130
|
|
|
*/ |
131
|
|
|
public function getJid() { |
132
|
|
|
|
133
|
|
|
return $this->jid; |
134
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function setJid($jid) { |
138
|
|
|
|
139
|
|
|
$this->jid = $jid; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get parameters |
147
|
|
|
* |
148
|
|
|
* @return TaskParameters |
149
|
|
|
*/ |
150
|
|
|
public function getParameters() { |
151
|
|
|
|
152
|
|
|
return $this->parameters; |
153
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function setParameters(TaskParameters $parameters = null) { |
157
|
|
|
|
158
|
|
|
$this->parameters = is_null($parameters) ? new TaskParameters() : $parameters; |
159
|
|
|
|
160
|
|
|
return $this; |
161
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get start timestamp (microseconds) |
166
|
|
|
* |
167
|
|
|
* @return float |
168
|
|
|
*/ |
169
|
|
|
public function getStartTimestamp() { |
170
|
|
|
|
171
|
|
|
return $this->start_timestamp; |
172
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function setStartTimestamp($time) { |
176
|
|
|
|
177
|
|
|
$this->start_timestamp = $time; |
178
|
|
|
|
179
|
|
|
return $this; |
180
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get pid |
185
|
|
|
* |
186
|
|
|
* @return int |
187
|
|
|
*/ |
188
|
|
|
public function getPid() { |
189
|
|
|
|
190
|
|
|
return $this->pid; |
191
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function setPid($pid) { |
195
|
|
|
|
196
|
|
|
$this->pid = $pid; |
197
|
|
|
|
198
|
|
|
return $this; |
199
|
|
|
|
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function hasOnDone() { |
203
|
|
|
|
204
|
|
|
return $this->done !== null; |
205
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function getOnDone() { |
209
|
|
|
|
210
|
|
|
return $this->done; |
211
|
|
|
|
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function onDone(Request $request = null) { |
215
|
|
|
|
216
|
|
|
$this->done = $request; |
217
|
|
|
|
218
|
|
|
return $this; |
219
|
|
|
|
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function hasOnFail() { |
223
|
|
|
|
224
|
|
|
return $this->fail !== null; |
225
|
|
|
|
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function getOnFail() { |
229
|
|
|
|
230
|
|
|
return $this->fail; |
231
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function onFail(Request $request = null) { |
235
|
|
|
|
236
|
|
|
$this->fail = $request; |
237
|
|
|
|
238
|
|
|
return $this; |
239
|
|
|
|
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function hasPipe() { |
243
|
|
|
|
244
|
|
|
return $this->pipe !== null; |
245
|
|
|
|
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function getPipe() { |
249
|
|
|
|
250
|
|
|
return $this->pipe; |
251
|
|
|
|
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function pipe(Request $request = null) { |
255
|
|
|
|
256
|
|
|
$this->pipe = $request; |
257
|
|
|
|
258
|
|
|
return $this; |
259
|
|
|
|
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
public function isChain() { |
263
|
|
|
|
264
|
|
|
return ( $this->done !== null || $this->fail !== null || $this->pipe !== null ); |
265
|
|
|
|
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
public function convertToMessage() { |
269
|
|
|
|
270
|
|
|
$message = new TaskRequestMessage(); |
271
|
|
|
|
272
|
|
|
$message->setName($this->getName()); |
273
|
|
|
$message->setTask($this->getTask()); |
274
|
|
|
$message->setNiceness($this->getNiceness()); |
275
|
|
|
$message->setMaxtime($this->getMaxtime()); |
276
|
|
|
$message->setParameters($this->getParameters()->export()); |
277
|
|
|
|
278
|
|
|
if ( $this->done !== null ) $message->onDone( $this->getOnDone()->convertToMessage() ); |
279
|
|
|
if ( $this->fail !== null ) $message->onFail( $this->getOnFail()->convertToMessage() ); |
280
|
|
|
if ( $this->pipe !== null ) $message->pipe( $this->getPipe()->convertToMessage() ); |
281
|
|
|
|
282
|
|
|
return $message; |
283
|
|
|
|
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
public static function create($name, $task, TaskParameters $parameters = null) { |
287
|
|
|
|
288
|
|
|
return new Request($name, $task, $parameters); |
289
|
|
|
|
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
public static function createFromMessage(TaskRequestMessage $message) { |
293
|
|
|
|
294
|
|
|
$name = $message->getName(); |
295
|
|
|
$task = $message->getTask(); |
296
|
|
|
$niceness = $message->getNiceness(); |
297
|
|
|
$maxtime = $message->getMaxtime(); |
298
|
|
|
$parameters = $message->getParameters(); |
299
|
|
|
$done = $message->getOnDone(); |
300
|
|
|
$fail = $message->getOnFail(); |
301
|
|
|
$pipe = $message->getPipe(); |
302
|
|
|
|
303
|
|
|
if ( empty($name) || empty($task) ) { |
304
|
|
|
throw new Exception("Invalid task name or task reference"); |
|
|
|
|
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
$request = Request::create( |
308
|
|
|
$name, |
309
|
|
|
$task, |
310
|
|
|
new TaskParameters($parameters) |
311
|
|
|
); |
312
|
|
|
|
313
|
|
|
$request |
314
|
|
|
->setNiceness($niceness) |
315
|
|
|
->setMaxTime($maxtime) |
316
|
|
|
->onDone(); |
317
|
|
|
|
318
|
|
|
$request->onDone(empty($done) ? null : Request::createFromMessage($done)); |
319
|
|
|
$request->onFail(empty($fail) ? null : Request::createFromMessage($fail)); |
320
|
|
|
$request->pipe(empty($pipe) ? null : Request::createFromMessage($pipe)); |
321
|
|
|
|
322
|
|
|
return $request; |
323
|
|
|
|
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
} |
327
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.