1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* tubee.io |
7
|
|
|
* |
8
|
|
|
* @copyright Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com) |
9
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tubee; |
13
|
|
|
|
14
|
|
|
use DateTime; |
15
|
|
|
use Generator; |
16
|
|
|
use MongoDB\BSON\ObjectIdInterface; |
17
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
18
|
|
|
use TaskScheduler\JobInterface as TaskJobInterface; |
19
|
|
|
use TaskScheduler\Process; |
|
|
|
|
20
|
|
|
use TaskScheduler\Scheduler; |
21
|
|
|
use Tubee\Job\JobInterface; |
22
|
|
|
use Tubee\Log\Factory as LogFactory; |
23
|
|
|
use Tubee\Log\LogInterface; |
24
|
|
|
use Tubee\Process\Factory as ProcessFactory; |
25
|
|
|
use Tubee\Process\ProcessInterface; |
26
|
|
|
use Tubee\Resource\AbstractResource; |
27
|
|
|
use Tubee\Resource\AttributeResolver; |
28
|
|
|
use Tubee\ResourceNamespace\ResourceNamespaceInterface; |
29
|
|
|
|
30
|
|
|
class Job extends AbstractResource implements JobInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Namespace. |
34
|
|
|
* |
35
|
|
|
* @var ResourceNamespaceInterface |
36
|
|
|
*/ |
37
|
|
|
protected $namespace; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Process factory. |
41
|
|
|
* |
42
|
|
|
* @var ProcessFactory |
43
|
|
|
*/ |
44
|
|
|
protected $process_factory; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Log factory. |
48
|
|
|
* |
49
|
|
|
* @var LogFactory |
50
|
|
|
*/ |
51
|
|
|
protected $log_factory; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Taskscheduler. |
55
|
|
|
* |
56
|
|
|
* @var Scheduler |
57
|
|
|
*/ |
58
|
|
|
protected $scheduler; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Data object. |
62
|
|
|
*/ |
63
|
|
|
public function __construct(array $resource, ResourceNamespaceInterface $namespace, Scheduler $scheduler, ProcessFactory $process_factory, LogFactory $log_factory) |
64
|
|
|
{ |
65
|
|
|
$this->resource = $resource; |
66
|
|
|
$this->namespace = $namespace; |
67
|
|
|
$this->process_factory = $process_factory; |
68
|
|
|
$this->log_factory = $log_factory; |
69
|
|
|
$this->scheduler = $scheduler; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function decorate(ServerRequestInterface $request): array |
76
|
|
|
{ |
77
|
|
|
$resource = $this; |
78
|
|
|
$scheduler = $this->scheduler; |
79
|
|
|
|
80
|
|
|
$result = [ |
81
|
|
|
'_links' => [ |
82
|
|
|
'namespace' => ['href' => (string) $request->getUri()->withPath('/api/v1/namespaces/'.$this->namespace->getName())], |
83
|
|
|
], |
84
|
|
|
'kind' => 'Job', |
85
|
|
|
'namespace' => $this->namespace->getName(), |
86
|
|
|
'data' => $this->getData(), |
87
|
|
|
'status' => function () use ($resource, $scheduler) { |
88
|
|
|
$process = iterator_to_array($scheduler->getJobs([ |
89
|
|
|
'data.job' => $resource->getName(), |
90
|
|
|
'data.parent' => ['$exists' => false], |
91
|
|
|
])); |
92
|
|
|
|
93
|
|
|
$process = end($process); |
94
|
|
|
|
95
|
|
|
if ($process === false) { |
96
|
|
|
return [ |
97
|
|
|
'status' => false, |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$process = $process->toArray(); |
102
|
|
|
|
103
|
|
|
return [ |
104
|
|
|
'status' => true, |
105
|
|
|
'last_process' => [ |
106
|
|
|
'process' => (string) $process['_id'], |
107
|
|
|
'next' => $process['options']['at'] === 0 ? null : (new DateTime('@'.(string) $process['options']['at']))->format('c'), |
108
|
|
|
'started' => $process['status'] === 0 ? null : $process['started']->toDateTime()->format('c'), |
109
|
|
|
'ended' => $process['status'] <= 2 ? null : $process['ended']->toDateTime()->format('c'), |
110
|
|
|
'result' => TaskJobInterface::STATUS_MAP[$process['status']], |
111
|
|
|
'code' => $process['status'], |
112
|
|
|
], |
113
|
|
|
]; |
114
|
|
|
}, |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
return AttributeResolver::resolve($request, $this, $result); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
|
public function getLogs(array $query = [], ?int $offset = null, ?int $limit = null, ?array $sort = []): Generator |
124
|
|
|
{ |
125
|
|
|
$query['context.job'] = (string) $this->getId(); |
126
|
|
|
|
127
|
|
|
return $this->log_factory->getAll($query, $offset, $limit, $sort); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
|
|
public function getResourceNamespace(): ResourceNamespaceInterface |
134
|
|
|
{ |
135
|
|
|
return $this->namespace; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
|
|
public function getLog(ObjectIdInterface $id): LogInterface |
142
|
|
|
{ |
143
|
|
|
return $this->log_factory->getOne($id); |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
|
|
public function getProcesses(array $query = [], ?int $offset = null, ?int $limit = null, array $sort = []): Generator |
150
|
|
|
{ |
151
|
|
|
$query['job'] = $this->getId(); |
152
|
|
|
|
153
|
|
|
return $this->process_factory->getAll($this->namespace, $query, $offset, $limit, $sort); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritdoc} |
158
|
|
|
*/ |
159
|
|
|
public function getProcess(ObjectIdInterface $id): ProcessInterface |
160
|
|
|
{ |
161
|
|
|
return $this->process_factory->getOne($this->namespace, $id); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: