|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Global Trading Technologies Ltd workflow-extension-bundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) fduch <[email protected]> |
|
9
|
|
|
* @date 28.07.16 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Gtt\Bundle\WorkflowExtensionsBundle\Schedule; |
|
13
|
|
|
|
|
14
|
|
|
use Carbon\Carbon; |
|
15
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
16
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Entity\Repository\ScheduledJobRepository; |
|
17
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Entity\ScheduledJob; |
|
18
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Logger\WorkflowLoggerContextTrait; |
|
19
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\SubjectManipulator; |
|
20
|
|
|
use JMS\JobQueueBundle\Entity\Job; |
|
21
|
|
|
use Psr\Log\LoggerInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Schedules transition to apply after some time |
|
25
|
|
|
*/ |
|
26
|
|
|
class TransitionScheduler |
|
27
|
|
|
{ |
|
28
|
|
|
use WorkflowLoggerContextTrait; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Persistance object manager |
|
32
|
|
|
* |
|
33
|
|
|
* @var ObjectManager |
|
34
|
|
|
*/ |
|
35
|
|
|
private $em; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Subject manipulator |
|
39
|
|
|
* |
|
40
|
|
|
* @var SubjectManipulator |
|
41
|
|
|
*/ |
|
42
|
|
|
private $subjectManipulator; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Logger |
|
46
|
|
|
* |
|
47
|
|
|
* @var LoggerInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
private $logger; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* TransitionScheduler constructor. |
|
53
|
|
|
* |
|
54
|
|
|
* @param ObjectManager $em entity manager |
|
55
|
|
|
* @param SubjectManipulator $subjectManipulator subject manipulator |
|
56
|
|
|
* @param LoggerInterface $logger logger |
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct(ObjectManager $em, SubjectManipulator $subjectManipulator, LoggerInterface $logger) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->em = $em; |
|
61
|
|
|
$this->subjectManipulator = $subjectManipulator; |
|
62
|
|
|
$this->logger = $logger; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Schedules transitions |
|
67
|
|
|
* |
|
68
|
|
|
* @param object $subject current subject |
|
69
|
|
|
* @param string $workflowName workflow name |
|
70
|
|
|
* @param ScheduledTransition[] $scheduledTransitions list of scheduled transitions |
|
71
|
|
|
*/ |
|
72
|
|
|
public function scheduleTransitions($subject, $workflowName, $scheduledTransitions) |
|
73
|
|
|
{ |
|
74
|
|
|
$subjectClass = get_class($subject); |
|
75
|
|
|
$subjectId = $this->subjectManipulator->getSubjectId($subject); |
|
76
|
|
|
|
|
77
|
|
|
$loggerContext = $this->getLoggerContext($workflowName, $subjectClass, $subjectId); |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** @var ScheduledJobRepository $scheduledJobRepository */ |
|
80
|
|
|
$scheduledJobRepository = $this->em->getRepository(ScheduledJob::class); |
|
81
|
|
|
|
|
82
|
|
|
foreach ($scheduledTransitions as $scheduledTransition) { |
|
83
|
|
|
$transitionName = $scheduledTransition->getTransitionName(); |
|
84
|
|
|
|
|
85
|
|
|
$transitionTriggerJobToSchedule = new Job('workflow:transition:trigger', |
|
86
|
|
|
[ |
|
87
|
|
|
'--transition=' . $transitionName, |
|
88
|
|
|
'--workflow=' . $workflowName, |
|
89
|
|
|
'--subjectClass=' . $subjectClass, |
|
90
|
|
|
'--subjectId=' . $subjectId, |
|
91
|
|
|
] |
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
|
|
try { |
|
95
|
|
|
$scheduledJobToReschedule = $scheduledJobRepository->findScheduledJobToReschedule($transitionTriggerJobToSchedule); |
|
96
|
|
|
if ($scheduledJobToReschedule) { |
|
97
|
|
|
// the job was already scheduled but not executed. Now we need to reschedule it |
|
98
|
|
|
$this->rescheduleTransitionTriggerJob($scheduledTransition, $scheduledJobToReschedule, $loggerContext); |
|
99
|
|
|
} else { |
|
100
|
|
|
// creating new jms job to trigger transition |
|
101
|
|
|
$this->scheduleTransitionTriggerJob($scheduledTransition, $transitionTriggerJobToSchedule, $loggerContext); |
|
102
|
|
|
} |
|
103
|
|
|
} catch (\Exception $e) { |
|
104
|
|
|
$this->logger->error( |
|
105
|
|
|
sprintf('Workflow cannot schedule/reschedule transition "%s". Details: %s', $transitionName, $e->getMessage()), |
|
106
|
|
|
$loggerContext |
|
107
|
|
|
); |
|
108
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
|
|
109
|
|
|
$this->logger->error( |
|
110
|
|
|
sprintf('Workflow cannot schedule/reschedule transition "%s". Details: %s', $transitionName, $e->getMessage()), |
|
111
|
|
|
$loggerContext |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Reschedules already scheduled transition |
|
119
|
|
|
* |
|
120
|
|
|
* @param ScheduledTransition $scheduledTransition scheduled transition |
|
121
|
|
|
* @param ScheduledJob $scheduledJob scheduled job for transition |
|
122
|
|
|
* @param array $loggerContext logger context |
|
123
|
|
|
*/ |
|
124
|
|
|
private function rescheduleTransitionTriggerJob(ScheduledTransition $scheduledTransition, ScheduledJob $scheduledJob, array $loggerContext) |
|
125
|
|
|
{ |
|
126
|
|
|
$transitionTriggerJob = $scheduledJob->getJob(); |
|
127
|
|
|
$transitionTriggerJob->setExecuteAfter($this->getTransitionTriggerExecutionDate($scheduledTransition)); |
|
128
|
|
|
|
|
129
|
|
|
// since jms Job states DEFERRED_EXPLICIT change tracking policy we should explicitly persist entity now |
|
130
|
|
|
$this->em->persist($transitionTriggerJob); |
|
131
|
|
|
$this->em->flush(); |
|
132
|
|
|
|
|
133
|
|
|
$this->logger->info( |
|
134
|
|
|
sprintf("Workflow successfully rescheduled transition '%s'", $scheduledTransition->getTransitionName()), |
|
135
|
|
|
$loggerContext |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Schedules transition |
|
141
|
|
|
* |
|
142
|
|
|
* @param ScheduledTransition $scheduledTransition scheduled transition |
|
143
|
|
|
* @param Job $transitionTriggerJobToSchedule Job to be scheduled |
|
144
|
|
|
* @param array $loggerContext logger context |
|
145
|
|
|
*/ |
|
146
|
|
|
private function scheduleTransitionTriggerJob(ScheduledTransition $scheduledTransition, Job $transitionTriggerJobToSchedule, $loggerContext) |
|
147
|
|
|
{ |
|
148
|
|
|
$executionDate = $this->getTransitionTriggerExecutionDate($scheduledTransition); |
|
149
|
|
|
$transitionTriggerJobToSchedule->setExecuteAfter($executionDate); |
|
150
|
|
|
|
|
151
|
|
|
$scheduledJob = new ScheduledJob( |
|
152
|
|
|
$transitionTriggerJobToSchedule |
|
153
|
|
|
); |
|
154
|
|
|
|
|
155
|
|
|
$this->em->persist($transitionTriggerJobToSchedule); |
|
156
|
|
|
$this->em->persist($scheduledJob); |
|
157
|
|
|
|
|
158
|
|
|
$this->em->flush(); |
|
159
|
|
|
|
|
160
|
|
|
$this->logger->info( |
|
161
|
|
|
sprintf("Workflow successfully scheduled transition '%s'", $scheduledTransition->getTransitionName()), |
|
162
|
|
|
$loggerContext |
|
163
|
|
|
); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Calculates execution date date for scheduled transition |
|
168
|
|
|
* |
|
169
|
|
|
* @param ScheduledTransition $scheduledTransition scheduled transition |
|
170
|
|
|
* |
|
171
|
|
|
* @return \DateTime |
|
172
|
|
|
*/ |
|
173
|
|
|
private function getTransitionTriggerExecutionDate(ScheduledTransition $scheduledTransition) |
|
174
|
|
|
{ |
|
175
|
|
|
$executionDate = Carbon::now(); |
|
176
|
|
|
$executionDate->add($scheduledTransition->getOffset()); |
|
177
|
|
|
|
|
178
|
|
|
return $executionDate; |
|
179
|
|
|
} |
|
180
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: