1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* TaskScheduler |
7
|
|
|
* |
8
|
|
|
* @author Raffael Sahli <[email protected]> |
9
|
|
|
* @copyright Copryright (c) 2017-2018 gyselroth GmbH (https://gyselroth.com) |
10
|
|
|
* @license MIT https://opensource.org/licenses/MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace TaskScheduler; |
14
|
|
|
|
15
|
|
|
use MongoDB\BSON\ObjectId; |
16
|
|
|
|
17
|
|
|
class Process |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Job. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $job; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Scheduler. |
28
|
|
|
* |
29
|
|
|
* @var Scheduler |
30
|
|
|
*/ |
31
|
|
|
protected $scheduler; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Events queue. |
35
|
|
|
* |
36
|
|
|
* @var MessageQueue |
37
|
|
|
*/ |
38
|
|
|
protected $events; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Initialize process. |
42
|
|
|
*/ |
43
|
31 |
|
public function __construct(array $job, Scheduler $scheduler, MessageQueue $events) |
44
|
|
|
{ |
45
|
31 |
|
$this->job = $job; |
46
|
31 |
|
$this->scheduler = $scheduler; |
47
|
31 |
|
$this->events = $events; |
48
|
31 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* To array. |
52
|
|
|
*/ |
53
|
18 |
|
public function toArray(): array |
54
|
|
|
{ |
55
|
18 |
|
return $this->job; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get job options. |
60
|
|
|
*/ |
61
|
5 |
|
public function getOptions(): array |
62
|
|
|
{ |
63
|
5 |
|
return $this->job; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get class. |
68
|
|
|
*/ |
69
|
1 |
|
public function getClass(): string |
70
|
|
|
{ |
71
|
1 |
|
return $this->job['class']; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get job data. |
76
|
|
|
*/ |
77
|
|
|
public function getData() |
78
|
|
|
{ |
79
|
|
|
return $this->job['data']; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get ID. |
84
|
|
|
*/ |
85
|
12 |
|
public function getId(): ObjectId |
86
|
|
|
{ |
87
|
12 |
|
return $this->job['_id']; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Restart job. |
92
|
|
|
*/ |
93
|
|
|
public function restart(): Process |
94
|
|
|
{ |
95
|
|
|
} |
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Kill running process. |
99
|
|
|
*/ |
100
|
|
|
public function kill(): bool |
101
|
|
|
{ |
102
|
|
|
} |
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Wait for job beeing executed. |
106
|
|
|
*/ |
107
|
2 |
|
public function wait(): Process |
108
|
|
|
{ |
109
|
2 |
|
$cursor = $this->events->getCursor([ |
110
|
2 |
|
'job' => $this->getId(), |
111
|
|
|
'status' => ['$gte' => JobInterface::STATUS_DONE], |
112
|
|
|
]); |
113
|
|
|
|
114
|
2 |
|
while (true) { |
115
|
2 |
|
if (null === $cursor->current()) { |
116
|
|
|
if ($cursor->getInnerIterator()->isDead()) { |
|
|
|
|
117
|
|
|
$this->events->create(); |
118
|
|
|
|
119
|
|
|
return $this->wait(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->events->next($cursor, function () { |
123
|
|
|
$this->wait(); |
124
|
|
|
}); |
125
|
|
|
|
126
|
|
|
continue; |
127
|
|
|
} |
128
|
|
|
|
129
|
2 |
|
$event = $cursor->current(); |
130
|
2 |
|
$this->events->next($cursor, function () { |
131
|
|
|
$this->wait(); |
132
|
2 |
|
}); |
133
|
|
|
|
134
|
2 |
|
$this->job['status'] = $event['status']; |
135
|
|
|
|
136
|
2 |
|
if (JobInterface::STATUS_FAILED === $this->job['status']) { |
137
|
1 |
|
$result = unserialize($event['data']); |
138
|
1 |
|
if ($result instanceof \Exception) { |
139
|
1 |
|
throw $result; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
1 |
|
return $this; |
144
|
|
|
} |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get status. |
149
|
|
|
*/ |
150
|
12 |
|
public function getStatus(): int |
151
|
|
|
{ |
152
|
12 |
|
return $this->job['status']; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: