Differential D9497 Diff 22974 src/applications/daemon/management/PhabricatorDaemonManagementWorkflow.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/daemon/management/PhabricatorDaemonManagementWorkflow.php
<?php | <?php | ||||
abstract class PhabricatorDaemonManagementWorkflow | abstract class PhabricatorDaemonManagementWorkflow | ||||
extends PhabricatorManagementWorkflow { | extends PhabricatorManagementWorkflow { | ||||
protected function loadAvailableDaemonClasses() { | protected final function loadAvailableDaemonClasses() { | ||||
$loader = new PhutilSymbolLoader(); | $loader = new PhutilSymbolLoader(); | ||||
return $loader | return $loader | ||||
->setAncestorClass('PhutilDaemon') | ->setAncestorClass('PhutilDaemon') | ||||
->setConcreteOnly(true) | ->setConcreteOnly(true) | ||||
->selectSymbolsWithoutLoading(); | ->selectSymbolsWithoutLoading(); | ||||
} | } | ||||
public function getPIDDirectory() { | protected final function getPIDDirectory() { | ||||
$path = PhabricatorEnv::getEnvConfig('phd.pid-directory'); | $path = PhabricatorEnv::getEnvConfig('phd.pid-directory'); | ||||
return $this->getControlDirectory($path); | return $this->getControlDirectory($path); | ||||
} | } | ||||
public function getLogDirectory() { | protected final function getLogDirectory() { | ||||
$path = PhabricatorEnv::getEnvConfig('phd.log-directory'); | $path = PhabricatorEnv::getEnvConfig('phd.log-directory'); | ||||
return $this->getControlDirectory($path); | return $this->getControlDirectory($path); | ||||
} | } | ||||
private function getControlDirectory($path) { | private function getControlDirectory($path) { | ||||
if (!Filesystem::pathExists($path)) { | if (!Filesystem::pathExists($path)) { | ||||
list($err) = exec_manual('mkdir -p %s', $path); | list($err) = exec_manual('mkdir -p %s', $path); | ||||
if ($err) { | if ($err) { | ||||
throw new Exception( | throw new Exception( | ||||
"phd requires the directory '{$path}' to exist, but it does not ". | "phd requires the directory '{$path}' to exist, but it does not ". | ||||
"exist and could not be created. Create this directory or update ". | "exist and could not be created. Create this directory or update ". | ||||
"'phd.pid-directory' / 'phd.log-directory' in your configuration ". | "'phd.pid-directory' / 'phd.log-directory' in your configuration ". | ||||
"to point to an existing directory."); | "to point to an existing directory."); | ||||
} | } | ||||
} | } | ||||
return $path; | return $path; | ||||
} | } | ||||
public function loadRunningDaemons() { | protected final function loadRunningDaemons() { | ||||
$results = array(); | $results = array(); | ||||
$ids = array(); | |||||
$pid_dir = $this->getPIDDirectory(); | $pid_dir = $this->getPIDDirectory(); | ||||
$pid_files = Filesystem::listDirectory($pid_dir); | $pid_files = Filesystem::listDirectory($pid_dir); | ||||
if (!$pid_files) { | if (!$pid_files) { | ||||
return $results; | return $results; | ||||
} | } | ||||
foreach ($pid_files as $pid_file) { | foreach ($pid_files as $pid_file) { | ||||
$pid_data = Filesystem::readFile($pid_dir.'/'.$pid_file); | $results[] = PhabricatorDaemonReference::newFromDictionary( | ||||
epriestley: Unused. | |||||
$dict = json_decode($pid_data, true); | $pid_dir.'/'.$pid_file); | ||||
epriestleyUnsubmitted Not Done Inline ActionsNot an array / file is not read. Probably intended to be newFromFile()? epriestley: Not an array / file is not read. Probably intended to be `newFromFile()`? | |||||
if (!is_array($dict)) { | $ids[] = $ref->getDaemonLog()->getID(); | ||||
epriestleyUnsubmitted Not Done Inline Actions$ref not defined. epriestley: $ref not defined. | |||||
epriestleyUnsubmitted Not Done Inline ActionsUnused. epriestley: Unused. | |||||
// Just return a hanging reference, since control code needs to be | |||||
// robust against unusual system states. | |||||
$dict = array(); | |||||
} | |||||
$ref = PhabricatorDaemonReference::newFromDictionary($dict); | |||||
$ref->setPIDFile($pid_dir.'/'.$pid_file); | |||||
$results[] = $ref; | |||||
} | } | ||||
return $results; | $other = id(new PhabricatorDaemonLogQuery()) | ||||
->setViewer(PhabricatorUser::getOmnipotentUser()) | |||||
->withStatus(PhabricatorDaemonLogQuery::STATUS_ALIVE) | |||||
->execute(); | |||||
epriestleyUnsubmitted Not Done Inline ActionsThis never returns. epriestley: This never returns. | |||||
} | } | ||||
private function findDaemonClass($substring) { | private function findDaemonClass($substring) { | ||||
$symbols = $this->loadAvailableDaemonClasses(); | $symbols = $this->loadAvailableDaemonClasses(); | ||||
$symbols = ipull($symbols, 'name'); | $symbols = ipull($symbols, 'name'); | ||||
$match = array(); | $match = array(); | ||||
foreach ($symbols as $symbol) { | foreach ($symbols as $symbol) { | ||||
Show All 19 Lines | if (count($match) == 0) { | ||||
"Specify a daemon unambiguously. Multiple daemons match '%s': %s.", | "Specify a daemon unambiguously. Multiple daemons match '%s': %s.", | ||||
$substring, | $substring, | ||||
implode(', ', $match))); | implode(', ', $match))); | ||||
} | } | ||||
return head($match); | return head($match); | ||||
} | } | ||||
protected final function launchDaemon($class, array $argv, $debug) { | |||||
protected function launchDaemon($class, array $argv, $debug) { | |||||
$daemon = $this->findDaemonClass($class); | $daemon = $this->findDaemonClass($class); | ||||
$console = PhutilConsole::getConsole(); | $console = PhutilConsole::getConsole(); | ||||
if ($debug) { | if ($debug) { | ||||
if ($argv) { | if ($argv) { | ||||
$console->writeOut( | $console->writeOut( | ||||
pht( | pht( | ||||
"Launching daemon \"%s\" in debug mode (not daemonized) ". | "Launching daemon \"%s\" in debug mode (not daemonized) ". | ||||
▲ Show 20 Lines • Show All 101 Lines • ▼ Show 20 Lines | foreach ($extension->getFunctions() as $function) { | ||||
if (!function_exists($function)) { | if (!function_exists($function)) { | ||||
echo "ERROR: The PHP function {$function}() is disabled. You must ". | echo "ERROR: The PHP function {$function}() is disabled. You must ". | ||||
"enable it to run daemons on this machine.\n"; | "enable it to run daemons on this machine.\n"; | ||||
exit(1); | exit(1); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
protected function willLaunchDaemons() { | protected final function willLaunchDaemons() { | ||||
$console = PhutilConsole::getConsole(); | $console = PhutilConsole::getConsole(); | ||||
$console->writeErr(pht('Preparing to launch daemons.')."\n"); | $console->writeErr(pht('Preparing to launch daemons.')."\n"); | ||||
$log_dir = $this->getLogDirectory().'/daemons.log'; | $log_dir = $this->getLogDirectory().'/daemons.log'; | ||||
$console->writeErr(pht("NOTE: Logs will appear in '%s'.", $log_dir)."\n\n"); | $console->writeErr(pht("NOTE: Logs will appear in '%s'.", $log_dir)."\n\n"); | ||||
} | } | ||||
/* -( Commands )----------------------------------------------------------- */ | /* -( Commands )----------------------------------------------------------- */ | ||||
protected function executeStartCommand($keep_leases = false) { | protected final function executeStartCommand($keep_leases = false) { | ||||
$console = PhutilConsole::getConsole(); | $console = PhutilConsole::getConsole(); | ||||
$running = $this->loadRunningDaemons(); | $running = $this->loadRunningDaemons(); | ||||
// This may include daemons which were launched but which are no longer | // This may include daemons which were launched but which are no longer | ||||
// running; check that we actually have active daemons before failing. | // running; check that we actually have active daemons before failing. | ||||
foreach ($running as $daemon) { | foreach ($running as $daemon) { | ||||
if ($daemon->isRunning()) { | if ($daemon->isRunning()) { | ||||
Show All 37 Lines | foreach ($daemons as $spec) { | ||||
$this->launchDaemon($name, $argv, $is_debug = false); | $this->launchDaemon($name, $argv, $is_debug = false); | ||||
} | } | ||||
$console->writeErr(pht('Done.')."\n"); | $console->writeErr(pht('Done.')."\n"); | ||||
return 0; | return 0; | ||||
} | } | ||||
protected final function executeStopCommand(array $pids) { | |||||
protected function executeStopCommand(array $pids) { | |||||
$console = PhutilConsole::getConsole(); | $console = PhutilConsole::getConsole(); | ||||
$daemons = $this->loadRunningDaemons(); | $daemons = $this->loadRunningDaemons(); | ||||
if (!$daemons) { | if (!$daemons) { | ||||
$console->writeErr(pht('There are no running Phabricator daemons.')."\n"); | $console->writeErr(pht('There are no running Phabricator daemons.')."\n"); | ||||
return 0; | return 0; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 86 Lines • Show Last 20 Lines |
Unused.