Completed
Push — master ( d448ac...513048 )
by Matthew
31:40 queued 17:02
created

RunManager::runStart()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4.0027

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 17
cts 18
cp 0.9444
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 18
nc 5
nop 4
crap 4.0027
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 42
    public function __construct($runClass)
15
    {
16 42
        $this->runClass = $runClass;
17 42
    }
18
19
    /**
20
     * @return string
21
     */
22 20
    public function getRunClass()
23
    {
24 20
        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
        }
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 3
    protected function persistRun(Run $run, $action = 'persist')
81
    {
82
        // To be overridden
83 3
    }
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 3
    public function runStart($start, $maxCount = null, $duration = null, $processTimeout = null)
105
    {
106 3
        $runClass = $this->getRunClass();
107
        /** @var Run $run */
108 3
        $run = new $runClass();
109 3
        $startDate = \DateTime::createFromFormat('U.u', $formattedStart = number_format($start, 6, '.', ''));
110 3
        if (false === $startDate) {
111
            throw new \RuntimeException("Could not create date from $start formatted as $formattedStart");
112
        }
113 3
        $run->setLastHeartbeatAt($startDate);
114 3
        $run->setStartedAt($startDate);
115 3
        if (null !== $maxCount) {
116 3
            $run->setMaxCount($maxCount);
117
        }
118 3
        if (null !== $duration) {
119 2
            $run->setDuration($duration);
120
        }
121 3
        $run->setHostname(gethostname());
122 3
        $run->setPid(getmypid());
123 3
        $run->setProcessed(0);
124 3
        $run->setProcessTimeout($processTimeout);
125 3
        $this->persistRun($run);
126
127 3
        return $run;
128
    }
129
130
    /**
131
     * @param Run      $run
132
     * @param int|null $start
133
     */
134 3
    public function runStop(Run $run, $start)
135
    {
136 3
        $end = microtime(true);
137 3
        $endedTime = \DateTime::createFromFormat('U.u', $end);
138 3
        if ($endedTime) {
139 3
            $run->setEndedAt($endedTime);
140
        }
141 3
        $run->setElapsed($end - $start);
142 3
        $this->persistRun($run, 'remove');
143 3
    }
144
}
145