1 | <?php |
||
9 | class RunManager |
||
10 | { |
||
11 | /** @var string */ |
||
12 | protected $runClass; |
||
13 | |||
14 | 37 | public function __construct($runClass) |
|
18 | |||
19 | /** |
||
20 | * @return string |
||
21 | */ |
||
22 | 18 | public function getRunClass() |
|
23 | { |
||
24 | 18 | return $this->runClass; |
|
25 | } |
||
26 | |||
27 | /** |
||
28 | * @param string $runClass |
||
29 | */ |
||
30 | public function setRunClass($runClass) |
||
34 | |||
35 | /** |
||
36 | * @param \DateTime $olderThan |
||
37 | * |
||
38 | * @return int Number of archived runs pruned |
||
39 | */ |
||
40 | public function pruneArchivedRuns(\DateTime $olderThan) |
||
44 | |||
45 | public function pruneStalledRuns() |
||
49 | |||
50 | /** |
||
51 | * @param float $start |
||
52 | * @param Job|null $job |
||
53 | */ |
||
54 | 3 | public function recordHeartbeat(Run $run, $start, Job $job = null) |
|
55 | { |
||
56 | 3 | $jobId = null; |
|
57 | 3 | if (null !== $job) { |
|
58 | 3 | $jobId = $job->getId(); |
|
59 | 3 | } |
|
60 | |||
61 | 3 | $run->setLastHeartbeatAt(new \DateTime()); |
|
62 | 3 | $run->setCurrentJobId($jobId); |
|
63 | 3 | $run->setElapsed(microtime(true) - $start); |
|
64 | 3 | $this->persistRun($run); |
|
65 | 3 | } |
|
66 | |||
67 | 1 | protected function persistRun(Run $run, $action = 'persist') |
|
68 | { |
||
69 | // To be overridden |
||
70 | 1 | } |
|
71 | |||
72 | /** |
||
73 | * @param int $count |
||
74 | */ |
||
75 | 3 | public function updateProcessed(Run $run, $count) |
|
80 | |||
81 | /** |
||
82 | * Sets up the runManager (document / entity persister) if appropriate. |
||
83 | * |
||
84 | * @param float $start |
||
85 | * @param int|null $maxCount |
||
86 | * @param int|null $duration |
||
87 | * @param int $processTimeout |
||
88 | * |
||
89 | * @return Run |
||
90 | */ |
||
91 | 3 | public function runStart($start, $maxCount = null, $duration = null, $processTimeout = null) |
|
92 | { |
||
93 | 3 | $runClass = $this->getRunClass(); |
|
94 | /** @var Run $run */ |
||
95 | 3 | $run = new $runClass(); |
|
96 | 3 | $startDate = \DateTime::createFromFormat('U.u', number_format($start, 6, '.', '')); |
|
97 | 3 | $run->setLastHeartbeatAt($startDate); |
|
|
|||
98 | 3 | $run->setStartedAt($startDate); |
|
99 | 3 | if (null !== $maxCount) { |
|
100 | 3 | $run->setMaxCount($maxCount); |
|
101 | 3 | } |
|
102 | 3 | if (null !== $duration) { |
|
103 | 2 | $run->setDuration($duration); |
|
104 | 2 | } |
|
105 | 3 | $run->setHostname(gethostname()); |
|
106 | 3 | $run->setPid(getmypid()); |
|
107 | 3 | $run->setProcessed(0); |
|
108 | 3 | $run->setProcessTimeout($processTimeout); |
|
109 | 3 | $this->persistRun($run); |
|
110 | |||
111 | 3 | return $run; |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * @param Run $run |
||
116 | * @param int|null $start |
||
117 | */ |
||
118 | 3 | public function runStop(Run $run, $start) |
|
128 | } |
||
129 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.