|
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 Gtt\Bundle\WorkflowExtensionsBundle\WorkflowContext; |
|
15
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Command\ExecuteActionCommand; |
|
16
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Entity\Repository\ScheduledJobRepository; |
|
17
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Entity\ScheduledJob; |
|
18
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Schedule\ValueObject\ScheduledAction; |
|
19
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\WorkflowSubject\SubjectManipulator; |
|
20
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
21
|
|
|
use JMS\JobQueueBundle\Entity\Job; |
|
22
|
|
|
use Carbon\Carbon; |
|
23
|
|
|
use Psr\Log\LoggerInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Schedules action to be executed after some time |
|
27
|
|
|
*/ |
|
28
|
|
|
class ActionScheduler |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* Persistance object manager |
|
32
|
|
|
* |
|
33
|
|
|
* @var ObjectManager |
|
34
|
|
|
*/ |
|
35
|
|
|
private $em; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Logger |
|
39
|
|
|
* |
|
40
|
|
|
* @var LoggerInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
private $logger; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* ActionScheduler constructor. |
|
46
|
|
|
* |
|
47
|
|
|
* @param ObjectManager $em entity manager |
|
48
|
|
|
* @param LoggerInterface $logger logger |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(ObjectManager $em, LoggerInterface $logger) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->em = $em; |
|
53
|
|
|
$this->logger = $logger; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Schedules actions |
|
58
|
|
|
* |
|
59
|
|
|
* @param WorkflowContext $workflowContext workflow context |
|
60
|
|
|
* @param ScheduledAction $scheduledAction scheduled action |
|
61
|
|
|
*/ |
|
62
|
|
|
public function scheduleAction(WorkflowContext $workflowContext, ScheduledAction $scheduledAction) |
|
63
|
|
|
{ |
|
64
|
|
|
/** @var ScheduledJobRepository $scheduledJobRepository */ |
|
65
|
|
|
$scheduledJobRepository = $this->em->getRepository(ScheduledJob::class); |
|
66
|
|
|
|
|
67
|
|
|
$jobToSchedule = new Job(ExecuteActionCommand::COMMAND_NAME, |
|
68
|
|
|
[ |
|
69
|
|
|
'--action=' . $scheduledAction->getName(), |
|
70
|
|
|
'--arguments=' . json_encode($scheduledAction->getArguments()), |
|
71
|
|
|
'--workflow=' . $workflowContext->getWorkflow()->getName(), |
|
72
|
|
|
'--subjectClass=' . get_class($workflowContext->getSubject()), |
|
73
|
|
|
'--subjectId=' . $workflowContext->getSubjectId(), |
|
74
|
|
|
] |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
$scheduledJob = null; |
|
78
|
|
|
if ($scheduledAction->isReschedulable()) { |
|
79
|
|
|
$scheduledJob = $scheduledJobRepository->findScheduledJobToReschedule($jobToSchedule); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if ($scheduledJob) { |
|
83
|
|
|
// the job was already scheduled but not executed. Now we need to reschedule it |
|
84
|
|
|
$this->rescheduleActionJob($scheduledAction, $scheduledJob, $workflowContext); |
|
85
|
|
|
} else { |
|
86
|
|
|
// creating new jms job to trigger action |
|
87
|
|
|
$this->scheduleActionJob($scheduledAction, $jobToSchedule, $workflowContext); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Reschedules already scheduled action |
|
93
|
|
|
* |
|
94
|
|
|
* @param ScheduledAction $scheduledAction scheduled action |
|
95
|
|
|
* @param Job $jobToSchedule job to schedule |
|
96
|
|
|
* @param WorkflowContext $workflowContext workflow context |
|
97
|
|
|
*/ |
|
98
|
|
|
private function scheduleActionJob(ScheduledAction $scheduledAction, Job $jobToSchedule, WorkflowContext $workflowContext) |
|
99
|
|
|
{ |
|
100
|
|
|
$executionDate = $this->getActionExecutionDate($scheduledAction); |
|
101
|
|
|
$jobToSchedule->setExecuteAfter($executionDate); |
|
102
|
|
|
|
|
103
|
|
|
$scheduledJob = new ScheduledJob($jobToSchedule, $scheduledAction->isReschedulable()); |
|
104
|
|
|
|
|
105
|
|
|
$this->em->persist($jobToSchedule); |
|
106
|
|
|
$this->em->persist($scheduledJob); |
|
107
|
|
|
|
|
108
|
|
|
$this->em->flush(); |
|
109
|
|
|
|
|
110
|
|
|
$this->logger->info( |
|
111
|
|
|
sprintf( |
|
112
|
|
|
"Workflow successfully scheduled action '%s' with parameters '%s'", |
|
113
|
|
|
$scheduledAction->getName(), |
|
114
|
|
|
json_encode($scheduledAction->getArguments()) |
|
115
|
|
|
), |
|
116
|
|
|
$workflowContext->getLoggerContext() |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Reschedules already scheduled action |
|
122
|
|
|
* |
|
123
|
|
|
* @param ScheduledAction $scheduledAction scheduled action |
|
124
|
|
|
* @param ScheduledJob $scheduledJob scheduled job for action |
|
125
|
|
|
* @param WorkflowContext $workflowContext workflow context |
|
126
|
|
|
*/ |
|
127
|
|
|
private function rescheduleActionJob(ScheduledAction $scheduledAction, ScheduledJob $scheduledJob, WorkflowContext $workflowContext) |
|
128
|
|
|
{ |
|
129
|
|
|
$actionJob = $scheduledJob->getJob(); |
|
130
|
|
|
$actionJob->setExecuteAfter($this->getActionExecutionDate($scheduledAction)); |
|
131
|
|
|
|
|
132
|
|
|
// since jms Job states DEFERRED_EXPLICIT change tracking policy we should explicitly persist entity now |
|
133
|
|
|
$this->em->persist($actionJob); |
|
134
|
|
|
$this->em->flush(); |
|
135
|
|
|
|
|
136
|
|
|
$this->logger->info( |
|
137
|
|
|
sprintf("Workflow successfully rescheduled action '%s' with parameters '%s'", |
|
138
|
|
|
$scheduledAction->getName(), |
|
139
|
|
|
json_encode($scheduledAction->getArguments()) |
|
140
|
|
|
), |
|
141
|
|
|
$workflowContext->getLoggerContext() |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Calculates execution date date for scheduled action |
|
147
|
|
|
* |
|
148
|
|
|
* @param ScheduledAction $scheduledAction scheduled action |
|
149
|
|
|
* |
|
150
|
|
|
* @return \DateTime |
|
151
|
|
|
*/ |
|
152
|
|
|
private function getActionExecutionDate(ScheduledAction $scheduledAction) |
|
153
|
|
|
{ |
|
154
|
|
|
$executionDate = Carbon::now(); |
|
155
|
|
|
$executionDate->add($scheduledAction->getOffset()); |
|
156
|
|
|
|
|
157
|
|
|
return $executionDate; |
|
158
|
|
|
} |
|
159
|
|
|
} |