Differential D20606 Diff 49171 src/applications/daemon/management/PhabricatorDaemonManagementWorkflow.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/daemon/management/PhabricatorDaemonManagementWorkflow.php
| Show All 35 Lines | if (!Filesystem::pathExists($path)) { | ||||
| $path, | $path, | ||||
| 'phd.pid-directory', | 'phd.pid-directory', | ||||
| 'phd.log-directory')); | 'phd.log-directory')); | ||||
| } | } | ||||
| } | } | ||||
| return $path; | return $path; | ||||
| } | } | ||||
| final protected function loadRunningDaemons() { | |||||
| $daemons = array(); | |||||
| $pid_dir = $this->getPIDDirectory(); | |||||
| $pid_files = Filesystem::listDirectory($pid_dir); | |||||
| foreach ($pid_files as $pid_file) { | |||||
| $path = $pid_dir.'/'.$pid_file; | |||||
| $daemons[] = PhabricatorDaemonReference::loadReferencesFromFile($path); | |||||
| } | |||||
| return array_mergev($daemons); | |||||
| } | |||||
| 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) { | ||||
| if (stripos($symbol, $substring) !== false) { | if (stripos($symbol, $substring) !== false) { | ||||
| if (strtolower($symbol) == strtolower($substring)) { | if (strtolower($symbol) == strtolower($substring)) { | ||||
| ▲ Show 20 Lines • Show All 73 Lines • ▼ Show 20 Lines | final protected function launchDaemons( | ||||
| if ($trace || PhabricatorEnv::getEnvConfig('phd.trace')) { | if ($trace || PhabricatorEnv::getEnvConfig('phd.trace')) { | ||||
| $flags[] = '--trace'; | $flags[] = '--trace'; | ||||
| } | } | ||||
| if ($debug || PhabricatorEnv::getEnvConfig('phd.verbose')) { | if ($debug || PhabricatorEnv::getEnvConfig('phd.verbose')) { | ||||
| $flags[] = '--verbose'; | $flags[] = '--verbose'; | ||||
| } | } | ||||
| $instance = PhabricatorEnv::getEnvConfig('cluster.instance'); | $instance = $this->getInstance(); | ||||
| if ($instance) { | if ($instance) { | ||||
| $flags[] = '-l'; | $flags[] = '-l'; | ||||
| $flags[] = $instance; | $flags[] = $instance; | ||||
| } | } | ||||
| $config = array(); | $config = array(); | ||||
| if (!$debug) { | if (!$debug) { | ||||
| ▲ Show 20 Lines • Show All 138 Lines • ▼ Show 20 Lines | PhutilTypeSpec::checkMap( | ||||
| 'keep-leases' => 'optional bool', | 'keep-leases' => 'optional bool', | ||||
| 'force' => 'optional bool', | 'force' => 'optional bool', | ||||
| 'reserve' => 'optional float', | 'reserve' => 'optional float', | ||||
| )); | )); | ||||
| $console = PhutilConsole::getConsole(); | $console = PhutilConsole::getConsole(); | ||||
| if (!idx($options, 'force')) { | if (!idx($options, 'force')) { | ||||
| $running = $this->loadRunningDaemons(); | $process_refs = $this->getOverseerProcessRefs(); | ||||
| if ($process_refs) { | |||||
| $this->logWarn( | |||||
| pht('RUNNING DAEMONS'), | |||||
| pht('Daemons are already running:')); | |||||
| fprintf(STDERR, '%s', "\n"); | |||||
| foreach ($process_refs as $process_ref) { | |||||
| fprintf( | |||||
| STDERR, | |||||
| '%s', | |||||
| tsprintf( | |||||
| " %s %s\n", | |||||
| $process_ref->getPID(), | |||||
| $process_ref->getCommand())); | |||||
| } | |||||
| fprintf(STDERR, '%s', "\n"); | |||||
| // This may include daemons which were launched but which are no longer | $this->logFail( | ||||
| // running; check that we actually have active daemons before failing. | pht('RUNNING DAEMONS'), | ||||
| foreach ($running as $daemon) { | pht( | ||||
| if ($daemon->isRunning()) { | 'Use "phd stop" to stop daemons, "phd restart" to restart '. | ||||
| $message = pht( | 'daemons, or "phd start --force" to ignore running processes.')); | ||||
| "phd start: Unable to start daemons because daemons are already ". | |||||
| "running.\n\n". | |||||
| "You can view running daemons with '%s'.\n". | |||||
| "You can stop running daemons with '%s'.\n". | |||||
| "You can use '%s' to stop all daemons before starting ". | |||||
| "new daemons.\n". | |||||
| "You can force daemons to start anyway with %s.", | |||||
| 'phd status', | |||||
| 'phd stop', | |||||
| 'phd restart', | |||||
| '--force'); | |||||
| $console->writeErr("%s\n", $message); | |||||
| exit(1); | exit(1); | ||||
| } | } | ||||
| } | } | ||||
| } | |||||
| if (idx($options, 'keep-leases')) { | if (idx($options, 'keep-leases')) { | ||||
| $console->writeErr("%s\n", pht('Not touching active task queue leases.')); | $console->writeErr("%s\n", pht('Not touching active task queue leases.')); | ||||
| } else { | } else { | ||||
| $console->writeErr("%s\n", pht('Freeing active task leases...')); | $console->writeErr("%s\n", pht('Freeing active task leases...')); | ||||
| $count = $this->freeActiveLeases(); | $count = $this->freeActiveLeases(); | ||||
| $console->writeErr( | $console->writeErr( | ||||
| "%s\n", | "%s\n", | ||||
| Show All 29 Lines | /* -( Commands )----------------------------------------------------------- */ | ||||
| final protected function executeStopCommand(array $options) { | final protected function executeStopCommand(array $options) { | ||||
| $grace_period = idx($options, 'graceful', 15); | $grace_period = idx($options, 'graceful', 15); | ||||
| $force = idx($options, 'force'); | $force = idx($options, 'force'); | ||||
| $query = id(new PhutilProcessQuery()) | $query = id(new PhutilProcessQuery()) | ||||
| ->withIsOverseer(true); | ->withIsOverseer(true); | ||||
| $instance = PhabricatorEnv::getEnvConfig('cluster.instance'); | $instance = $this->getInstance(); | ||||
| if ($instance !== null && !$force) { | if ($instance !== null && !$force) { | ||||
| $query->withInstances(array($instance)); | $query->withInstances(array($instance)); | ||||
| } | } | ||||
| try { | try { | ||||
| $process_refs = $query->execute(); | $process_refs = $query->execute(); | ||||
| } catch (Exception $ex) { | } catch (Exception $ex) { | ||||
| // See T13321. If this fails for some reason, just continue for now so | // See T13321. If this fails for some reason, just continue for now so | ||||
| ▲ Show 20 Lines • Show All 62 Lines • ▼ Show 20 Lines | if ($live_pids) { | ||||
| 'Unable to stop all daemon processes. You may need to run this '. | 'Unable to stop all daemon processes. You may need to run this '. | ||||
| 'command as root with "sudo".')); | 'command as root with "sudo".')); | ||||
| } | } | ||||
| return 0; | return 0; | ||||
| } | } | ||||
| final protected function executeReloadCommand(array $pids) { | final protected function executeReloadCommand(array $pids) { | ||||
| $console = PhutilConsole::getConsole(); | $process_refs = $this->getOverseerProcessRefs(); | ||||
| $daemons = $this->loadRunningDaemons(); | if (!$process_refs) { | ||||
| if (!$daemons) { | $this->logInfo( | ||||
| $console->writeErr( | pht('NO DAEMONS'), | ||||
| "%s\n", | pht('There are no running daemon processes to reload.')); | ||||
| pht('There are no running daemons to reload.')); | |||||
| return 0; | |||||
| } | |||||
| $reload_pids = $this->selectDaemonPIDs($daemons, $pids); | |||||
| if (!$reload_pids) { | |||||
| $console->writeErr( | |||||
| "%s\n", | |||||
| pht('No daemons to reload.')); | |||||
| return 0; | return 0; | ||||
| } | } | ||||
| foreach ($reload_pids as $pid) { | foreach ($process_refs as $process_ref) { | ||||
| $console->writeOut( | $pid = $process_ref->getPID(); | ||||
| "%s\n", | |||||
| $this->logInfo( | |||||
| pht('RELOAD'), | |||||
| pht('Reloading process %d...', $pid)); | pht('Reloading process %d...', $pid)); | ||||
| posix_kill($pid, SIGHUP); | posix_kill($pid, SIGHUP); | ||||
| } | } | ||||
| return 0; | return 0; | ||||
| } | } | ||||
| private function sendStopSignals($pids, $grace_period) { | private function sendStopSignals($pids, $grace_period) { | ||||
| // If we're doing a graceful shutdown, try SIGINT first. | // If we're doing a graceful shutdown, try SIGINT first. | ||||
| ▲ Show 20 Lines • Show All 136 Lines • ▼ Show 20 Lines | if (!$pids) { | ||||
| $select_pids[$pid] = $pid; | $select_pids[$pid] = $pid; | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| return $select_pids; | return $select_pids; | ||||
| } | } | ||||
| protected function getOverseerProcessRefs() { | |||||
| $query = id(new PhutilProcessQuery()) | |||||
| ->withIsOverseer(true); | |||||
| $instance = PhabricatorEnv::getEnvConfig('cluster.instance'); | |||||
| if ($instance !== null) { | |||||
| $query->withInstances(array($instance)); | |||||
| } | |||||
| return $query->execute(); | |||||
| } | |||||
| protected function getInstance() { | |||||
| return PhabricatorEnv::getEnvConfig('cluster.instance'); | |||||
| } | |||||
| } | } | ||||