- Notifications
You must be signed in to change notification settings - Fork370
Implement daemon mode#351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:master
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| <?php | ||
| namespace PHPPM\Commands; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Output\NullOutput; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
| use Symfony\Component\Console\Output\StreamOutput; | ||
| trait DaemonTrait | ||
| { | ||
| /** | ||
| * Split into a new process. Returns true when it's the child process and false | ||
| * for the parent process. | ||
| * | ||
| * Borrowed from https://github.com/phlib/console-process/blob/master/src/Command/DaemonCommand.php | ||
| * | ||
| * @return bool | ||
| */ | ||
| private function daemonize() | ||
| { | ||
| // prevent permission issues | ||
| umask(0); | ||
| $pid = pcntl_fork(); | ||
| if ($pid == -1) { | ||
| /* fork failed */ | ||
| throw new \RuntimeException('Failed to fork the daemon.'); | ||
| } elseif ($pid) { | ||
| /* close the parent */ | ||
| return false; | ||
| } | ||
| // make ourselves the session leader | ||
| if (posix_setsid() == -1) { | ||
| throw new \RuntimeException('Failed to become a daemon.'); | ||
| } | ||
| return true; | ||
| } | ||
| /** | ||
| * Borrowed from https://github.com/phlib/console-process/blob/master/src/Command/DaemonCommand.php | ||
| * @param InputInterface $input | ||
| * @return InputInterface | ||
| */ | ||
| private function recreateInput(InputInterface $input) | ||
| { | ||
| return clone $input; | ||
| } | ||
| /** | ||
| * Borrowed from https://github.com/phlib/console-process/blob/master/src/Command/DaemonCommand.php | ||
| * @param OutputInterface $output | ||
| * @return OutputInterface | ||
| */ | ||
| private function recreateOutput(OutputInterface $output, $logfile) | ||
| { | ||
| $verbosityLevel = $output->getVerbosity(); | ||
| if ($logfile) { | ||
| $newInstance = new StreamOutput(fopen($logfile, 'a')); | ||
| } else { | ||
| $newInstance = new NullOutput(); | ||
| } | ||
| $newInstance->setVerbosity($verbosityLevel); | ||
| return $newInstance; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -6,11 +6,13 @@ | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputArgument; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
| class StartCommand extends Command | ||
| { | ||
| use ConfigTrait; | ||
| use DaemonTrait; | ||
| /** | ||
| * {@inheritdoc} | ||
| @@ -30,6 +32,20 @@ protected function configure() | ||
| protected function execute(InputInterface $input, OutputInterface $output) | ||
| { | ||
| if ($input->getOption('daemonize')) { | ||
| $isChild = $this->daemonize(); | ||
| if (!$isChild) { | ||
| if ($output->isVerbose()) { | ||
| $output->writeln('Daemonized'); | ||
| } | ||
| return null; | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Asthis project supports Symfony 5, the return value of a commands | ||
| } | ||
| // children shouldn't hold onto parents input/output | ||
| $input = $this->recreateInput($input); | ||
| $output = $this->recreateOutput($output, $input->getOption('logfile')); | ||
| } | ||
| $config = $this->initializeConfig($input, $output); | ||
| $handler = new ProcessManager($output, $config['port'], $config['host'], $config['workers']); | ||