|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\Manager; |
|
4
|
|
|
|
|
5
|
|
|
use Dtc\QueueBundle\Exception\UnsupportedException; |
|
6
|
|
|
use Dtc\QueueBundle\Model\Job; |
|
7
|
|
|
use Dtc\QueueBundle\Model\Run; |
|
8
|
|
|
|
|
9
|
|
|
class RunManager |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var string */ |
|
12
|
|
|
protected $runClass; |
|
13
|
|
|
|
|
14
|
41 |
|
public function __construct($runClass) |
|
15
|
|
|
{ |
|
16
|
41 |
|
$this->runClass = $runClass; |
|
17
|
41 |
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @return string |
|
21
|
|
|
*/ |
|
22
|
22 |
|
public function getRunClass() |
|
23
|
|
|
{ |
|
24
|
22 |
|
return $this->runClass; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $runClass |
|
29
|
|
|
*/ |
|
30
|
1 |
|
public function setRunClass($runClass) |
|
31
|
|
|
{ |
|
32
|
1 |
|
$this->runClass = $runClass; |
|
33
|
1 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param \DateTime $olderThan |
|
37
|
|
|
* |
|
38
|
|
|
* @return int Number of archived runs pruned |
|
39
|
|
|
* |
|
40
|
|
|
* @throws UnsupportedException |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function pruneArchivedRuns(\DateTime $olderThan) |
|
43
|
|
|
{ |
|
44
|
1 |
|
throw new UnsupportedException('not supported - '.$olderThan->getTimestamp()); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Prunes stalled runs. |
|
49
|
|
|
* |
|
50
|
|
|
* @return int Number of stalled runs pruned |
|
51
|
|
|
* |
|
52
|
|
|
* @throws UnsupportedException |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function pruneStalledRuns() |
|
55
|
|
|
{ |
|
56
|
1 |
|
throw new UnsupportedException('not supported'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param float $start |
|
61
|
|
|
* @param Job|null $job |
|
62
|
|
|
*/ |
|
63
|
3 |
|
public function recordHeartbeat(Run $run, $start, Job $job = null) |
|
64
|
|
|
{ |
|
65
|
3 |
|
$jobId = null; |
|
66
|
3 |
|
if (null !== $job) { |
|
67
|
3 |
|
$jobId = $job->getId(); |
|
68
|
3 |
|
} |
|
69
|
|
|
|
|
70
|
3 |
|
$run->setLastHeartbeatAt(new \DateTime()); |
|
71
|
3 |
|
$run->setCurrentJobId($jobId); |
|
72
|
3 |
|
$run->setElapsed(microtime(true) - $start); |
|
73
|
3 |
|
$this->persistRun($run); |
|
74
|
3 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param Run $run |
|
78
|
|
|
* @param string $action |
|
79
|
|
|
*/ |
|
80
|
5 |
|
protected function persistRun(Run $run, $action = 'persist') |
|
81
|
|
|
{ |
|
82
|
|
|
// To be overridden |
|
83
|
5 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param int $count |
|
87
|
|
|
*/ |
|
88
|
3 |
|
public function updateProcessed(Run $run, $count) |
|
89
|
|
|
{ |
|
90
|
3 |
|
$run->setProcessed($count); |
|
91
|
3 |
|
$this->persistRun($run); |
|
92
|
3 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Sets up the runManager (document / entity persister) if appropriate. |
|
96
|
|
|
* |
|
97
|
|
|
* @param float $start |
|
98
|
|
|
* @param int|null $maxCount |
|
99
|
|
|
* @param int|null $duration |
|
100
|
|
|
* @param int $processTimeout |
|
101
|
|
|
* |
|
102
|
|
|
* @return Run |
|
103
|
|
|
*/ |
|
104
|
5 |
|
public function runStart($start, $maxCount = null, $duration = null, $processTimeout = null) |
|
105
|
|
|
{ |
|
106
|
5 |
|
$runClass = $this->getRunClass(); |
|
107
|
|
|
/** @var Run $run */ |
|
108
|
5 |
|
$run = new $runClass(); |
|
109
|
5 |
|
$startDate = \DateTime::createFromFormat('U.u', $formattedStart = number_format($start, 6, '.', '')); |
|
110
|
5 |
|
if (false === $startDate) { |
|
111
|
|
|
throw new \RuntimeException("Could not create date from $start formatted as $formattedStart"); |
|
112
|
|
|
} |
|
113
|
5 |
|
$run->setLastHeartbeatAt($startDate); |
|
114
|
5 |
|
$run->setStartedAt($startDate); |
|
115
|
5 |
|
if (null !== $maxCount) { |
|
116
|
3 |
|
$run->setMaxCount($maxCount); |
|
117
|
3 |
|
} |
|
118
|
5 |
|
if (null !== $duration) { |
|
119
|
2 |
|
$run->setDuration($duration); |
|
120
|
2 |
|
} |
|
121
|
5 |
|
$run->setHostname(gethostname()); |
|
122
|
5 |
|
$run->setPid(getmypid()); |
|
123
|
5 |
|
$run->setProcessed(0); |
|
124
|
5 |
|
$run->setProcessTimeout($processTimeout); |
|
125
|
5 |
|
$this->persistRun($run); |
|
126
|
|
|
|
|
127
|
5 |
|
return $run; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param Run $run |
|
132
|
|
|
* @param int|null $start |
|
133
|
|
|
*/ |
|
134
|
5 |
|
public function runStop(Run $run, $start) |
|
135
|
|
|
{ |
|
136
|
5 |
|
$end = microtime(true); |
|
137
|
5 |
|
$endedTime = \DateTime::createFromFormat('U.u', $end); |
|
138
|
5 |
|
if ($endedTime) { |
|
139
|
5 |
|
$run->setEndedAt($endedTime); |
|
140
|
5 |
|
} |
|
141
|
5 |
|
$run->setElapsed($end - $start); |
|
142
|
5 |
|
$this->persistRun($run, 'remove'); |
|
143
|
5 |
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|