Issues (2551)

src/Impl/Batch/BatchEntity.php (8 issues)

1
<?php
2
3
namespace Jabe\Impl\Batch;
4
5
use Jabe\Batch\BatchInterface;
6
use Jabe\Impl\Context\Context;
7
use Jabe\Impl\Db\{
8
    DbEntityInterface,
9
    HasDbReferencesInterface,
10
    HasDbRevisionInterface
11
};
12
use Jabe\Impl\Interceptor\CommandContext;
13
use Jabe\Impl\Persistence\Entity\{
14
    HistoricIncidentManager,
15
    HistoricJobLogManager,
16
    JobDefinitionEntity,
17
    JobDefinitionManager,
18
    JobEntity,
19
    NameableInterface,
20
    SuspensionState,
21
    VariableInstanceEntity,
22
    VariableInstanceManager
23
};
24
use Jabe\Impl\Persistence\Entity\Util\ByteArrayField;
25
use Jabe\Impl\Util\ClockUtil;
26
use Jabe\Repository\ResourceTypes;
27
28
class BatchEntity implements BatchInterface, DbEntityInterface, HasDbReferencesInterface, NameableInterface, HasDbRevisionInterface
29
{
30
    public static $BATCH_SEED_JOB_DECLARATION;
31
    public static $BATCH_MONITOR_JOB_DECLARATION;
32
33
    // persistent
34
    protected $id;
35
    protected $type;
36
37
    protected $totalJobs;
38
    protected $jobsCreated;
39
    protected $batchJobsPerSeed;
40
    protected $invocationsPerBatchJob;
41
42
    protected $seedJobDefinitionId;
43
    protected $monitorJobDefinitionId;
44
    protected $batchJobDefinitionId;
45
46
    protected $configuration;
47
48
    protected $tenantId;
49
    protected $createUserId;
50
51
    protected $suspensionState;
52
53
    protected $revision;
54
55
    // transient
56
    protected $seedJobDefinition;
57
    protected $monitorJobDefinition;
58
    protected $batchJobDefinition;
59
60
    protected $batchJobHandler;
61
62
    public function __construct()
63
    {
64
        if (self::$BATCH_SEED_JOB_DECLARATION == null) {
65
            self::$BATCH_SEED_JOB_DECLARATION = new BatchSeedJobDeclaration();
66
        }
67
        if (self::$BATCH_MONITOR_JOB_DECLARATION == null) {
68
            self::$BATCH_MONITOR_JOB_DECLARATION = new BatchMonitorJobDeclaration();
69
        }
70
        $this->configuration = new ByteArrayField($this, ResourceTypes::runtime());
71
        $this->suspensionState = SuspensionState::active()->getStateCode();
72
    }
73
74
    public function getId(): string
75
    {
76
        return $this->id;
77
    }
78
79
    public function setId(string $id): void
80
    {
81
        $this->id = $id;
82
    }
83
84
    public function getName(): string
85
    {
86
        return $this->id;
87
    }
88
89
    public function getType(): string
90
    {
91
        return $this->type;
92
    }
93
94
    public function setType(string $type): void
95
    {
96
        $this->type = $type;
97
    }
98
99
    public function getTotalJobs(): int
100
    {
101
        return $this->totalJobs;
102
    }
103
104
    public function setTotalJobs(int $totalJobs): void
105
    {
106
        $this->totalJobs = $totalJobs;
107
    }
108
109
    public function getJobsCreated(): int
110
    {
111
        return $this->jobsCreated;
112
    }
113
114
    public function setJobsCreated(int $jobsCreated): void
115
    {
116
        $this->jobsCreated = $jobsCreated;
117
    }
118
119
    public function getBatchJobsPerSeed(): int
120
    {
121
        return $this->batchJobsPerSeed;
122
    }
123
124
    public function setBatchJobsPerSeed(int $batchJobsPerSeed): void
125
    {
126
        $this->batchJobsPerSeed = $batchJobsPerSeed;
127
    }
128
129
    public function getInvocationsPerBatchJob(): int
130
    {
131
        return $this->invocationsPerBatchJob;
132
    }
133
134
    public function setInvocationsPerBatchJob(int $invocationsPerBatchJob): void
135
    {
136
        $this->invocationsPerBatchJob = $invocationsPerBatchJob;
137
    }
138
139
    public function getSeedJobDefinitionId(): string
140
    {
141
        return $this->seedJobDefinitionId;
142
    }
143
144
    public function setSeedJobDefinitionId(string $seedJobDefinitionId): void
145
    {
146
        $this->seedJobDefinitionId = $seedJobDefinitionId;
147
    }
148
149
    public function getMonitorJobDefinitionId(): string
150
    {
151
        return $this->monitorJobDefinitionId;
152
    }
153
154
    public function setMonitorJobDefinitionId(string $monitorJobDefinitionId): void
155
    {
156
        $this->monitorJobDefinitionId = $monitorJobDefinitionId;
157
    }
158
159
    public function getBatchJobDefinitionId(): string
160
    {
161
        return $this->batchJobDefinitionId;
162
    }
163
164
    public function setBatchJobDefinitionId(string $batchJobDefinitionId): void
165
    {
166
        $this->batchJobDefinitionId = $batchJobDefinitionId;
167
    }
168
169
    public function getTenantId(): ?string
170
    {
171
        return $this->tenantId;
172
    }
173
174
    public function setTenantId(?string $tenantId): void
175
    {
176
        $this->tenantId = $tenantId;
177
    }
178
179
    public function getCreateUserId(): string
180
    {
181
        return $this->createUserId;
182
    }
183
184
    public function setCreateUserId(string $createUserId): void
185
    {
186
        $this->createUserId = $createUserId;
187
    }
188
189
    public function getConfiguration(): string
190
    {
191
        return $this->configuration->getByteArrayId();
192
    }
193
194
    public function setConfiguration(string $configuration): void
195
    {
196
        $this->configuration->setByteArrayId($configuration);
197
    }
198
199
    public function setSuspensionState(int $state): void
200
    {
201
        $this->suspensionState = $state;
202
    }
203
204
    public function getSuspensionState(): int
205
    {
206
        return $this->suspensionState;
207
    }
208
209
    public function isSuspended(): bool
210
    {
211
        return $this->suspensionState == SuspensionState::suspended()->getStateCode();
212
    }
213
214
    public function setRevision(int $revision): void
215
    {
216
        $this->revision = $revision;
217
    }
218
219
    public function getRevision(): int
220
    {
221
        return $this->revision;
222
    }
223
224
    public function getRevisionNext(): int
225
    {
226
        return $this->revision + 1;
227
    }
228
229
    // transient
230
231
    public function getSeedJobDefinition(): JobDefinitionEntity
232
    {
233
        if ($this->seedJobDefinition == null && $this->seedJobDefinitionId !== null) {
234
            $this->seedJobDefinition = Context::getCommandContext()->getJobDefinitionManager()->findById($this->seedJobDefinitionId);
235
        }
236
        return $this->seedJobDefinition;
237
    }
238
239
    public function getMonitorJobDefinition(): JobDefinitionEntity
240
    {
241
        if ($this->monitorJobDefinition == null && $this->monitorJobDefinitionId !== null) {
242
            $this->monitorJobDefinition = Context::getCommandContext()->getJobDefinitionManager()->findById($this->monitorJobDefinitionId);
243
        }
244
        return $this->monitorJobDefinition;
245
    }
246
247
    public function getBatchJobDefinition(): JobDefinitionEntity
248
    {
249
        if ($this->batchJobDefinition == null && $this->batchJobDefinitionId !== null) {
250
            $this->batchJobDefinition = Context::getCommandContext()->getJobDefinitionManager()->findById($this->batchJobDefinitionId);
251
        }
252
253
        return $this->batchJobDefinition;
254
    }
255
256
    public function getConfigurationBytes(): string
257
    {
258
        return $this->configuration->getByteArrayValue();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->configuration->getByteArrayValue() could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
259
    }
260
261
    public function setConfigurationBytes(string $configuration): void
262
    {
263
        $this->configuration->setByteArrayValue($configuration);
264
    }
265
266
    public function getBatchJobHandler(): BatchJobHandlerInterface
267
    {
268
        if ($this->batchJobHandler == null) {
269
            $this->batchJobHandler = Context::getCommandContext()->getProcessEngineConfiguration()->getBatchHandlers()->get($this->type);
0 ignored issues
show
The method getBatchHandlers() does not exist on Jabe\Impl\Cfg\ProcessEngineConfigurationImpl. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

269
            $this->batchJobHandler = Context::getCommandContext()->getProcessEngineConfiguration()->/** @scrutinizer ignore-call */ getBatchHandlers()->get($this->type);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
270
        }
271
        return $this->batchJobHandler;
272
    }
273
274
    public function getPersistentState()
275
    {
276
        $persistentState = [];
277
        $persistentState["jobsCreated"] = $this->jobsCreated;
278
        return $persistentState;
279
    }
280
281
    public function createSeedJobDefinition(string $deploymentId): JobDefinitionEntity
0 ignored issues
show
The parameter $deploymentId is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

281
    public function createSeedJobDefinition(/** @scrutinizer ignore-unused */ string $deploymentId): JobDefinitionEntity

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
282
    {
283
        $this->seedJobDefinition = new JobDefinitionEntity(self::$BATCH_SEED_JOB_DECLARATION);
284
        $this->seedJobDefinition->setJobConfiguration($this->id);
285
        $this->seedJobDefinition->setTenantId($this->tenantId);
286
        $this->seedJobDefinition->setDeploymentId($this->deploymentId);
0 ignored issues
show
Bug Best Practice introduced by
The property deploymentId does not exist on Jabe\Impl\Batch\BatchEntity. Did you maybe forget to declare it?
Loading history...
287
288
        Context::getCommandContext()->getJobDefinitionManager()->insert($this->seedJobDefinition);
289
290
        $this->seedJobDefinitionId = $this->seedJobDefinition->getId();
291
292
        return $this->seedJobDefinition;
293
    }
294
295
    public function createMonitorJobDefinition(): JobDefinitionEntity
296
    {
297
        $this->monitorJobDefinition = new JobDefinitionEntity(self::$BATCH_MONITOR_JOB_DECLARATION);
298
        $this->monitorJobDefinition->setJobConfiguration($this->id);
299
        $this->monitorJobDefinition->setTenantId($this->tenantId);
300
301
        Context::getCommandContext()->getJobDefinitionManager()->insert($this->monitorJobDefinition);
302
303
        $this->monitorJobDefinitionId = $this->monitorJobDefinition->getId();
304
305
        return $this->monitorJobDefinition;
306
    }
307
308
    public function createBatchJobDefinition(): JobDefinitionEntity
309
    {
310
        $this->batchJobDefinition = new JobDefinitionEntity($this->getBatchJobHandler()->getJobDeclaration());
311
        $this->batchJobDefinition->setJobConfiguration($this->id);
312
        $this->batchJobDefinition->setTenantId($this->tenantId);
313
314
        Context::getCommandContext()->getJobDefinitionManager()->insert($this->batchJobDefinition);
315
316
        $this->batchJobDefinitionId = $this->batchJobDefinition->getId();
317
318
        return $this->batchJobDefinition;
319
    }
320
321
    public function createSeedJob(): JobEntity
322
    {
323
        $seedJob = self::$BATCH_SEED_JOB_DECLARATION->createJobInstance($this);
324
325
        Context::getCommandContext()->getJobManager()->insertAndHintJobExecutor($seedJob);
326
327
        return $seedJob;
328
    }
329
330
    public function deleteSeedJob(): void
331
    {
332
        $seedJobs = Context::getCommandContext()
333
            ->getJobManager()
334
            ->findJobsByJobDefinitionId($this->seedJobDefinitionId);
335
336
        foreach ($seedJobs as $job) {
337
            $job->delete();
338
        }
339
    }
340
341
    public function createMonitorJob(bool $setDueDate): JobEntity
342
    {
343
        // Maybe use an other job declaration
344
        $monitorJob = self::$BATCH_MONITOR_JOB_DECLARATION->createJobInstance($this);
345
        if ($setDueDate) {
346
            $monitorJob->setDuedate($this->calculateMonitorJobDueDate());
347
        }
348
349
        Context::getCommandContext()
350
            ->getJobManager()->insertAndHintJobExecutor($monitorJob);
351
352
        return $monitorJob;
353
    }
354
355
    protected function calculateMonitorJobDueDate(): string
356
    {
357
        $pollTime = Context::getCommandContext()
358
            ->getProcessEngineConfiguration()
359
            ->getBatchPollTime();
0 ignored issues
show
The method getBatchPollTime() does not exist on Jabe\Impl\Cfg\ProcessEngineConfigurationImpl. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

359
            ->/** @scrutinizer ignore-call */ getBatchPollTime();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
360
        $dueTime = ClockUtil::getCurrentTime()->getTimestamp() + $pollTime;
361
        return (new \DateTime())->setTimestamp($dueTime)->format('c');
362
    }
363
364
    public function deleteMonitorJob(): void
365
    {
366
        $monitorJobs = Context::getCommandContext()
367
            ->getJobManager()
368
            ->findJobsByJobDefinitionId($this->monitorJobDefinitionId);
369
370
        foreach ($monitorJobs as $monitorJob) {
371
            $monitorJob->delete();
372
        }
373
    }
374
375
    public function delete(bool $cascadeToHistory, bool $deleteJobs): void
376
    {
377
        $commandContext = Context::getCommandContext();
378
379
        if (
380
            BatchInterface::TYPE_SET_VARIABLES == $this->type ||
381
            BatchInterface::TYPE_PROCESS_INSTANCE_MIGRATION == $this->type ||
382
            BatchInterface::TYPE_CORRELATE_MESSAGE == $this->type
0 ignored issues
show
The constant Jabe\Batch\BatchInterface::TYPE_CORRELATE_MESSAGE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
383
        ) {
384
            $this->deleteVariables($commandContext);
0 ignored issues
show
It seems like $commandContext can also be of type null; however, parameter $commandContext of Jabe\Impl\Batch\BatchEntity::deleteVariables() does only seem to accept Jabe\Impl\Interceptor\CommandContext, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

384
            $this->deleteVariables(/** @scrutinizer ignore-type */ $commandContext);
Loading history...
385
        }
386
387
        $this->deleteSeedJob();
388
        $this->deleteMonitorJob();
389
        if ($deleteJobs) {
390
            $this->getBatchJobHandler()->deleteJobs($this);
391
        }
392
393
        $jobDefinitionManager = $commandContext->getJobDefinitionManager();
394
        $jobDefinitionManager->delete($this->getSeedJobDefinition());
395
        $jobDefinitionManager->delete($this->getMonitorJobDefinition());
396
        $jobDefinitionManager->delete($this->getBatchJobDefinition());
397
398
        $commandContext->getBatchManager()->delete($this);
399
        $this->configuration->deleteByteArrayValue();
400
401
        $this->fireHistoricEndEvent();
402
403
        if ($cascadeToHistory) {
404
            $historicIncidentManager = $commandContext->getHistoricIncidentManager();
405
            $historicIncidentManager->deleteHistoricIncidentsByJobDefinitionId($this->seedJobDefinitionId);
406
            $historicIncidentManager->deleteHistoricIncidentsByJobDefinitionId($this->monitorJobDefinitionId);
407
            $historicIncidentManager->deleteHistoricIncidentsByJobDefinitionId($this->batchJobDefinitionId);
408
409
            $historicJobLogManager = $commandContext->getHistoricJobLogManager();
410
            $historicJobLogManager->deleteHistoricJobLogsByJobDefinitionId($this->seedJobDefinitionId);
411
            $historicJobLogManager->deleteHistoricJobLogsByJobDefinitionId($this->monitorJobDefinitionId);
412
            $historicJobLogManager->deleteHistoricJobLogsByJobDefinitionId($this->batchJobDefinitionId);
413
414
            $commandContext->getHistoricBatchManager()->deleteHistoricBatchById($this->id);
415
        }
416
    }
417
418
    protected function deleteVariables(CommandContext $commandContext): void
419
    {
420
        $variableInstanceManager = $commandContext->getVariableInstanceManager();
421
422
        $variableInstances =
423
            $variableInstanceManager->findVariableInstancesByBatchId($this->id);
424
425
        foreach ($variableInstances as $variable) {
426
            $variable->delete();
427
        }
428
    }
429
430
    public function fireHistoricStartEvent(): void
431
    {
432
        Context::getCommandContext()
433
            ->getHistoricBatchManager()
434
            ->createHistoricBatch($this);
435
    }
436
437
    public function fireHistoricEndEvent(): void
438
    {
439
        Context::getCommandContext()
440
            ->getHistoricBatchManager()
441
            ->completeHistoricBatch($this);
442
    }
443
444
    public function isCompleted(): bool
445
    {
446
        return Context::getCommandContext()->getProcessEngineConfiguration()
447
            ->getManagementService()
0 ignored issues
show
The method getManagementService() does not exist on Jabe\Impl\Cfg\ProcessEngineConfigurationImpl. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

447
            ->/** @scrutinizer ignore-call */ getManagementService()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
448
            ->createJobQuery()
449
            ->jobDefinitionId($this->batchJobDefinitionId)
450
            ->count() == 0;
451
    }
452
453
    public function __toString()
454
    {
455
        return "BatchEntity{" .
456
            "batchHandler=" . $this->batchJobHandler .
457
            ", id='" . $this->id . '\'' .
458
            ", type='" . $this->type . '\'' .
459
            ", size=" . $this->totalJobs .
460
            ", jobCreated=" . $this->jobsCreated .
461
            ", batchJobsPerSeed=" . $this->batchJobsPerSeed .
462
            ", invocationsPerBatchJob=" . $this->invocationsPerBatchJob .
463
            ", seedJobDefinitionId='" . $this->seedJobDefinitionId . '\'' .
464
            ", monitorJobDefinitionId='" . $this->seedJobDefinitionId . '\'' .
465
            ", batchJobDefinitionId='" . $this->batchJobDefinitionId . '\'' .
466
            ", configurationId='" . $this->configuration->getByteArrayId() . '\'' .
467
            '}';
468
    }
469
470
    public function getReferencedEntityIds(): array
471
    {
472
        $referencedEntityIds = [];
473
        return $referencedEntityIds;
474
    }
475
476
    public function getReferencedEntitiesIdAndClass(): array
477
    {
478
        $referenceIdAndClass = [];
479
480
        if ($this->seedJobDefinitionId !== null) {
481
            $referenceIdAndClass[$this->seedJobDefinitionId] = JobDefinitionEntity::class;
482
        }
483
        if ($this->batchJobDefinitionId !== null) {
484
            $referenceIdAndClass[$this->batchJobDefinitionId] = JobDefinitionEntity::class;
485
        }
486
        if ($this->monitorJobDefinitionId !== null) {
487
            $referenceIdAndClass[$this->monitorJobDefinitionId] = JobDefinitionEntity::class;
488
        }
489
490
        return $referenceIdAndClass;
491
    }
492
}
493