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
|
19 |
|
public function save(TaskExecutionInterface $execution) |
38
|
|
|
{ |
39
|
19 |
|
$this->_em->persist($execution); |
40
|
19 |
|
$this->_em->flush($execution); |
41
|
|
|
|
42
|
19 |
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
1 |
|
public function remove(TaskExecutionInterface $execution) |
49
|
|
|
{ |
50
|
1 |
|
$this->_em->remove($execution); |
51
|
1 |
|
$this->_em->flush($execution); |
52
|
|
|
|
53
|
1 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
8 |
View Code Duplication |
public function findAll($page = 1, $pageSize = null) |
|
|
|
|
60
|
|
|
{ |
61
|
8 |
|
$query = $this->createQueryBuilder('e') |
62
|
8 |
|
->innerJoin('e.task', 't') |
63
|
8 |
|
->getQuery(); |
64
|
|
|
|
65
|
8 |
|
if ($pageSize) { |
|
|
|
|
66
|
4 |
|
$query->setMaxResults($pageSize); |
67
|
4 |
|
$query->setFirstResult(($page - 1) * $pageSize); |
68
|
4 |
|
} |
69
|
|
|
|
70
|
8 |
|
return $query->getResult(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
8 |
|
public function findPending(TaskInterface $task) |
77
|
|
|
{ |
78
|
|
|
try { |
79
|
8 |
|
return $this->createQueryBuilder('e') |
80
|
8 |
|
->innerJoin('e.task', 't') |
81
|
8 |
|
->where('t.uuid = :uuid') |
82
|
8 |
|
->andWhere('e.status in (:status)') |
83
|
8 |
|
->setParameter('uuid', $task->getUuid()) |
84
|
8 |
|
->setParameter('status', [TaskStatus::PLANNED, TaskStatus::RUNNING]) |
85
|
8 |
|
->getQuery() |
86
|
8 |
|
->getSingleResult(); |
87
|
8 |
|
} catch (NoResultException $e) { |
88
|
8 |
|
return; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
5 |
|
public function findByUuid($uuid) |
96
|
|
|
{ |
97
|
|
|
try { |
98
|
5 |
|
return $this->createQueryBuilder('e') |
99
|
5 |
|
->where('e.uuid = :uuid') |
100
|
5 |
|
->setParameter('uuid', $uuid) |
101
|
5 |
|
->getQuery() |
102
|
5 |
|
->getSingleResult(); |
103
|
1 |
|
} catch (NoResultException $e) { |
104
|
1 |
|
return; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
4 |
|
public function findByTask(TaskInterface $task) |
112
|
|
|
{ |
113
|
4 |
|
return $this->findByTaskUuid($task->getUuid()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
5 |
|
public function findByTaskUuid($taskUuid) |
120
|
|
|
{ |
121
|
5 |
|
return $this->createQueryBuilder('e') |
122
|
5 |
|
->innerJoin('e.task', 't') |
123
|
5 |
|
->where('t.uuid = :uuid') |
124
|
5 |
|
->setParameter('uuid', $taskUuid) |
125
|
5 |
|
->getQuery() |
126
|
5 |
|
->getResult(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
4 |
|
public function findNextScheduled(\DateTime $dateTime = null) |
133
|
|
|
{ |
134
|
4 |
|
$query = $this->createQueryBuilder('e') |
135
|
4 |
|
->innerJoin('e.task', 't') |
136
|
4 |
|
->where('e.status = :status') |
137
|
4 |
|
->andWhere('e.scheduleTime < :date') |
138
|
4 |
|
->setParameter('date', $dateTime ?: new \DateTime()) |
139
|
4 |
|
->setParameter('status', TaskStatus::PLANNED) |
140
|
4 |
|
->setMaxResults(1) |
141
|
|
|
->getQuery(); |
142
|
4 |
|
|
143
|
|
|
try { |
144
|
|
|
return $query->getSingleResult(); |
145
|
|
|
} catch (NoResultException $exception) { |
146
|
|
|
return null; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|