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
|
|
|
* Initialize process. |
35
|
|
|
*/ |
36
|
47 |
|
public function __construct(array $job, Scheduler $scheduler) |
37
|
|
|
{ |
38
|
47 |
|
$this->job = $job; |
39
|
47 |
|
$this->scheduler = $scheduler; |
40
|
47 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Replace process data |
44
|
|
|
*/ |
45
|
2 |
|
public function replace(Process $process): self |
46
|
|
|
{ |
47
|
2 |
|
$this->job = $process->toArray(); |
48
|
2 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* To array. |
53
|
|
|
*/ |
54
|
18 |
|
public function toArray(): array |
55
|
|
|
{ |
56
|
18 |
|
return $this->job; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get job options. |
61
|
|
|
*/ |
62
|
10 |
|
public function getOptions(): array |
63
|
|
|
{ |
64
|
10 |
|
return $this->job['options']; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get class. |
69
|
|
|
*/ |
70
|
1 |
|
public function getClass(): string |
71
|
|
|
{ |
72
|
1 |
|
return $this->job['class']; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get job data. |
77
|
|
|
*/ |
78
|
1 |
|
public function getData() |
79
|
|
|
{ |
80
|
1 |
|
return $this->job['data']; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get ID. |
85
|
|
|
*/ |
86
|
33 |
|
public function getId(): ObjectId |
87
|
|
|
{ |
88
|
33 |
|
return $this->job['_id']; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Restart job. |
93
|
|
|
*/ |
94
|
1 |
|
public function getWorker(): ObjectId |
95
|
|
|
{ |
96
|
1 |
|
return $this->job['worker']; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get current job progress |
101
|
|
|
*/ |
102
|
1 |
|
public function getProgress(): float |
103
|
|
|
{ |
104
|
1 |
|
return $this->job['progress'] ?? 0.0; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Wait for job beeing executed. |
109
|
|
|
*/ |
110
|
2 |
|
public function wait(): Process |
111
|
|
|
{ |
112
|
2 |
|
$this->scheduler->waitFor([$this], Scheduler::OPTION_THROW_EXCEPTION); |
113
|
1 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get status. |
118
|
|
|
*/ |
119
|
27 |
|
public function getStatus(): int |
120
|
|
|
{ |
121
|
27 |
|
return $this->job['status']; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|