Completed
Push — master ( 38df09...7a08de )
by Daniel
08:33
created

RunCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 4
c 3
b 0
f 1
lcom 0
cbo 9
dl 0
loc 40
ccs 0
cts 24
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
A execute() 0 22 3
1
<?php
2
/**
3
 * This file is part of the Commander project.
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Commander\Console\Command;
9
10
use GravityMedia\Commander\Commander;
11
use GravityMedia\Commander\Process\OutputCallback;
12
use GravityMedia\Commander\TaskManager;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Process\Process;
16
17
/**
18
 * Run command class.
19
 *
20
 * @package GravityMedia\Commander\Console\Command
21
 */
22
class RunCommand extends Command
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected function configure()
28
    {
29
        parent::configure();
30
31
        $this
32
            ->setName('tasks:run')
33
            ->setDescription('Run tasks');
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    protected function execute(InputInterface $input, OutputInterface $output)
40
    {
41
        $config = $this->getCommanderConfig();
42
        $commander = new Commander($config);
43
        if (!$commander->isSchemaValid()) {
44
            $commander->updateSchema();
45
        }
46
47
        $entityManager = $commander->getEntityManager();
48
        $taskManager = new TaskManager($entityManager);
49
        $outputCallback = new OutputCallback($output, $commander->getLogger());
50
51
        while (null !== $task = $taskManager->findNextTask(['pid' => null])) {
52
            $process = new Process($task->getEntity()->getScript());
53
            $process->setTimeout($config->getCommandTimeout());
54
            $process->start();
55
56
            $task->updatePid($process->getPid());
57
            $process->wait($outputCallback);
58
            $task->updateExitCode($process->getExitCode());
59
        }
60
    }
61
}
62