-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJobRepository.php
More file actions
207 lines (178 loc) · 6.65 KB
/
Copy pathJobRepository.php
File metadata and controls
207 lines (178 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
declare(strict_types=1);
namespace CodeRhapsodie\DataflowBundle\Repository;
use CodeRhapsodie\DataflowBundle\Entity\Job;
use CodeRhapsodie\DataflowBundle\Entity\ScheduledDataflow;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Query\QueryBuilder;
/**
* Repository.
*
* @codeCoverageIgnore
*/
class JobRepository
{
use InitFromDbTrait;
public const TABLE_NAME = 'cr_dataflow_job';
/**
* JobRepository constructor.
*/
public function __construct(private Connection $connection)
{
}
public function find(int $jobId)
{
$qb = $this->createQueryBuilder();
$qb
->andWhere($qb->expr()->eq('id', $qb->createNamedParameter($jobId, ParameterType::INTEGER)))
;
return $this->returnFirstOrNull($qb);
}
public function findOneshotDataflows(): iterable
{
$qb = $this->createQueryBuilder();
$qb
->andWhere($qb->expr()->isNull('scheduled_dataflow_id'))
->andWhere($qb->expr()->eq('status', $qb->createNamedParameter(Job::STATUS_PENDING, ParameterType::INTEGER)));
$stmt = $qb->executeQuery();
if ($stmt->rowCount() === 0) {
return [];
}
while (false !== ($row = $stmt->fetchAssociative())) {
yield Job::createFromArray($this->initDateTime($this->initArray($row)));
}
}
public function findPendingForScheduledDataflow(ScheduledDataflow $scheduled): ?Job
{
$qb = $this->createQueryBuilder();
$qb
->andWhere($qb->expr()->eq('scheduled_dataflow_id', $qb->createNamedParameter($scheduled->getId(), ParameterType::INTEGER)))
->andWhere($qb->expr()->eq('status', $qb->createNamedParameter(Job::STATUS_PENDING, ParameterType::INTEGER)));
return $this->returnFirstOrNull($qb);
}
public function findNextPendingDataflow(): ?Job
{
$qb = $this->createQueryBuilder();
$qb->andWhere($qb->expr()->lte('requested_date', $qb->createNamedParameter(new \DateTime(), 'datetime')))
->andWhere($qb->expr()->eq('status', $qb->createNamedParameter(Job::STATUS_PENDING, ParameterType::INTEGER)))
->orderBy('requested_date', 'ASC')
->setMaxResults(1)
;
return $this->returnFirstOrNull($qb);
}
public function findLastForDataflowId(int $dataflowId): ?Job
{
$qb = $this->createQueryBuilder();
$qb->andWhere($qb->expr()->eq('scheduled_dataflow_id', $qb->createNamedParameter($dataflowId, ParameterType::INTEGER)))
->orderBy('requested_date', 'DESC')
->setMaxResults(1)
;
return $this->returnFirstOrNull($qb);
}
public function findLatests(): iterable
{
$qb = $this->createQueryBuilder();
$qb
->orderBy('requested_date', 'DESC')
->setMaxResults(20);
$stmt = $qb->executeQuery();
if ($stmt->rowCount() === 0) {
return [];
}
while (false !== ($row = $stmt->fetchAssociative())) {
yield Job::createFromArray($row);
}
}
public function findForScheduled(int $id): iterable
{
$qb = $this->createQueryBuilder();
$qb->andWhere($qb->expr()->eq('scheduled_dataflow_id', $qb->createNamedParameter($id, ParameterType::INTEGER)))
->orderBy('requested_date', 'DESC')
->setMaxResults(20);
$stmt = $qb->executeQuery();
if ($stmt->rowCount() === 0) {
return [];
}
while (false !== ($row = $stmt->fetchAssociative())) {
yield Job::createFromArray($row);
}
}
public function save(Job $job)
{
$datas = $job->toArray();
unset($datas['id']);
if (\is_array($datas['options'])) {
$datas['options'] = json_encode($datas['options'], \JSON_THROW_ON_ERROR);
}
if (\is_array($datas['exceptions'])) {
$datas['exceptions'] = json_encode($datas['exceptions'], \JSON_THROW_ON_ERROR);
}
if ($job->getId() === null) {
$this->connection->insert(static::TABLE_NAME, $datas, $this->getFields());
$job->setId((int) $this->connection->lastInsertId());
return;
}
$this->connection->update(static::TABLE_NAME, $datas, ['id' => $job->getId()], $this->getFields());
}
public function updateCount(int $jobId, int $count): void
{
$this->connection->update(static::TABLE_NAME, ['count' => $count], ['id' => $jobId]);
}
public function createQueryBuilder($alias = null): QueryBuilder
{
$qb = $this->connection->createQueryBuilder();
$qb->select('*')
->from(static::TABLE_NAME, $alias);
return $qb;
}
public function crashLongRunning(int $hours): void
{
$qb = $this->connection->createQueryBuilder();
$qb->update(static::TABLE_NAME, 'j')
->set('j.status', ':new_status')
->set('j.end_time', ':now')
->andWhere('j.status = :status')
->andWhere('j.start_time < :date')
->setParameter('status', Job::STATUS_RUNNING)
->setParameter('date', new \DateTime("- {$hours} hours"), 'datetime')
->setParameter('new_status', Job::STATUS_CRASHED)
->setParameter('now', new \DateTime(), 'datetime')
->executeStatement()
;
}
public function deleteOld(int $days): void
{
$qb = $this->connection->createQueryBuilder();
$qb->delete(static::TABLE_NAME, 'j')
->andWhere($qb->expr()->in('j.status', [Job::STATUS_COMPLETED, Job::STATUS_CRASHED]))
->andWhere('j.end_time < :date')
->setParameter('date', new \DateTime("- {$days} days"), 'datetime')
->executeStatement()
;
}
private function returnFirstOrNull(QueryBuilder $qb): ?Job
{
$stmt = $qb->executeQuery();
if ($stmt->rowCount() === 0) {
return null;
}
return Job::createFromArray($this->initDateTime($this->initArray($stmt->fetchAssociative())));
}
private function getFields(): array
{
return [
'id' => ParameterType::INTEGER,
'status' => ParameterType::INTEGER,
'label' => ParameterType::STRING,
'dataflow_type' => ParameterType::STRING,
'options' => ParameterType::STRING,
'requested_date' => 'datetime',
'scheduled_dataflow_id' => ParameterType::INTEGER,
'count' => ParameterType::INTEGER,
'exceptions' => ParameterType::STRING,
'start_time' => 'datetime',
'end_time' => 'datetime',
];
}
}