Completed
Push — master ( 659ca0...8da12a )
by Sébastien
13:12 queued 02:21
created

PjbServerStatusCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
crap 1
1
<?php
2
3
namespace PjbServer\Tools\Console\Command;
4
5
use PjbServer\Tools\Exception as PjbException;
6
use PjbServer\Tools\StandaloneServer;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class PjbServerStatusCommand extends Command
13
{
14
    use LoggerTrait;
15
16
    /**
17
     * @var StandaloneServer
18
     */
19
    protected $server;
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    protected function configure()
25
    {
26
        $this->setName('pjbserver:status')
27 7
             ->setDescription(
28
                 'Get the status of the standalone pjb server (java)'
29 7
               )
30 7
            ->addArgument(
31
                'config-file',
32 7
                InputArgument::REQUIRED,
33 7
                'Configuration file, see ./dist/pjbserver.config.php.dist'
34 7
            )
35 7
36
             ->setHelp(<<<'EOT'
37 7
Get the status of the php java bridge server in the background.
38
EOT
39 7
        );
40
    }
41
42 7
    protected function execute(InputInterface $input, OutputInterface $output)
43 7
    {
44
        $logger = $this->getConsoleLogger($output);
45 2
46
        $file = $input->getArgument('config-file');
47 2
48
        // Test if config file exists
49 2
        if (!file_exists($file) || !is_readable($file)) {
50
            $msg = "Configuration file '$file' does not exists or is not readable'";
51
            throw new \InvalidArgumentException($msg);
52 2
        }
53
54
        $params = include $file;
55
        $port = $params['port'];
56
57 2
        $config = new StandaloneServer\Config($params);
58 2
        $this->logServerConfig($logger, $config);
59
        $this->server = new StandaloneServer($config, $logger);
60 2
61 2
        $pid_file = $config->getPidFile();
62 2
        $log_file = $config->getLogFile();
63
64 2
        if (file_exists($log_file)) {
65 2
            $output->writeln('---------------------------------------------------------');
66
            $output->writeln("Content of log file ($log_file)");
67 2
            $output->writeln('---------------------------------------------------------');
68 1
            $output->writeln($this->server->getOutput());
69 1
            $output->writeln('---------------------------------------------------------');
70 1
        }
71 1
72 1
        $isRunning = false;
73 1
        try {
74
            $pid = $this->server->getPid();
75
76 2
            if (!$this->server->isProcessRunning()) {
77
                $logger->error("Not running but pid file exists ($pid_file) and pid found ($pid)");
78 2
                $msg = "Server not running but pid exists (pid: $pid) in pid_file ($pid_file). Please restart.";
79
            } else {
80 1
                $isRunning = true;
81
                $msg = "Server is running on port '$port' (pid: $pid)";
82
            }
83
        } catch (PjbException\PidCorruptedException $e) {
84 1
            // Pid file corrupted
85 1
            $logger->critical("Cannot find server pid, your '$pid_file' is corrupted. Remove it.");
86
            $msg = 'Server not running (Critical error: pid file corrupted)';
87 2
        } catch (PjbException\PidNotFoundException $e) {
88
            $logger->info("Pid file '$pid_file' not exists, assuming server is down.");
89
            $msg = "Server not running on port '$port' (no pid file found)";
90
        } catch (\Exception $e) {
91 1
            $logger->error('Unexpected exception when testing pid file.');
92 1
            $msg = 'Cannot test server status';
93 1
        }
94 1
95
        $output->writeln($msg);
96
97
        return ($isRunning) ? 0 : 1;
98
    }
99
}
100