1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package: Chapi |
4
|
|
|
* |
5
|
|
|
* @author: msiebeneicher |
6
|
|
|
* @since: 2015-07-31 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Chapi\BusinessCase\JobManagement; |
11
|
|
|
|
12
|
|
|
use Chapi\BusinessCase\Comparison\JobComparisonInterface; |
13
|
|
|
use Chapi\Entity\Chronos\ChronosJobEntity; |
14
|
|
|
use Chapi\Service\JobDependencies\JobDependencyServiceInterface; |
15
|
|
|
use Chapi\Service\JobIndex\JobIndexServiceInterface; |
16
|
|
|
use Chapi\Service\JobRepository\JobRepositoryInterface; |
17
|
|
|
use Psr\Log\LoggerInterface; |
18
|
|
|
|
19
|
|
|
class ChronosStoreJobBusinessCase extends AbstractStoreJobBusinessCase implements StoreJobBusinessCaseInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var JobDependencyServiceInterface |
24
|
|
|
*/ |
25
|
|
|
private $jobDependencyService; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param JobIndexServiceInterface $jobIndexService |
29
|
|
|
* @param JobRepositoryInterface $jobRepositoryRemote |
30
|
|
|
* @param JobRepositoryInterface $jobRepositoryLocal |
31
|
|
|
* @param JobComparisonInterface $jobComparisonBusinessCase |
32
|
|
|
* @param JobDependencyServiceInterface $jobDependencyService |
33
|
|
|
* @param LoggerInterface $logger |
34
|
|
|
*/ |
35
|
13 |
View Code Duplication |
public function __construct( |
36
|
|
|
JobIndexServiceInterface $jobIndexService, |
37
|
|
|
JobRepositoryInterface $jobRepositoryRemote, |
38
|
|
|
JobRepositoryInterface $jobRepositoryLocal, |
39
|
|
|
JobComparisonInterface $jobComparisonBusinessCase, |
40
|
|
|
JobDependencyServiceInterface $jobDependencyService, |
41
|
|
|
LoggerInterface $logger |
42
|
|
|
) { |
43
|
13 |
|
$this->jobIndexService = $jobIndexService; |
44
|
13 |
|
$this->jobRepositoryRemote = $jobRepositoryRemote; |
45
|
13 |
|
$this->jobRepositoryLocal = $jobRepositoryLocal; |
46
|
13 |
|
$this->jobComparisonBusinessCase = $jobComparisonBusinessCase; |
47
|
13 |
|
$this->jobDependencyService = $jobDependencyService; |
48
|
13 |
|
$this->logger = $logger; |
49
|
13 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
11 |
View Code Duplication |
public function storeIndexedJobs() |
55
|
|
|
{ |
56
|
|
|
// add new jobs to chronos |
57
|
11 |
|
$newJobs = $this->jobComparisonBusinessCase->getRemoteMissingJobs(); |
58
|
11 |
|
foreach ($newJobs as $jobName) { |
59
|
6 |
|
$this->hasAddedJob($jobName); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// delete missing jobs from chronos |
63
|
11 |
|
$missingJobs = $this->jobComparisonBusinessCase->getLocalMissingJobs(); |
64
|
11 |
|
foreach ($missingJobs as $jobName) { |
65
|
5 |
|
$this->hasRemovedJob($jobName); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// update jobs on chronos |
69
|
11 |
|
$localJobUpdates = $this->jobComparisonBusinessCase->getLocalJobUpdates(); |
70
|
11 |
|
foreach ($localJobUpdates as $jobName) { |
71
|
4 |
|
$this->hasUpdatedJob($jobName); |
72
|
|
|
} |
73
|
11 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $jobName |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
6 |
|
private function hasAddedJob($jobName) |
80
|
|
|
{ |
81
|
6 |
|
$jobEntityLocal = $this->jobRepositoryLocal->getJob($jobName); |
82
|
|
|
|
83
|
6 |
|
if (!$jobEntityLocal instanceof ChronosJobEntity) { |
84
|
|
|
throw new \RuntimeException('Expected ChronosJobEntity. Received something else.'); |
85
|
|
|
} |
86
|
|
|
|
87
|
6 |
|
if ($this->isAbleToStoreEntity($jobEntityLocal)) { |
88
|
4 |
|
if ($this->jobRepositoryRemote->addJob($jobEntityLocal)) { |
89
|
3 |
|
$this->jobIndexService->removeJob($jobEntityLocal->getKey()); |
90
|
3 |
|
$this->logger->notice(sprintf( |
91
|
3 |
|
'Job "%s" successfully added to chronos', |
92
|
3 |
|
$jobEntityLocal->getKey() |
93
|
|
|
)); |
94
|
|
|
|
95
|
3 |
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
$this->logger->error(sprintf( |
99
|
1 |
|
'Failed to add job "%s" to chronos', |
100
|
1 |
|
$jobEntityLocal->getKey() |
101
|
|
|
)); |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
return false; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param $jobName |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
5 |
View Code Duplication |
private function hasRemovedJob($jobName) |
112
|
|
|
{ |
113
|
5 |
|
if ($this->isAbleToDeleteJob($jobName)) { |
114
|
3 |
|
if ($this->jobRepositoryRemote->removeJob($jobName)) { |
115
|
3 |
|
$this->jobIndexService->removeJob($jobName); |
116
|
3 |
|
$this->logger->notice(sprintf( |
117
|
3 |
|
'Job "%s" successfully removed from chronos', |
118
|
|
|
$jobName |
119
|
|
|
)); |
120
|
|
|
|
121
|
3 |
|
return true; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$this->logger->error(sprintf( |
125
|
|
|
'Failed to remove job "%s" from chronos', |
126
|
|
|
$jobName |
127
|
|
|
)); |
128
|
|
|
} |
129
|
|
|
|
130
|
2 |
|
return false; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $jobName |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
4 |
|
private function hasUpdatedJob($jobName) |
138
|
|
|
{ |
139
|
4 |
|
$jobEntityLocal = $this->jobRepositoryLocal->getJob($jobName); |
140
|
|
|
|
141
|
4 |
|
if (!$jobEntityLocal instanceof ChronosJobEntity) { |
142
|
|
|
throw new \RuntimeException('Expected ChronosJobEntity. Received something else.'); |
143
|
|
|
} |
144
|
|
|
|
145
|
4 |
|
if ($this->isAbleToStoreEntity($jobEntityLocal)) { |
146
|
3 |
|
$jobEntityChronos = $this->jobRepositoryRemote->getJob($jobName); |
147
|
|
|
|
148
|
|
|
// handle job update |
149
|
3 |
|
if ($this->jobComparisonBusinessCase->hasSameJobType($jobEntityLocal, $jobEntityChronos)) { |
150
|
2 |
|
$hasUpdatedJob = $this->jobRepositoryRemote->updateJob($jobEntityLocal); |
151
|
|
|
} else { |
152
|
|
|
$hasUpdatedJob = ( |
153
|
1 |
|
$this->jobRepositoryRemote->removeJob($jobEntityChronos->getKey()) |
154
|
1 |
|
&& $this->jobRepositoryRemote->addJob($jobEntityLocal) |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// handle update result |
159
|
3 |
|
if ($hasUpdatedJob) { |
160
|
3 |
|
$this->jobIndexService->removeJob($jobEntityLocal->getKey()); |
161
|
3 |
|
$this->logger->notice(sprintf( |
162
|
3 |
|
'Job "%s" successfully updated in chronos', |
163
|
3 |
|
$jobEntityLocal->getKey() |
164
|
|
|
)); |
165
|
|
|
|
166
|
3 |
|
return true; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
// in case of an error |
170
|
|
|
$this->logger->error(sprintf( |
171
|
|
|
'Failed to update job "%s" in chronos', |
172
|
|
|
$jobEntityLocal->getKey() |
173
|
|
|
)); |
174
|
|
|
} |
175
|
|
|
|
176
|
1 |
|
return false; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param ChronosJobEntity $entity |
181
|
|
|
* @return bool |
182
|
|
|
*/ |
183
|
8 |
|
private function isAbleToStoreEntity(ChronosJobEntity $entity) |
184
|
|
|
{ |
185
|
8 |
|
if ($this->jobIndexService->isJobInIndex($entity->getKey())) { |
186
|
7 |
|
if ($entity->isSchedulingJob()) { |
187
|
5 |
|
return true; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
//else :: are all parents available? |
191
|
2 |
|
foreach ($entity->parents as $parentJobName) { |
192
|
2 |
|
if (false === $this->jobRepositoryRemote->hasJob($parentJobName)) { |
193
|
1 |
|
$this->logger->warning(sprintf( |
194
|
1 |
|
'Parent job is not available for "%s" on chronos. Please add parent "%s" first.', |
195
|
1 |
|
$entity->name, |
196
|
|
|
$parentJobName |
197
|
|
|
)); |
198
|
|
|
|
199
|
2 |
|
return false; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
1 |
|
return true; |
204
|
|
|
} |
205
|
|
|
|
206
|
2 |
|
return false; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param string $jobName |
211
|
|
|
* @return bool |
212
|
|
|
*/ |
213
|
5 |
|
private function isAbleToDeleteJob($jobName) |
214
|
|
|
{ |
215
|
5 |
|
if ($this->jobIndexService->isJobInIndex($jobName)) { |
216
|
4 |
|
$childJobs = $this->jobDependencyService->getChildJobs($jobName, JobDependencyServiceInterface::REPOSITORY_CHRONOS); |
217
|
4 |
|
if (empty($childJobs)) { |
218
|
3 |
|
return true; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
// else :: are child also in index to delete? |
222
|
1 |
|
foreach ($childJobs as $childJobName) { |
223
|
1 |
|
if (false === $this->jobIndexService->isJobInIndex($childJobName)) { |
224
|
1 |
|
$this->logger->warning(sprintf( |
225
|
1 |
|
'Child job is still available for "%s" on chronos. Please remove child "%s" first.', |
226
|
|
|
$jobName, |
227
|
|
|
$childJobName |
228
|
|
|
)); |
229
|
|
|
|
230
|
1 |
|
return false; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
// child job are also in index |
235
|
|
|
return true; |
236
|
|
|
} |
237
|
|
|
|
238
|
1 |
|
return false; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|