Completed
Pull Request — master (#30)
by Matthew
23:29 queued 08:10
created

RunManager   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 84.91%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 3
dl 0
loc 123
ccs 45
cts 53
cp 0.8491
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setRunClass() 0 4 1
A pruneArchivedRuns() 0 4 1
A pruneStalledRuns() 0 4 1
A getRunClass() 0 4 1
A recordHeartbeat() 0 12 2
A updateProcessed() 0 5 1
A persistRun() 0 4 1
B runStart() 0 25 4
A runStop() 0 10 2
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 37
    public function __construct($runClass)
15
    {
16 37
        $this->runClass = $runClass;
17 37
    }
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)
31
    {
32
        $this->runClass = $runClass;
33
    }
34
35
    /**
36
     * @param \DateTime $olderThan
37
     *
38
     * @return int Number of archived runs pruned
39
     */
40
    public function pruneArchivedRuns(\DateTime $olderThan)
41
    {
42
        throw new UnsupportedException('not supported');
43
    }
44
45
    public function pruneStalledRuns()
46
    {
47
        throw new UnsupportedException('not supported');
48
    }
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
        }
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)
76
    {
77 3
        $run->setProcessed($count);
78 3
        $this->persistRun($run);
79 3
    }
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', $formattedStart = number_format($start, 6, '.', ''));
97 3
        if (false === $startDate) {
98
            throw new \RuntimeException("Could not create date from $start formatted as $formattedStart");
99
        }
100 3
        $run->setLastHeartbeatAt($startDate);
101 3
        $run->setStartedAt($startDate);
102 3
        if (null !== $maxCount) {
103 3
            $run->setMaxCount($maxCount);
104
        }
105 3
        if (null !== $duration) {
106 2
            $run->setDuration($duration);
107
        }
108 3
        $run->setHostname(gethostname());
109 3
        $run->setPid(getmypid());
110 3
        $run->setProcessed(0);
111 3
        $run->setProcessTimeout($processTimeout);
112 3
        $this->persistRun($run);
113
114 3
        return $run;
115
    }
116
117
    /**
118
     * @param Run      $run
119
     * @param int|null $start
120
     */
121 3
    public function runStop(Run $run, $start)
122
    {
123 3
        $end = microtime(true);
124 3
        $endedTime = \DateTime::createFromFormat('U.u', $end);
125 3
        if ($endedTime) {
126 3
            $run->setEndedAt($endedTime);
127
        }
128 3
        $run->setElapsed($end - $start);
129 3
        $this->persistRun($run, 'remove');
130 3
    }
131
}
132