SAREhub/php_component_worker

View on GitHub
src/SAREhub/Component/Worker/Cli/StartManagerCommand.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace SAREhub\Component\Worker\Cli;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class StartManagerCommand extends CliCommand {
    
    /**
     * @var SystemdHelper
     */
    private $systemdHelper;
    
    
    /**
     * @return StartManagerCommand
     */
    public static function newInstance() {
        return new self();
    }
    
    /**
     * @param SystemdHelper $helper
     * @return $this
     */
    public function withSystemdHelper(SystemdHelper $helper) {
        $this->systemdHelper = $helper;
        return $this;
    }
    
    protected function configure() {
        $this->setName('start-manager')
          ->setDescription('Starts selected worker manager')
          ->setHelp("Starts worker manager with config from file. Check example config: cli/workerManagerConfigExample.php ")
          ->addArgument('manager', InputArgument::REQUIRED, 'manager id to start');
    }
    
    protected function execute(InputInterface $input, OutputInterface $output) {
        $managerId = $input->getArgument('manager');
        if ($this->getCli()->isManagerConfigFileExists($managerId)) {
            $configPath = $this->getCli()->getManagerConfigFilePath($managerId);
            $this->getLogger()->info('starting manager with config ', ['config' => $configPath]);
            $output->writeln('starting manager with config: '.$configPath);
            
            $unitName = $this->getManagerUnitInstanceName($managerId);
            $this->getLogger()->info('manager unit instance name: ', ['unit' => $unitName]);
            $output->write('<info>manager instance unit name: </info>'.$unitName.' ');
            
            $return = $this->systemdHelper->start($unitName);
            if (!empty($return)) {
                $this->getLogger()->info('systemd start output: '.$return);
                $output->writeln('<error>systemd start output: </error>'.$return);
            } else {
                $output->writeln('<info>started</info>');
            }
        } else {
            $configPath = $this->getCli()->getManagerConfigFilePath($managerId);
            $output->writeln("<error>manager config file isn't exists</error>");
            $this->getLogger()->warning("config file isn't exists", ['config' => $configPath]);
        }
    }
    
    private function getManagerUnitInstanceName($managerId) {
        return 'worker-manager@'.$managerId.'.service';
    }
}