Completed
Pull Request — master (#30)
by Matthew
07:24
created

RunManager::pruneStalledRuns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 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', number_format($start, 6, '.', ''));
97 3
        $run->setLastHeartbeatAt($startDate);
0 ignored issues
show
Security Bug introduced by
It seems like $startDate defined by \DateTime::createFromFor...at($start, 6, '.', '')) on line 96 can also be of type false; however, Dtc\QueueBundle\Model\Run::setLastHeartbeatAt() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

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 returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
98 3
        $run->setStartedAt($startDate);
0 ignored issues
show
Security Bug introduced by
It seems like $startDate defined by \DateTime::createFromFor...at($start, 6, '.', '')) on line 96 can also be of type false; however, Dtc\QueueBundle\Model\Run::setStartedAt() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

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 returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
99 3
        if (null !== $maxCount) {
100 3
            $run->setMaxCount($maxCount);
101
        }
102 3
        if (null !== $duration) {
103 2
            $run->setDuration($duration);
104
        }
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)
119
    {
120 3
        $end = microtime(true);
121 3
        $endedTime = \DateTime::createFromFormat('U.u', $end);
122 3
        if ($endedTime) {
123 3
            $run->setEndedAt($endedTime);
124
        }
125 3
        $run->setElapsed($end - $start);
126 3
        $this->persistRun($run, 'remove');
127 3
    }
128
}
129