|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of php-task library. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) php-task |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Task\TaskBundle\Entity; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\ORM\EntityRepository; |
|
15
|
|
|
use Doctrine\ORM\NoResultException; |
|
16
|
|
|
use Task\Execution\TaskExecutionInterface; |
|
17
|
|
|
use Task\Storage\TaskExecutionRepositoryInterface; |
|
18
|
|
|
use Task\TaskInterface; |
|
19
|
|
|
use Task\TaskStatus; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Repository for task-execution. |
|
23
|
|
|
*/ |
|
24
|
|
|
class TaskExecutionRepository extends EntityRepository implements TaskExecutionRepositoryInterface |
|
|
|
|
|
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
9 |
|
public function create(TaskInterface $task, \DateTime $scheduleTime) |
|
30
|
|
|
{ |
|
31
|
9 |
|
return new TaskExecution($task, $task->getHandlerClass(), $scheduleTime, $task->getWorkload()); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
*/ |
|
37
|
9 |
|
public function save(TaskExecutionInterface $execution) |
|
38
|
|
|
{ |
|
39
|
9 |
|
$this->_em->persist($execution); |
|
40
|
9 |
|
$this->_em->flush($execution); |
|
41
|
|
|
|
|
42
|
9 |
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
|
|
public function remove(TaskExecutionInterface $execution) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->_em->remove($execution); |
|
51
|
|
|
$this->_em->flush($execution); |
|
52
|
|
|
|
|
53
|
|
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
6 |
View Code Duplication |
public function findAll($page = 1, $pageSize = null) |
|
|
|
|
|
|
60
|
|
|
{ |
|
61
|
6 |
|
$query = $this->createQueryBuilder('e') |
|
62
|
6 |
|
->innerJoin('e.task', 't') |
|
63
|
6 |
|
->getQuery(); |
|
64
|
|
|
|
|
65
|
6 |
|
if ($pageSize) { |
|
|
|
|
|
|
66
|
3 |
|
$query->setMaxResults($pageSize); |
|
67
|
3 |
|
$query->setFirstResult(($page - 1) * $pageSize); |
|
68
|
3 |
|
} |
|
69
|
|
|
|
|
70
|
6 |
|
return $query->getResult(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
7 |
|
public function findPending(TaskInterface $task) |
|
77
|
|
|
{ |
|
78
|
|
|
try { |
|
79
|
7 |
|
return $this->createQueryBuilder('e') |
|
80
|
7 |
|
->innerJoin('e.task', 't') |
|
81
|
7 |
|
->where('t.uuid = :uuid') |
|
82
|
7 |
|
->andWhere('e.status in (:status)') |
|
83
|
7 |
|
->setParameter('uuid', $task->getUuid()) |
|
84
|
7 |
|
->setParameter('status', [TaskStatus::PLANNED, TaskStatus::STARTED]) |
|
85
|
7 |
|
->getQuery() |
|
86
|
7 |
|
->getSingleResult(); |
|
87
|
7 |
|
} catch (NoResultException $e) { |
|
88
|
7 |
|
return; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public function findByTask(TaskInterface $task) |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->createQueryBuilder('e') |
|
98
|
|
|
->innerJoin('e.task', 't') |
|
99
|
|
|
->where('t.uuid = :uuid') |
|
100
|
|
|
->setParameter('uuid', $task->getUuid()) |
|
101
|
|
|
->getQuery() |
|
102
|
|
|
->getResult(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* {@inheritdoc} |
|
107
|
|
|
*/ |
|
108
|
2 |
|
public function findScheduled(\DateTime $dateTime = null) |
|
109
|
|
|
{ |
|
110
|
2 |
|
if ($dateTime === null) { |
|
111
|
2 |
|
$dateTime = new \DateTime(); |
|
112
|
2 |
|
} |
|
113
|
|
|
|
|
114
|
2 |
|
return $this->createQueryBuilder('e') |
|
115
|
2 |
|
->innerJoin('e.task', 't') |
|
116
|
2 |
|
->where('e.status = :status AND e.scheduleTime < :dateTime') |
|
117
|
2 |
|
->setParameter('status', TaskStatus::PLANNED) |
|
118
|
2 |
|
->setParameter('dateTime', $dateTime) |
|
119
|
2 |
|
->getQuery() |
|
120
|
2 |
|
->getResult(); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|