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