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\JobEntity; |
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 StoreJobBusinessCase implements StoreJobBusinessCaseInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var JobIndexServiceInterface |
23
|
|
|
*/ |
24
|
|
|
private $oJobIndexService; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var JobRepositoryInterface |
28
|
|
|
*/ |
29
|
|
|
private $oJobRepositoryChronos; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var JobRepositoryInterface |
33
|
|
|
*/ |
34
|
|
|
private $oJobRepositoryLocal; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var JobComparisonInterface |
38
|
|
|
*/ |
39
|
|
|
private $oJobComparisonBusinessCase; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var JobDependencyServiceInterface |
43
|
|
|
*/ |
44
|
|
|
private $oJobDependencyService; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var LoggerInterface |
48
|
|
|
*/ |
49
|
|
|
private $oLogger; |
50
|
|
|
|
51
|
|
|
|
52
|
13 |
|
public function __construct( |
53
|
|
|
JobIndexServiceInterface $oJobIndexService, |
54
|
|
|
JobRepositoryInterface $oJobRepositoryChronos, |
55
|
|
|
JobRepositoryInterface $oJobRepositoryLocal, |
56
|
|
|
JobComparisonInterface $oJobComparisonBusinessCase, |
57
|
|
|
JobDependencyServiceInterface $oJobDependencyService, |
58
|
|
|
LoggerInterface $oLogger |
59
|
|
|
) |
60
|
|
|
{ |
61
|
13 |
|
$this->oJobIndexService = $oJobIndexService; |
62
|
13 |
|
$this->oJobRepositoryChronos = $oJobRepositoryChronos; |
63
|
13 |
|
$this->oJobRepositoryLocal = $oJobRepositoryLocal; |
64
|
13 |
|
$this->oJobComparisonBusinessCase = $oJobComparisonBusinessCase; |
65
|
13 |
|
$this->oJobDependencyService = $oJobDependencyService; |
66
|
13 |
|
$this->oLogger = $oLogger; |
67
|
13 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritdoc |
71
|
|
|
*/ |
72
|
11 |
|
public function storeIndexedJobs() |
73
|
|
|
{ |
74
|
|
|
// add new jobs to chronos |
75
|
11 |
|
$_aNewJobs = $this->oJobComparisonBusinessCase->getChronosMissingJobs(); |
76
|
11 |
|
foreach ($_aNewJobs as $_sJobName) |
77
|
|
|
{ |
78
|
6 |
|
$this->hasAddedJob($_sJobName); |
79
|
11 |
|
} |
80
|
|
|
|
81
|
|
|
// delete missing jobs from chronos |
82
|
11 |
|
$_aMissingJobs = $this->oJobComparisonBusinessCase->getLocalMissingJobs(); |
83
|
11 |
|
foreach ($_aMissingJobs as $_sJobName) |
84
|
|
|
{ |
85
|
5 |
|
$this->hasRemovedJob($_sJobName); |
86
|
11 |
|
} |
87
|
|
|
|
88
|
|
|
// update jobs on chronos |
89
|
11 |
|
$_aLocalJobUpdates = $this->oJobComparisonBusinessCase->getLocalJobUpdates(); |
90
|
11 |
|
foreach ($_aLocalJobUpdates as $_sJobName) |
91
|
|
|
{ |
92
|
4 |
|
$this->hasUpdatedJob($_sJobName); |
93
|
11 |
|
} |
94
|
11 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritdoc |
98
|
|
|
*/ |
99
|
2 |
|
public function storeJobsToLocalRepository(array $aJobNames = [], $bForceOverwrite = false) |
100
|
|
|
{ |
101
|
2 |
|
if (empty($aJobNames)) |
102
|
2 |
|
{ |
103
|
|
|
$_aChronosJobs = $this->oJobRepositoryChronos->getJobs(); |
104
|
|
|
} |
105
|
|
|
else |
106
|
|
|
{ |
107
|
2 |
|
$_aChronosJobs = []; |
108
|
2 |
|
foreach ($aJobNames as $_sJobName) |
109
|
|
|
{ |
110
|
2 |
|
$_aChronosJobs[] = $this->oJobRepositoryChronos->getJob($_sJobName); |
111
|
2 |
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** @var JobEntity $_oJobEntity */ |
115
|
2 |
|
foreach ($_aChronosJobs as $_oJobEntity) |
116
|
|
|
{ |
117
|
2 |
|
$_oJobEntityLocal = $this->oJobRepositoryLocal->getJob($_oJobEntity->name); |
118
|
|
|
// new job |
119
|
2 |
View Code Duplication |
if (empty($_oJobEntityLocal->name)) |
|
|
|
|
120
|
2 |
|
{ |
121
|
1 |
|
if ($this->oJobRepositoryLocal->addJob($_oJobEntity)) |
122
|
1 |
|
{ |
123
|
1 |
|
$this->oLogger->notice(sprintf( |
124
|
1 |
|
'Job "%s" successfully stored in local repository', |
125
|
1 |
|
$_oJobEntity->name |
126
|
1 |
|
)); |
127
|
1 |
|
} |
128
|
|
|
else |
129
|
|
|
{ |
130
|
|
|
$this->oLogger->error(sprintf( |
131
|
|
|
'Failed to store job "%s" in local repository', |
132
|
|
|
$_oJobEntity->name |
133
|
|
|
)); |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
continue; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// update job |
140
|
2 |
|
$_aDiff = $this->oJobComparisonBusinessCase->getJobDiff($_oJobEntity->name); |
141
|
2 |
|
if (!empty($_aDiff)) |
142
|
2 |
|
{ |
143
|
2 |
|
if (!$bForceOverwrite) |
144
|
2 |
|
{ |
145
|
1 |
|
throw new \InvalidArgumentException( |
146
|
1 |
|
sprintf( |
147
|
1 |
|
'The job "%s" already exist in your local repository. Use the "force" option to overwrite the job', |
148
|
1 |
|
$_oJobEntity->name |
149
|
1 |
|
) |
150
|
1 |
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
View Code Duplication |
if ($this->oJobRepositoryLocal->updateJob($_oJobEntity)) |
|
|
|
|
154
|
1 |
|
{ |
155
|
1 |
|
$this->oLogger->notice(sprintf( |
156
|
1 |
|
'Job "%s" successfully updated in local repository', |
157
|
1 |
|
$_oJobEntity->name |
158
|
1 |
|
)); |
159
|
1 |
|
} |
160
|
|
|
else |
161
|
|
|
{ |
162
|
|
|
$this->oLogger->error(sprintf( |
163
|
|
|
'Failed to update job "%s" in local repository', |
164
|
|
|
$_oJobEntity->name |
165
|
|
|
)); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
// remove job from index in case off added in the past |
169
|
1 |
|
$this->oJobIndexService->removeJob($_oJobEntity->name); |
170
|
1 |
|
} |
171
|
1 |
|
} |
172
|
1 |
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param string $sJobName |
176
|
|
|
* @return bool |
177
|
|
|
*/ |
178
|
6 |
|
private function hasAddedJob($sJobName) |
179
|
|
|
{ |
180
|
6 |
|
$_oJobEntityLocal = $this->oJobRepositoryLocal->getJob($sJobName); |
181
|
|
|
|
182
|
6 |
View Code Duplication |
if ($this->isAbleToStoreEntity($_oJobEntityLocal)) |
|
|
|
|
183
|
6 |
|
{ |
184
|
4 |
|
if ($this->oJobRepositoryChronos->addJob($_oJobEntityLocal)) |
185
|
4 |
|
{ |
186
|
3 |
|
$this->oJobIndexService->removeJob($_oJobEntityLocal->name); |
187
|
3 |
|
$this->oLogger->notice(sprintf( |
188
|
3 |
|
'Job "%s" successfully added to chronos', |
189
|
3 |
|
$_oJobEntityLocal->name |
190
|
3 |
|
)); |
191
|
|
|
|
192
|
3 |
|
return true; |
193
|
|
|
} |
194
|
|
|
|
195
|
1 |
|
$this->oLogger->error(sprintf( |
196
|
1 |
|
'Failed to add job "%s" to chronos', |
197
|
1 |
|
$_oJobEntityLocal->name |
198
|
1 |
|
)); |
199
|
1 |
|
} |
200
|
|
|
|
201
|
3 |
|
return false; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param $sJobName |
206
|
|
|
* @return bool |
207
|
|
|
*/ |
208
|
5 |
|
private function hasRemovedJob($sJobName) |
209
|
|
|
{ |
210
|
5 |
View Code Duplication |
if ($this->isAbleToDeleteJob($sJobName)) |
|
|
|
|
211
|
5 |
|
{ |
212
|
3 |
|
if ($this->oJobRepositoryChronos->removeJob($sJobName)) |
213
|
3 |
|
{ |
214
|
3 |
|
$this->oJobIndexService->removeJob($sJobName); |
215
|
3 |
|
$this->oLogger->notice(sprintf( |
216
|
3 |
|
'Job "%s" successfully removed from chronos', |
217
|
|
|
$sJobName |
218
|
3 |
|
)); |
219
|
|
|
|
220
|
3 |
|
return true; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$this->oLogger->error(sprintf( |
224
|
|
|
'Failed to remove job "%s" from chronos', |
225
|
|
|
$sJobName |
226
|
|
|
)); |
227
|
|
|
} |
228
|
|
|
|
229
|
2 |
|
return false; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param string $sJobName |
234
|
|
|
* @return bool |
235
|
|
|
*/ |
236
|
4 |
|
private function hasUpdatedJob($sJobName) |
237
|
|
|
{ |
238
|
4 |
|
$_oJobEntityLocal = $this->oJobRepositoryLocal->getJob($sJobName); |
239
|
|
|
|
240
|
4 |
|
if ($this->isAbleToStoreEntity($_oJobEntityLocal)) |
241
|
4 |
|
{ |
242
|
3 |
|
$_oJobEntityChronos = $this->oJobRepositoryChronos->getJob($sJobName); |
243
|
|
|
|
244
|
|
|
// handle job update |
245
|
3 |
|
if ($this->oJobComparisonBusinessCase->hasSameJobType($_oJobEntityLocal, $_oJobEntityChronos)) |
246
|
3 |
|
{ |
247
|
2 |
|
$_bHasUpdatedJob = $this->oJobRepositoryChronos->updateJob($_oJobEntityLocal); |
248
|
2 |
|
} |
249
|
|
|
else |
250
|
|
|
{ |
251
|
|
|
$_bHasUpdatedJob = ( |
252
|
1 |
|
$this->oJobRepositoryChronos->removeJob($_oJobEntityChronos->name) |
253
|
1 |
|
&& $this->oJobRepositoryChronos->addJob($_oJobEntityLocal) |
254
|
1 |
|
); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
// handle update result |
258
|
|
|
if ($_bHasUpdatedJob) |
259
|
3 |
|
{ |
260
|
3 |
|
$this->oJobIndexService->removeJob($_oJobEntityLocal->name); |
261
|
3 |
|
$this->oLogger->notice(sprintf( |
262
|
3 |
|
'Job "%s" successfully updated in chronos', |
263
|
3 |
|
$_oJobEntityLocal->name |
264
|
3 |
|
)); |
265
|
|
|
|
266
|
3 |
|
return true; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
// in case of an error |
270
|
|
|
$this->oLogger->error(sprintf( |
271
|
|
|
'Failed to update job "%s" in chronos', |
272
|
|
|
$_oJobEntityLocal->name |
273
|
|
|
)); |
274
|
|
|
} |
275
|
|
|
|
276
|
1 |
|
return false; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param JobEntity $oEntity |
281
|
|
|
* @return bool |
282
|
|
|
*/ |
283
|
8 |
|
private function isAbleToStoreEntity(JobEntity $oEntity) |
284
|
|
|
{ |
285
|
8 |
|
if ($this->oJobIndexService->isJobInIndex($oEntity->name)) |
286
|
8 |
|
{ |
287
|
7 |
|
if ($oEntity->isSchedulingJob()) |
288
|
7 |
|
{ |
289
|
5 |
|
return true; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
//else :: are all parents available? |
293
|
2 |
|
foreach ($oEntity->parents as $_sParentJobName) |
294
|
|
|
{ |
295
|
2 |
|
if (false === $this->oJobRepositoryChronos->hasJob($_sParentJobName)) |
296
|
2 |
|
{ |
297
|
1 |
|
$this->oLogger->warning(sprintf( |
298
|
1 |
|
'Parent job is not available for "%s" on chronos. Please add parent "%s" first.', |
299
|
1 |
|
$oEntity->name, |
300
|
|
|
$_sParentJobName |
301
|
1 |
|
)); |
302
|
|
|
|
303
|
1 |
|
return false; |
304
|
|
|
} |
305
|
1 |
|
} |
306
|
|
|
|
307
|
1 |
|
return true; |
308
|
|
|
} |
309
|
|
|
|
310
|
2 |
|
return false; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @param string $sJobName |
315
|
|
|
* @return bool |
316
|
|
|
*/ |
317
|
5 |
|
private function isAbleToDeleteJob($sJobName) |
318
|
|
|
{ |
319
|
5 |
|
if ($this->oJobIndexService->isJobInIndex($sJobName)) |
320
|
5 |
|
{ |
321
|
4 |
|
$_aChildJobs = $this->oJobDependencyService->getChildJobs($sJobName, JobDependencyServiceInterface::REPOSITORY_CHRONOS); |
322
|
4 |
|
if (empty($_aChildJobs)) |
323
|
4 |
|
{ |
324
|
3 |
|
return true; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
// else :: are child also in index to delete? |
328
|
1 |
|
foreach ($_aChildJobs as $_sChildJobName) |
329
|
|
|
{ |
330
|
1 |
|
if (false === $this->oJobIndexService->isJobInIndex($_sChildJobName)) |
331
|
1 |
|
{ |
332
|
1 |
|
$this->oLogger->warning(sprintf( |
333
|
1 |
|
'Child job is still available for "%s" on chronos. Please remove child "%s" first.', |
334
|
1 |
|
$sJobName, |
335
|
|
|
$_sChildJobName |
336
|
1 |
|
)); |
337
|
|
|
|
338
|
1 |
|
return false; |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
// child job are also in index |
343
|
|
|
return true; |
344
|
|
|
} |
345
|
|
|
|
346
|
1 |
|
return false; |
347
|
|
|
} |
348
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.