|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author jan huang <[email protected]> |
|
4
|
|
|
* @copyright 2017 |
|
5
|
|
|
* |
|
6
|
|
|
* @see https://www.github.com/janhuang |
|
7
|
|
|
* @see https://fastdlabs.com |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace FastD\Console; |
|
11
|
|
|
|
|
12
|
|
|
use FastD\Swoole\Process; |
|
13
|
|
|
use Symfony\Component\Console\Command\Command; |
|
14
|
|
|
use Symfony\Component\Console\Helper\Table; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class Process. |
|
22
|
|
|
*/ |
|
23
|
|
|
class Processor extends Command |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* php bin/console process {name} {args} {options}. |
|
27
|
|
|
*/ |
|
28
|
|
|
protected function configure() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->setName('process'); |
|
31
|
|
|
$this->addArgument('process', InputArgument::OPTIONAL); |
|
32
|
|
|
$this->addOption('pid', '-p', InputOption::VALUE_OPTIONAL, 'set process pid path.'); |
|
33
|
|
|
$this->addOption('name', null, InputOption::VALUE_OPTIONAL, 'set process name.', app()->getName()); |
|
34
|
|
|
$this->addOption('daemon', '-d', InputOption::VALUE_NONE, 'set process daemonize.'); |
|
35
|
|
|
$this->addOption('list', '-l', InputOption::VALUE_NONE, 'show all processes.'); |
|
36
|
|
|
$this->setDescription('Create new processor.'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param InputInterface $input |
|
41
|
|
|
* @param OutputInterface $output |
|
42
|
|
|
* |
|
43
|
|
|
* @return int |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
46
|
|
|
{ |
|
47
|
|
|
$process = $input->getArgument('process'); |
|
48
|
|
|
|
|
49
|
|
|
if ($input->hasParameterOption(['--list', '-l']) || empty($process)) { |
|
50
|
|
|
return $this->showProcesses($input, $output); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$processes = config()->get('processes', []); |
|
54
|
|
|
|
|
55
|
|
|
if (!isset($processes[$process])) { |
|
56
|
|
|
throw new \RuntimeException(sprintf('Process %s cannot found', $process)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$processor = $processes[$process]; |
|
60
|
|
|
if (!class_exists($processor)) { |
|
61
|
|
|
throw new \RuntimeException(sprintf('Class "%s" is not found.', $process)); |
|
62
|
|
|
} |
|
63
|
|
|
$name = $input->getOption('name'); |
|
64
|
|
|
$process = new $processor($name); |
|
65
|
|
|
if (!($process instanceof Process)) { |
|
66
|
|
|
throw new \RuntimeException('Process must be instance of \FastD\Swoole\Process'); |
|
67
|
|
|
} |
|
68
|
|
|
if ($input->hasParameterOption(['--daemon', '-d'])) { |
|
69
|
|
|
$process->daemon(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$path = $this->targetDirectory($input); |
|
73
|
|
|
$file = $path.'/'.$name.'.pid'; |
|
74
|
|
|
|
|
75
|
|
|
$pid = $process->start(); |
|
76
|
|
|
file_put_contents($file, $pid); |
|
77
|
|
|
|
|
78
|
|
|
$output->writeln(sprintf('Process %s is started, pid: %s', $name, $pid)); |
|
79
|
|
|
$output->writeln(sprintf('Pid file save is %s', $file)); |
|
80
|
|
|
|
|
81
|
|
|
return $pid; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param InputInterface $input |
|
86
|
|
|
* |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function targetDirectory(InputInterface $input) |
|
90
|
|
|
{ |
|
91
|
|
|
$pid = $input->getParameterOption(['--path', '-p']); |
|
92
|
|
|
|
|
93
|
|
|
if (empty($pid)) { |
|
94
|
|
|
$path = app()->getPath().'/runtime/process'; |
|
95
|
|
|
} else { |
|
96
|
|
|
$path = dirname($pid); |
|
97
|
|
|
} |
|
98
|
|
|
if (!file_exists($path)) { |
|
99
|
|
|
mkdir($path, 0755, true); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $path; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param InputInterface $input |
|
107
|
|
|
* @param OutputInterface $output |
|
108
|
|
|
* |
|
109
|
|
|
* @return int |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function showProcesses(InputInterface $input, OutputInterface $output) |
|
|
|
|
|
|
112
|
|
|
{ |
|
113
|
|
|
$table = new Table($output); |
|
114
|
|
|
$table->setHeaders(array('Process', 'Pid', 'Status', 'Start At', 'Runtime')); |
|
115
|
|
|
$rows = []; |
|
116
|
|
|
foreach (config()->get('processes', []) as $name => $processor) { |
|
117
|
|
|
$rows[] = [ |
|
118
|
|
|
$name, |
|
119
|
|
|
'', |
|
120
|
|
|
'', |
|
121
|
|
|
'', |
|
122
|
|
|
]; |
|
123
|
|
|
} |
|
124
|
|
|
$table->setRows($rows); |
|
125
|
|
|
$table->render(); |
|
126
|
|
|
|
|
127
|
|
|
return 0; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.