JobEntity::getSuspensionState()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Jabe\Impl\Persistence\Entity;
4
5
use Jabe\Impl\Util\{
6
    EnsureUtil,
7
    ExceptionUtil,
8
    StringUtil
9
};
10
use Jabe\Impl\ProcessEngineLogger;
11
use Jabe\Impl\Cfg\ProcessEngineConfigurationImpl;
12
use Jabe\Impl\Context\Context;
13
use Jabe\Impl\Db\{
14
    DbEntityInterface,
15
    DbEntityLifecycleAwareInterface,
16
    EnginePersistenceLogger,
17
    HasDbReferencesInterface,
18
    HasDbRevisionInterface
19
};
20
use Jabe\Impl\Incident\{
21
    IncidentContext,
22
    IncidentHandling
23
};
24
use Jabe\Impl\Interceptor\CommandContext;
25
use Jabe\Impl\JobExecutor\{
26
    DefaultJobPriorityProvider,
27
    JobHandlerInterface,
28
    JobHandlerConfigurationInterface
29
};
30
use Jabe\Management\JobDefinitionInterface;
31
use Jabe\Repository\ResourceTypes;
32
use Jabe\Runtime\{
33
    IncidentInterface,
34
    JobInterface
35
};
36
use Jabe\Impl\Util\ClassNameUtil;
37
38
abstract class JobEntity extends AcquirableJobEntity implements \Serializable, HasDbReferencesInterface, JobInterface, DbEntityInterface, HasDbRevisionInterface, DbEntityLifecycleAwareInterface
39
{
40
    //private final static EnginePersistenceLogger LOG = ProcessEngineLogger.PERSISTENCE_LOGGER;
41
42
    public const DEFAULT_RETRIES = 3;
43
44
    protected $executionId = null;
45
46
    protected $processDefinitionId = null;
47
    protected $processDefinitionKey = null;
48
49
    protected $retries = self::DEFAULT_RETRIES;
50
51
    // entity is active by default
52
    protected $suspensionState = 1;
53
54
    protected $jobHandlerType = null;
55
    protected $jobHandlerConfiguration = null;
56
57
    protected $exceptionByteArray;
58
    protected $exceptionByteArrayId;
59
60
    protected $exceptionMessage;
61
62
    protected $deploymentId;
63
64
    protected $jobDefinitionId;
65
66
    protected $priority = DefaultJobPriorityProvider::DEFAULT_PRIORITY;
67
68
    protected $tenantId;
69
70
    protected $createTime;
71
72
    // runtime state /////////////////////////////
73
    protected $activityId;
74
    protected $jobDefinition;
75
    protected $execution;
76
77
    // sequence counter //////////////////////////
78
    protected $sequenceCounter = 1;
79
80
    // last failure log id ///////////////////////
81
    protected $lastFailureLogId;
82
83
    // last failing activity id ///////////////////////
84
    protected $failedActivityId;
85
86
    protected $persistedDependentEntities;
87
88
    public function execute(CommandContext $commandContext)
89
    {
90
        $execution = null;
91
        if ($this->executionId !== null) {
92
            $execution = $this->getExecution();
93
            EnsureUtil::ensureNotNull("Cannot find execution with id '" . $this->executionId . "' referenced from job '" . $this . "'", "execution", $execution);
94
        }
95
96
        // initialize activity id
97
        $this->getActivityId();
98
99
        // increment sequence counter before job execution
100
        $this->incrementSequenceCounter();
101
102
        $this->preExecute($commandContext);
103
        $jobHandler = $this->getJobHandler();
104
        $configuration = $this->getJobHandlerConfiguration();
105
        EnsureUtil::ensureNotNull("Cannot find job handler '" . $this->jobHandlerType . "' from job '" . $this . "'", "jobHandler", $jobHandler);
106
        $jobHandler->execute($configuration, $execution, $commandContext, $this->tenantId);
0 ignored issues
show
Bug introduced by
It seems like $configuration can also be of type null; however, parameter $configuration of Jabe\Impl\JobExecutor\Jo...lerInterface::execute() does only seem to accept Jabe\Impl\JobExecutor\Jo...rConfigurationInterface, 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

106
        $jobHandler->execute(/** @scrutinizer ignore-type */ $configuration, $execution, $commandContext, $this->tenantId);
Loading history...
Bug introduced by
It seems like $execution can also be of type null; however, parameter $execution of Jabe\Impl\JobExecutor\Jo...lerInterface::execute() does only seem to accept Jabe\Impl\Persistence\Entity\ExecutionEntity, 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

106
        $jobHandler->execute($configuration, /** @scrutinizer ignore-type */ $execution, $commandContext, $this->tenantId);
Loading history...
107
        $this->postExecute($commandContext);
108
    }
109
110
    protected function preExecute(CommandContext $commandContext): void
111
    {
112
        // nothing to do
113
    }
114
115
    protected function postExecute(CommandContext $commandContext): void
116
    {
117
        //LOG.debugJobExecuted(this);
118
        $this->delete(true);
119
        $commandContext->getHistoricJobLogManager()->fireJobSuccessfulEvent($this);
120
    }
121
122
    public function init(CommandContext $commandContext): void
123
    {
124
        // nothing to do
125
    }
126
127
    public function insert(): void
128
    {
129
        $commandContext = Context::getCommandContext();
130
131
        // add link to execution and deployment
132
        $execution = $this->getExecution();
133
        if ($execution !== null) {
134
            $execution->addJob($this);
135
136
            $processDefinition = $execution->getProcessDefinition();
137
            $this->deploymentId = $processDefinition->getDeploymentId();
138
        }
139
140
        $commandContext
141
        ->getJobManager()
142
        ->insertJob($this);
143
    }
144
145
    public function delete(?bool $incidentResolved = false): void
146
    {
147
        $commandContext = Context::getCommandContext();
148
149
        $this->incrementSequenceCounter();
150
151
        // clean additional data related to this job
152
        $jobHandler = $this->getJobHandler();
153
        if ($jobHandler !== null) {
154
            $jobHandler->onDelete($this->getJobHandlerConfiguration(), $this);
0 ignored issues
show
Bug introduced by
It seems like $this->getJobHandlerConfiguration() can also be of type null; however, parameter $configuration of Jabe\Impl\JobExecutor\Jo...erInterface::onDelete() does only seem to accept Jabe\Impl\JobExecutor\Jo...rConfigurationInterface, 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

154
            $jobHandler->onDelete(/** @scrutinizer ignore-type */ $this->getJobHandlerConfiguration(), $this);
Loading history...
155
        }
156
157
        // fire delete event if this job is not being executed
158
        $executingJob = $this->equals($commandContext->getCurrentJob());
159
        $commandContext->getJobManager()->deleteJob($this, !$executingJob);
160
161
        // Also delete the job's exception byte array
162
        if ($this->exceptionByteArrayId !== null) {
163
            $commandContext->getByteArrayManager()->deleteByteArrayById($this->exceptionByteArrayId);
164
        }
165
166
        // remove link to execution
167
        $execution = $this->getExecution();
168
        if ($execution !== null) {
169
            $execution->removeJob($this);
170
        }
171
172
        $this->removeFailedJobIncident($incidentResolved);
0 ignored issues
show
Bug introduced by
It seems like $incidentResolved can also be of type null; however, parameter $incidentResolved of Jabe\Impl\Persistence\En...moveFailedJobIncident() does only seem to accept boolean, 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

172
        $this->removeFailedJobIncident(/** @scrutinizer ignore-type */ $incidentResolved);
Loading history...
173
    }
174
175
    public function getPersistentState()
176
    {
177
        $persistentState = parent::getPersistentState();
178
        $persistentState["executionId"] = $this->executionId;
179
        $persistentState["retries"] = $this->retries;
180
        $persistentState["exceptionMessage"] = $this->exceptionMessage;
181
        $persistentState["suspensionState"] = $this->suspensionState;
182
        $persistentState["processDefinitionId"] = $this->processDefinitionId;
183
        $persistentState["jobDefinitionId"] = $this->jobDefinitionId;
184
        $persistentState["deploymentId"] = $this->deploymentId;
185
        $persistentState["jobHandlerConfiguration"] = $this->jobHandlerConfiguration;
186
        $persistentState["priority"] = $this->priority;
187
        $persistentState["tenantId"] = $this->tenantId;
188
        if ($this->exceptionByteArrayId !== null) {
189
            $persistentState["exceptionByteArrayId"] = $this->exceptionByteArrayId;
190
        }
191
        return $persistentState;
192
    }
193
194
    public function setExecution(ExecutionEntity $execution): void
195
    {
196
        if ($execution !== null) {
197
            $this->execution = $execution;
198
            $this->executionId = $execution->getId();
199
            $this->processInstanceId = $execution->getProcessInstanceId();
200
            $this->execution->addJob($this);
201
        } else {
202
            $this->execution->removeJob($this);
203
            $this->execution = $execution;
204
            $this->processInstanceId = null;
205
            $this->executionId = null;
206
        }
207
    }
208
209
    // sequence counter /////////////////////////////////////////////////////////
210
211
    public function getSequenceCounter(): int
212
    {
213
        return $this->sequenceCounter;
214
    }
215
216
    public function setSequenceCounter(int $sequenceCounter): void
217
    {
218
        $this->sequenceCounter = $sequenceCounter;
219
    }
220
221
    public function incrementSequenceCounter(): void
222
    {
223
        $this->sequenceCounter += 1;
224
    }
225
226
    // getters and setters //////////////////////////////////////////////////////
227
228
    public function getExecutionId(): string
229
    {
230
        return $this->executionId;
231
    }
232
233
    public function setExecutionId(string $executionId): void
234
    {
235
        $this->executionId = $executionId;
236
    }
237
238
    public function getExecution(): ?ExecutionEntity
239
    {
240
        $this->ensureExecutionInitialized();
241
        return $this->execution;
242
    }
243
244
    protected function ensureExecutionInitialized(): void
245
    {
246
        if ($this->execution === null && $this->executionId !== null) {
247
            $this->execution = Context::getCommandContext()
248
                ->getExecutionManager()
249
                ->findExecutionById($this->executionId);
250
        }
251
    }
252
253
    public function getRetries(): int
254
    {
255
        return $this->retries;
256
    }
257
258
    public function setRetries(int $retries): void
259
    {
260
        // if retries should be set to a negative value set it to 0
261
        if ($retries < 0) {
262
            $retries = 0;
263
        }
264
265
        // Assuming: if the number of retries will
266
        // be changed from 0 to x (x >= 1), means
267
        // that the corresponding incident is resolved.
268
        if ($this->retries == 0 && $retries > 0) {
269
            $this->removeFailedJobIncident(true);
270
        }
271
272
        // If the retries will be set to 0, an
273
        // incident has to be created.
274
        if ($retries == 0 && $this->retries > 0) {
275
            $this->createFailedJobIncident();
276
        }
277
        $this->retries = $retries;
278
    }
279
280
    // special setter for MyBatis which does not influence incidents
281
    public function setRetriesFromPersistence(int $retries): void
282
    {
283
        $this->retries = $retries;
284
    }
285
286
    protected function createFailedJobIncident(): void
287
    {
288
        $processEngineConfiguration = Context::getProcessEngineConfiguration();
289
290
        if ($processEngineConfiguration->isCreateIncidentOnFailedJobEnabled()) {
291
            $incidentHandlerType = IncidentInterface::FAILED_JOB_HANDLER_TYPE;
292
293
            // make sure job has an ID set:
294
            if ($this->id === null) {
295
                $this->id = $processEngineConfiguration
296
                    ->getIdGenerator()
0 ignored issues
show
Bug introduced by
The method getIdGenerator() 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

296
                    ->/** @scrutinizer ignore-call */ getIdGenerator()

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...
297
                    ->getNextId();
298
            } else {
299
                // check whether there exists already an incident
300
                // for this job
301
                $failedJobIncidents = Context::getCommandContext()
302
                    ->getIncidentManager()
303
                    ->findIncidentByConfigurationAndIncidentType($this->id, $incidentHandlerType);
304
305
                if (!empty($failedJobIncidents)) {
306
                    // update the historic job log id in the historic incidents (if available)
307
                    foreach ($failedJobIncidents as $incident) {
308
                        $historicIncidentEvent = Context::getCommandContext()
309
                            ->getHistoricIncidentManager()
310
                            ->findHistoricIncidentById($incident->getId());
311
                        if ($historicIncidentEvent !== null) {
312
                            $historicIncidentEvent->setHistoryConfiguration($this->getLastFailureLogId());
0 ignored issues
show
Bug introduced by
It seems like $this->getLastFailureLogId() can also be of type null; however, parameter $historyConfiguration of Jabe\Impl\History\Event\...tHistoryConfiguration() does only seem to accept string, 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

312
                            $historicIncidentEvent->setHistoryConfiguration(/** @scrutinizer ignore-type */ $this->getLastFailureLogId());
Loading history...
313
                            Context::getCommandContext()->getDbEntityManager()->merge($historicIncidentEvent);
314
                        }
315
                    }
316
                    return;
317
                }
318
            }
319
            $incidentContext = $this->createIncidentContext();
320
            $incidentContext->setActivityId($this->getActivityId());
321
            $incidentContext->setHistoryConfiguration($this->getLastFailureLogId());
0 ignored issues
show
Bug introduced by
It seems like $this->getLastFailureLogId() can also be of type null; however, parameter $historicConfiguration of Jabe\Impl\Incident\Incid...tHistoryConfiguration() does only seem to accept string, 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

321
            $incidentContext->setHistoryConfiguration(/** @scrutinizer ignore-type */ $this->getLastFailureLogId());
Loading history...
322
            $incidentContext->setFailedActivityId($this->getFailedActivityId());
0 ignored issues
show
Bug introduced by
It seems like $this->getFailedActivityId() can also be of type null; however, parameter $failedActivityId of Jabe\Impl\Incident\Incid...::setFailedActivityId() does only seem to accept string, 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

322
            $incidentContext->setFailedActivityId(/** @scrutinizer ignore-type */ $this->getFailedActivityId());
Loading history...
323
            IncidentHandling::createIncident($incidentHandlerType, $incidentContext, $this->exceptionMessage);
324
        }
325
    }
326
327
    protected function removeFailedJobIncident(bool $incidentResolved): void
328
    {
329
        $incidentContext = $this->createIncidentContext();
330
        IncidentHandling::removeIncidents(IncidentInterface::FAILED_JOB_HANDLER_TYPE, $incidentContext, $incidentResolved);
331
    }
332
333
    protected function createIncidentContext(): IncidentContext
334
    {
335
        $incidentContext = new IncidentContext();
0 ignored issues
show
Bug introduced by
The call to Jabe\Impl\Incident\IncidentContext::__construct() has too few arguments starting with incident. ( Ignorable by Annotation )

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

335
        $incidentContext = /** @scrutinizer ignore-call */ new IncidentContext();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
336
        $incidentContext->setProcessDefinitionId($this->processDefinitionId);
337
        $incidentContext->setExecutionId($this->executionId);
338
        $incidentContext->setTenantId($this->tenantId);
339
        $incidentContext->setConfiguration($this->id);
340
        $incidentContext->setJobDefinitionId($this->jobDefinitionId);
341
342
        return $incidentContext;
343
    }
344
345
    public function getExceptionStacktrace(): string
346
    {
347
        $byteArray = $this->getExceptionByteArray();
348
        return ExceptionUtil::getExceptionStacktrace($byteArray);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Jabe\Impl\Util\Ex...nStacktrace($byteArray) 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...
349
    }
350
351
    public function setSuspensionState(int $state): void
352
    {
353
        $this->suspensionState = $state;
354
    }
355
356
    public function getSuspensionState(): int
357
    {
358
        return $this->suspensionState;
359
    }
360
361
    public function isSuspended(): bool
362
    {
363
        return $this->suspensionState == SuspensionState::suspended()->getStateCode();
364
    }
365
366
    public function getProcessDefinitionId(): string
367
    {
368
        return $this->processDefinitionId;
369
    }
370
371
    public function setProcessDefinitionId(string $processDefinitionId): void
372
    {
373
        $this->processDefinitionId = $processDefinitionId;
374
    }
375
376
    public function getProcessDefinitionKey(): string
377
    {
378
        return $this->processDefinitionKey;
379
    }
380
381
    public function setProcessDefinitionKey(string $processDefinitionKey): void
382
    {
383
        $this->processDefinitionKey = $processDefinitionKey;
384
    }
385
386
    public function setExceptionStacktrace(string $exception): void
387
    {
388
        $exceptionBytes = $exception;
389
390
        $byteArray = $this->getExceptionByteArray();
391
392
        if ($byteArray === null) {
393
            $byteArray = ExceptionUtil::createJobExceptionByteArray($exceptionBytes, ResourceTypes::runtime());
394
            $this->exceptionByteArrayId = $byteArray->getId();
395
            $this->exceptionByteArray = $byteArray;
396
        } else {
397
            $byteArray->setBytes($exceptionBytes);
398
        }
399
    }
400
401
    protected function getJobHandler(): ?JobHandlerInterface
402
    {
403
        $jobHandlers = Context::getProcessEngineConfiguration()->getJobHandlers();
0 ignored issues
show
Bug introduced by
The method getJobHandlers() 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

403
        $jobHandlers = Context::getProcessEngineConfiguration()->/** @scrutinizer ignore-call */ getJobHandlers();

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...
404
        if (array_key_exists($this->jobHandlerType, $jobHandlers)) {
405
            return $jobHandlers[$this->jobHandlerType];
406
        }
407
        return null;
408
    }
409
410
    public function getJobHandlerConfiguration(): ?JobHandlerConfigurationInterface
411
    {
412
        $handler = $this->getJobHandler();
413
        if ($handler !== null) {
414
            return $handler->newConfiguration($this->jobHandlerConfiguration);
415
        }
416
        return null;
417
    }
418
419
    public function setJobHandlerConfiguration(JobHandlerConfigurationInterface $configuration): void
420
    {
421
        $this->jobHandlerConfiguration = $configuration->toCanonicalString();
422
    }
423
424
    public function getJobHandlerType(): string
425
    {
426
        return $this->jobHandlerType;
427
    }
428
429
    public function setJobHandlerType(string $jobHandlerType): void
430
    {
431
        $this->jobHandlerType = $jobHandlerType;
432
    }
433
434
    public function getJobHandlerConfigurationRaw(): string
435
    {
436
        return $this->jobHandlerConfiguration;
437
    }
438
439
    public function setJobHandlerConfigurationRaw(string $jobHandlerConfiguration): void
440
    {
441
        $this->jobHandlerConfiguration = $jobHandlerConfiguration;
442
    }
443
444
    public function getExceptionMessage(): ?string
445
    {
446
        return $this->exceptionMessage;
447
    }
448
449
    public function getJobDefinitionId(): ?string
450
    {
451
        return $this->jobDefinitionId;
452
    }
453
454
    public function setJobDefinitionId(string $jobDefinitionId): void
455
    {
456
        $this->jobDefinitionId = $jobDefinitionId;
457
    }
458
459
    public function getJobDefinition(): ?JobDefinitionInterface
460
    {
461
        $this->ensureJobDefinitionInitialized();
462
        return $this->jobDefinition;
463
    }
464
465
    public function setJobDefinition(?JobDefinitionInterface $jobDefinition): void
466
    {
467
        $this->jobDefinition = $jobDefinition;
468
        if ($jobDefinition !== null) {
469
            $this->jobDefinitionId = $jobDefinition->getId();
470
        } else {
471
            $this->jobDefinitionId = null;
472
        }
473
    }
474
475
    protected function ensureJobDefinitionInitialized(): void
476
    {
477
        if ($this->jobDefinition === null && $this->jobDefinitionId !== null) {
478
            $this->jobDefinition = Context::getCommandContext()
479
                ->getJobDefinitionManager()
480
                ->findById($this->jobDefinitionId);
481
        }
482
    }
483
484
    public function setExceptionMessage(string $exceptionMessage): void
485
    {
486
        $this->exceptionMessage = StringUtil::trimToMaximumLengthAllowed($exceptionMessage);
487
    }
488
489
    public function getExceptionByteArrayId(): ?string
490
    {
491
        return $this->exceptionByteArrayId;
492
    }
493
494
    protected function getExceptionByteArray(): ?ByteArrayEntity
495
    {
496
        $this->ensureExceptionByteArrayInitialized();
497
        return $this->exceptionByteArray;
498
    }
499
500
    protected function ensureExceptionByteArrayInitialized(): void
501
    {
502
        if ($this->exceptionByteArray === null && $this->exceptionByteArrayId !== null) {
503
            $this->exceptionByteArray = Context::getCommandContext()
504
                ->getDbEntityManager()
505
                ->selectById(ByteArrayEntity::class, $this->exceptionByteArrayId);
506
        }
507
    }
508
509
    protected function clearFailedJobException(): void
510
    {
511
        $byteArray = $this->getExceptionByteArray();
512
513
        // Avoid NPE when the job was reconfigured by another
514
        // node in the meantime
515
        if ($byteArray !== null) {
516
            Context::getCommandContext()
517
                ->getDbEntityManager()
518
                ->delete($byteArray);
519
        }
520
521
        $this->exceptionByteArrayId = null;
522
        $this->exceptionMessage = null;
523
    }
524
525
    public function getDeploymentId(): ?string
526
    {
527
        return $this->deploymentId;
528
    }
529
530
    public function setDeploymentId(string $deploymentId): void
0 ignored issues
show
Unused Code introduced by
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

530
    public function setDeploymentId(/** @scrutinizer ignore-unused */ string $deploymentId): void

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...
531
    {
532
        $this->deploymentId = $this->deploymentId;
533
    }
534
535
    public function isInInconsistentLockState(): bool
536
    {
537
        return ($this->lockOwner !== null && $this->lockExpirationTime === null)
538
            || ($this->retries == 0 && ($this->lockOwner !== null || $this->lockExpirationTime !== null));
539
    }
540
541
    public function resetLock(): void
542
    {
543
        $this->lockOwner = null;
544
        $this->lockExpirationTime = null;
545
    }
546
547
    public function getActivityId(): string
548
    {
549
        $this->ensureActivityIdInitialized();
550
        return $this->activityId;
551
    }
552
553
    public function setActivityId(string $activityId): void
554
    {
555
        $this->activityId = $activityId;
556
    }
557
558
    public function getPriority(): int
559
    {
560
        return $this->priority;
561
    }
562
563
    public function setPriority(int $priority): void
564
    {
565
        $this->priority = $priority;
566
    }
567
568
    public function getTenantId(): ?string
569
    {
570
        return $this->tenantId;
571
    }
572
573
    public function setTenantId(?string $tenantId): void
574
    {
575
        $this->tenantId = $tenantId;
576
    }
577
578
    public function getCreateTime(): string
579
    {
580
        return $this->createTime;
581
    }
582
583
    public function setCreateTime(string $createTime): void
584
    {
585
        $this->createTime = $createTime;
586
    }
587
588
    protected function ensureActivityIdInitialized(): void
589
    {
590
        if ($this->activityId === null) {
591
            $jobDefinition = $this->getJobDefinition();
592
            if ($jobDefinition !== null) {
593
                $this->activityId = $jobDefinition->getActivityId();
594
            } else {
595
                $execution = $this->getExecution();
596
                if ($execution !== null) {
597
                    $this->activityId = $execution->getActivityId();
598
                }
599
            }
600
        }
601
    }
602
603
    /**
604
     *
605
     * Unlock from current lock owner
606
     *
607
     */
608
    public function unlock(): void
609
    {
610
        $this->lockOwner = null;
611
        $this->lockExpirationTime = null;
612
    }
613
614
    abstract public function getType(): string;
615
616
    public function equals($obj): bool
617
    {
618
        if ($this == $obj) {
619
            return true;
620
        }
621
        if ($obj === null) {
622
            return false;
623
        }
624
        if (get_class($this) != get_class($obj)) {
625
            return false;
626
        }
627
        if ($this->id === null) {
628
            if ($obj->id !== null) {
629
                return false;
630
            }
631
        } elseif ($this->id != $obj->id) {
632
            return false;
633
        }
634
        return true;
635
    }
636
637
    public function getReferencedEntityIds(): array
638
    {
639
        return [];
640
    }
641
642
    public function getReferencedEntitiesIdAndClass(): array
643
    {
644
        $referenceIdAndClass = [];
645
646
        if ($this->exceptionByteArrayId !== null) {
647
            $referenceIdAndClass[$this->exceptionByteArrayId] = ByteArrayEntity::class;
648
        }
649
650
        return $referenceIdAndClass;
651
    }
652
653
    public function getDependentEntities(): array
654
    {
655
        return $this->persistedDependentEntities;
656
    }
657
658
    public function postLoad(): void
659
    {
660
        if ($this->exceptionByteArrayId !== null) {
661
            $this->persistedDependentEntities = [];
662
            $this->persistedDependentEntities[$this->exceptionByteArrayId] = ByteArrayEntity::class;
663
        } else {
664
            $this->persistedDependentEntities = [];
665
        }
666
    }
667
668
    public function getLastFailureLogId(): ?string
669
    {
670
        return $this->lastFailureLogId;
671
    }
672
673
    public function setLastFailureLogId(string $lastFailureLogId): void
674
    {
675
        $this->lastFailureLogId = $lastFailureLogId;
676
    }
677
678
    public function getFailedActivityId(): ?string
679
    {
680
        return $this->failedActivityId;
681
    }
682
683
    public function setFailedActivityId(string $failedActivityId): void
684
    {
685
        $this->failedActivityId = $failedActivityId;
686
    }
687
688
    public function serialize()
689
    {
690
        return json_encode([
691
            'id' => $this->id,
692
            'revision' => $this->revision,
693
            'duedate' => $this->duedate,
694
            'lockOwner' => $this->lockOwner,
695
            'lockExpirationTime' => $this->lockExpirationTime,
696
            'executionId' => $this->executionId,
697
            'processInstanceId' => $this->processInstanceId,
698
            'isExclusive' => $this->isExclusive,
699
            'jobDefinitionId' => $this->jobDefinitionId,
700
            'jobHandlerType' => $this->jobHandlerType,
701
            'jobHandlerConfiguration' => $this->jobHandlerConfiguration,
702
            'exceptionByteArray' => serialize($this->exceptionByteArray),
703
            'exceptionByteArrayId' => $this->exceptionByteArrayId,
704
            'exceptionMessage' => $this->exceptionMessage,
705
            'failedActivityId' => $this->failedActivityId,
706
            'deploymentId' => $this->deploymentId,
707
            'priority' => $this->priority,
708
            'tenantId' => $this->tenantId
709
        ]);
710
    }
711
712
    public function unserialize($data)
713
    {
714
        $json = json_decode($data);
715
        $this->id = $json->id;
716
        $this->revision = $json->revision;
717
        $this->lockOwner = $json->lockOwner;
718
        $this->lockExpirationTime = $json->lockExpirationTime;
719
        $this->executionId = $json->executionId;
720
        $this->processInstanceId = $json->processInstanceId;
721
        $this->isExclusive = $json->isExclusive;
722
        $this->jobDefinitionId = $json->jobDefinitionId;
723
        $this->jobHandlerType = $json->jobHandlerType;
724
        $this->jobHandlerConfiguration = $json->jobHandlerConfiguration;
725
        $this->exceptionByteArray = unserialize($json->exceptionByteArray);
726
        $this->exceptionByteArrayId = $json->exceptionByteArrayId;
727
        $this->exceptionMessage = $json->exceptionMessage;
728
        $this->failedActivityId = $json->failedActivityId;
729
        $this->deploymentId = $json->deploymentId;
730
        $this->priority = $json->priority;
731
        $this->tenantId = $json->tenantId;
732
    }
733
734
    public function __toString()
735
    {
736
        $className = ClassNameUtil::getClassNameWithoutPackage(get_class($this));
737
        return $className
738
            . "[id=" . $this->id
739
            . ", revision=" . $this->revision
740
            . ", duedate=" . $this->duedate
741
            . ", lockOwner=" . $this->lockOwner
742
            . ", lockExpirationTime=" . $this->lockExpirationTime
743
            . ", executionId=" . $this->executionId
744
            . ", processInstanceId=" . $this->processInstanceId
745
            . ", isExclusive=" . $this->isExclusive
746
            . ", jobDefinitionId=" . $this->jobDefinitionId
747
            . ", jobHandlerType=" . $this->jobHandlerType
748
            . ", jobHandlerConfiguration=" . $this->jobHandlerConfiguration
749
            . ", exceptionByteArray=" . $this->exceptionByteArray
750
            . ", exceptionByteArrayId=" . $this->exceptionByteArrayId
751
            . ", exceptionMessage=" . $this->exceptionMessage
752
            . ", failedActivityId=" . $this->failedActivityId
753
            . ", deploymentId=" . $this->deploymentId
754
            . ", priority=" . $this->priority
755
            . ", tenantId=" . $this->tenantId
756
            . "]";
757
    }
758
}
759