1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /** |
||
6 | * TaskScheduler |
||
7 | * |
||
8 | * @author Raffael Sahli <[email protected]> |
||
9 | * @copyright Copryright (c) 2017-2019 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 | 46 | public function __construct(array $job, Scheduler $scheduler, MessageQueue $events) |
|
44 | { |
||
45 | 46 | $this->job = $job; |
|
46 | 46 | $this->scheduler = $scheduler; |
|
47 | 46 | $this->events = $events; |
|
48 | 46 | } |
|
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 | 10 | public function getOptions(): array |
|
62 | { |
||
63 | 10 | return $this->job['options']; |
|
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 | 1 | public function getData() |
|
78 | { |
||
79 | 1 | return $this->job['data']; |
|
80 | } |
||
81 | |||
82 | /** |
||
83 | * Get ID. |
||
84 | */ |
||
85 | 32 | public function getId(): ObjectId |
|
86 | { |
||
87 | 32 | return $this->job['_id']; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * Restart job. |
||
92 | */ |
||
93 | 1 | public function getWorker(): ObjectId |
|
94 | { |
||
95 | 1 | return $this->job['worker']; |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * Wait for job beeing executed. |
||
100 | */ |
||
101 | 2 | public function wait(): Process |
|
102 | { |
||
103 | 2 | $cursor = $this->events->getCursor([ |
|
104 | 2 | 'job' => $this->getId(), |
|
105 | 'status' => ['$gte' => JobInterface::STATUS_DONE], |
||
106 | ]); |
||
107 | |||
108 | 2 | while (true) { |
|
109 | 2 | if (null === $cursor->current()) { |
|
110 | if ($cursor->getInnerIterator()->isDead()) { |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
111 | $this->events->create(); |
||
112 | |||
113 | return $this->wait(); |
||
114 | } |
||
115 | |||
116 | $this->events->next($cursor, function () { |
||
117 | $this->wait(); |
||
118 | }); |
||
119 | |||
120 | continue; |
||
121 | } |
||
122 | |||
123 | 2 | $event = $cursor->current(); |
|
124 | 2 | $this->events->next($cursor, function () { |
|
125 | $this->wait(); |
||
126 | 2 | }); |
|
127 | |||
128 | 2 | $this->job['status'] = $event['status']; |
|
129 | |||
130 | 2 | if (JobInterface::STATUS_FAILED === $this->job['status'] && isset($event['exception'])) { |
|
131 | 1 | throw new $event['exception']['class']( |
|
132 | 1 | $event['exception']['message'], |
|
133 | 1 | $event['exception']['code'] |
|
134 | ); |
||
135 | } |
||
136 | |||
137 | 1 | return $this; |
|
138 | } |
||
0 ignored issues
–
show
In this branch, the function will implicitly return
null which is incompatible with the type-hinted return TaskScheduler\Process . Consider adding a return statement or allowing null as return value.
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: interface ReturnsInt {
public function returnsIntHinted(): int;
}
class MyClass implements ReturnsInt {
public function returnsIntHinted(): int
{
if (foo()) {
return 123;
}
// here: null is implicitly returned
}
}
![]() |
|||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Get status. |
||
143 | */ |
||
144 | 26 | public function getStatus(): int |
|
145 | { |
||
146 | 26 | return $this->job['status']; |
|
147 | } |
||
148 | } |
||
149 |