|
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 01.08.16 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Gtt\Bundle\WorkflowExtensionsBundle\Entity\Repository; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\ORM\EntityRepository; |
|
15
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Entity\ScheduledJob; |
|
16
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Exception\NonUniqueReschedulabeJobFoundException; |
|
17
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Schedule\Action\ScheduledAction; |
|
18
|
|
|
use JMS\JobQueueBundle\Entity\Job; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* ScheduledJobRepository |
|
22
|
|
|
*/ |
|
23
|
|
|
class ScheduledJobRepository extends EntityRepository |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Finds ScheduledJob by original Job can be scheduled earlier |
|
27
|
|
|
* |
|
28
|
|
|
* @param $originalJob $originalJob original job |
|
|
|
|
|
|
29
|
|
|
* |
|
30
|
|
|
* @return ScheduledJob|null |
|
31
|
|
|
*/ |
|
32
|
|
|
public function findScheduledJobToReschedule(Job $originalJob) |
|
33
|
|
|
{ |
|
34
|
|
|
// fetching scheduled job that was not started before - it can be rescheduled |
|
35
|
|
|
$queryString = <<<'QUERY' |
|
36
|
|
|
SELECT sj FROM WorkflowExtensionsBundle:ScheduledJob sj |
|
37
|
|
|
JOIN sj.job j |
|
38
|
|
|
WHERE |
|
39
|
|
|
j.state in (:stateNew, :statePending) AND |
|
40
|
|
|
j.command = :command AND |
|
41
|
|
|
j.args = :args AND |
|
42
|
|
|
sj.reschedulable = 1 |
|
43
|
|
|
QUERY; |
|
44
|
|
|
|
|
45
|
|
|
/** @var ScheduledJob[] $scheduledJobsToReschedule */ |
|
46
|
|
|
$scheduledJobsToReschedule = $this->_em |
|
47
|
|
|
->createQuery($queryString) |
|
48
|
|
|
->setParameters([ |
|
49
|
|
|
'stateNew' => Job::STATE_NEW, |
|
50
|
|
|
'statePending' => Job::STATE_PENDING, |
|
51
|
|
|
'command' => $originalJob->getCommand(), |
|
52
|
|
|
'args' => json_encode($originalJob->getArgs()) |
|
53
|
|
|
]) |
|
54
|
|
|
->getResult() |
|
55
|
|
|
; |
|
56
|
|
|
|
|
57
|
|
|
if (!$scheduledJobsToReschedule) { |
|
|
|
|
|
|
58
|
|
|
return null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (count($scheduledJobsToReschedule) > 1) { |
|
62
|
|
|
// since there is normally only one scheduled pending/new job here |
|
63
|
|
|
// (because an attempt to schedule duplicate job raises rescheduling of the first one) |
|
64
|
|
|
// we throwing exception in case of several results here |
|
65
|
|
|
// TODO probably we need support several jobs for the same transition, workflow and subject scheduled for different times? |
|
66
|
|
|
$duplicateReschedulableJobsIds = []; |
|
67
|
|
|
foreach ($scheduledJobsToReschedule as $scheduledJobToReschedule) { |
|
68
|
|
|
$duplicateReschedulableJobsIds[] = $scheduledJobToReschedule->getJob()->getId(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
throw new NonUniqueReschedulabeJobFoundException($originalJob, $duplicateReschedulableJobsIds); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return reset($scheduledJobsToReschedule); |
|
75
|
|
|
} |
|
76
|
|
|
} |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.