protected function execute(InputInterface $input, OutputInterface $output) { try { if (!$input->getOption('allow-root')) { $aProcessUser = posix_getpwuid(posix_geteuid()); if ($aProcessUser['name'] == 'root') { throw new \InvalidArgumentException('You can however run a command with ' . 'sudo using --allow-root option'); } } DaemonUtils::configCheck(); //Check daemons $daemons = DaemonUtils::getDaemonsFromConfig(); foreach ($daemons as $daemon) { if (!$daemon instanceof DaemonizerInterface) { throw new \Exception('Invalid [cli-daemonizer.php] file: file contain not-implementer ' . 'DaemonizerInterface class'); } DaemonUtils::checkScheduleItem($daemon->getSchedule(), get_class($daemon)); } $id = mt_rand(1, 100000); shell_exec(DAEMON_FILE . " internal:master --id={$id} > /dev/null 2>&1 &"); $output->writeln("<info>Daemon started</info>"); } catch (\Exception $e) { $output->writeln('<error>' . $e->getMessage() . '</error>'); } }
protected function execute(InputInterface $input, OutputInterface $output) { try { $output->writeln("<info>Daemon stopped...</info>"); $devNull = "> /dev/null 2>&1"; list($iPid, $idKey) = DaemonUtils::checkPidFile(); if ($iPid == 0) { throw new \Exception('Daemon not running'); } shell_exec('kill -15 ' . $iPid . " {$devNull}"); sleep(5); $aProcess = explode(PHP_EOL, shell_exec("ps -ef | grep " . escapeshellarg(DAEMON_FILE . " internal:master --id={$idKey}") . " | grep -v grep | awk '{print \$2}'")); foreach ($aProcess as $iProcessId) { if ($iProcessId = (int) $iProcessId) { shell_exec('kill -9 ' . $iProcessId . " {$devNull}"); } } sleep(5); shell_exec('kill -9 ' . $iPid . " {$devNull}"); @unlink(DaemonUtils::getPidFilename()); $output->writeln("<info>Daemon shutdown complete</info>"); } catch (\Exception $e) { $output->writeln('<error>' . $e->getMessage() . '</error>'); } }
public function testConfigChecker() { $this->assertEquals(false, \Brainfit\Daemonizer\DaemonUtils::checkScheduleItem('* * * * *')); $this->assertEquals(false, \Brainfit\Daemonizer\DaemonUtils::checkScheduleItem('59 * * * *')); $this->assertEquals(false, \Brainfit\Daemonizer\DaemonUtils::checkScheduleItem(array(0, 1, 2, 3, 4, 5, 10, 12, 15, 19, 23))); $this->assertEquals(false, \Brainfit\Daemonizer\DaemonUtils::checkScheduleItem(1)); $this->assertEquals(false, \Brainfit\Daemonizer\DaemonUtils::checkScheduleItem()); $this->assertNotEquals(false, \Brainfit\Daemonizer\DaemonUtils::checkScheduleItem('* * * * * 1999')); $this->assertNotEquals(false, \Brainfit\Daemonizer\DaemonUtils::checkScheduleItem('60 * * * *')); $this->assertNotEquals(false, \Brainfit\Daemonizer\DaemonUtils::checkScheduleItem('61 * * * *')); $this->assertNotEquals(false, \Brainfit\Daemonizer\DaemonUtils::checkScheduleItem(array(1, 25))); $this->assertNotEquals(false, \Brainfit\Daemonizer\DaemonUtils::checkScheduleItem(array(-1, 25))); }
public static function configCheck() { if (!defined('DAEMON_FILE') || !DAEMON_FILE || !file_exists(DAEMON_FILE)) { throw new \RuntimeException('Not defined DAEMON_FILE'); } if (!defined('DAEMON_CWD') || !DAEMON_CWD) { throw new \RuntimeException('Not defined DAEMON_CWD'); } if (DaemonUtils::checkPidFile() !== false) { throw new \Exception('Daemon already running'); } //writable-test @file_put_contents(self::getPidFilename(), 'test'); if (@file_get_contents(self::getPidFilename()) != 'test') { throw new \RuntimeException('Pid file is not writable [' . self::getPidFilename() . ']'); } @unlink(self::getPidFilename()); }
protected function execute(InputInterface $input, OutputInterface $output) { try { $id = $input->getOption('id'); DaemonUtils::configCheck(); $daemons = DaemonUtils::getDaemonsFromConfig(); file_put_contents(DaemonUtils::getPidFilename(), getmypid() . ',' . $id); $loop = \React\EventLoop\Factory::create(); $master = new MasterController($loop, new ChildController()); foreach ($daemons as $daemon) { if (!$daemon instanceof DaemonizerInterface) { throw new \Exception('Invalid [cli-daemonizer.php] file: file contain not-implementer ' . 'DaemonizerInterface class'); } $master->attach($daemon); } $master->start(); $loop->run(); } catch (\Exception $e) { $output->writeln('<error>' . $e->getMessage() . '</error>'); } }