QueueProcessor::process()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 4
nc 3
nop 0
1
<?php
2
3
namespace Glooby\TaskBundle\Queue;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Glooby\TaskBundle\Model\QueuedTaskInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Process\Process;
9
10
/**
11
 * @author Emil Kilhage
12
 */
13
class QueueProcessor
14
{
15
    /**
16
     * @var int
17
     */
18
    private $limit;
19
20
    /**
21
     * @var bool
22
     */
23
    private $debug;
24
25
    /**
26
     * @var Process[]
27
     */
28
    private $processes = [];
29
30
    /**
31
     * @var OutputInterface
32
     */
33
    protected $output;
34
35
    /**
36
     * @var ManagerRegistry
37
     */
38
    protected $doctrine;
39
40
    /**
41
     * @param ManagerRegistry $doctrine
42
     */
43
    public function setDoctrine($doctrine)
44
    {
45
        $this->doctrine = $doctrine;
46
    }
47
48
    /**
49
     * @param OutputInterface $output
50
     */
51
    public function setOutput(OutputInterface $output)
52
    {
53
        $this->output = $output;
54
    }
55
56
    /**
57
     * @param boolean $debug
58
     */
59
    public function setDebug($debug)
60
    {
61
        $this->debug = $debug;
62
    }
63
64
    /**
65
     * @param int $limit
66
     */
67
    public function setLimit($limit)
68
    {
69
        $this->limit = $limit;
70
    }
71
72
    /**
73
     * @throws \Exception
74
     */
75
    public function process()
76
    {
77
        $queueRepo = $this->doctrine->getManager()
78
            ->getRepository('GloobyTaskBundle:QueuedTask');
79
80
        $started = [];
81
        foreach ($queueRepo->findQueued($this->limit) as $queuedTask) {
82
            if (!$queueRepo->isRunning($queuedTask->getName()) && !in_array($queuedTask->getName(), $started)) {
83
                $started[] = $queuedTask->getName();
84
                $this->start($queuedTask);
85
            }
86
        }
87
88
        $this->wait();
89
    }
90
91
    /**
92
     * @return string
93
     */
94
    private function getProcessParams()
95
    {
96
        $params = [];
97
98
        if (!$this->debug) {
99
            $params[] = '--env=prod';
100
        }
101
102
        return implode(' ', $params);
103
    }
104
105
    /**
106
     *
107
     */
108
    private function wait()
109
    {
110
        while (count($this->processes) > 0) {
111
            sleep(1);
112
113
            foreach ($this->processes as $i => $process) {
114
                if (!$process->isRunning()) {
115
                    unset($this->processes[$i]);
116
                    echo $process->getOutput();
117
                }
118
            }
119
        }
120
    }
121
122
    /**
123
     * @param QueuedTaskInterface $queuedTask
124
     */
125
    private function start(QueuedTaskInterface $queuedTask)
126
    {
127
        $command = $this->createCommand($queuedTask);
128
        $process = $this->createProcess($command);
129
130
        $this->processes[] = $process;
131
132
        if (null !== $this->output) {
133
            $this->output->writeln("$command");
134
        }
135
    }
136
137
    /**
138
     * @param string $command
139
     * @return Process
140
     */
141
    private function createProcess($command)
142
    {
143
        $that = $this;
0 ignored issues
show
Unused Code introduced by
$that is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
144
        $nl = false;
0 ignored issues
show
Unused Code introduced by
$nl is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
145
146
        $process = new Process($command);
147
        $process->setTimeout(0);
148
        $process->start();
149
150
        return $process;
151
    }
152
153
    /**
154
     * @param QueuedTaskInterface $queuedTask
155
     * @return string
156
     */
157
    private function createCommand(QueuedTaskInterface $queuedTask)
158
    {
159
        $command = sprintf(
160
            '%s -d memory_limit=%s bin/console task:run --id=%s %s',
161
            exec("readlink -f /proc/".posix_getpid()."/exe"),
162
            ini_get('memory_limit'),
163
            $queuedTask->getId(),
164
            $this->getProcessParams()
165
        );
166
167
        return $command;
168
    }
169
}
170