Differential D15698 Diff 37842 src/applications/aphlict/management/PhabricatorAphlictManagementWorkflow.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/aphlict/management/PhabricatorAphlictManagementWorkflow.php
<?php | <?php | ||||
abstract class PhabricatorAphlictManagementWorkflow | abstract class PhabricatorAphlictManagementWorkflow | ||||
extends PhabricatorManagementWorkflow { | extends PhabricatorManagementWorkflow { | ||||
private $debug = false; | private $debug = false; | ||||
private $clientHost; | private $clientHost; | ||||
private $clientPort; | private $clientPort; | ||||
protected function didConstruct() { | final protected function setDebug($debug) { | ||||
$this | $this->debug = $debug; | ||||
->setArguments( | return $this; | ||||
array( | } | ||||
protected function getLaunchArguments() { | |||||
return array( | |||||
array( | array( | ||||
'name' => 'client-host', | 'name' => 'client-host', | ||||
'param' => 'hostname', | 'param' => 'hostname', | ||||
'help' => pht('Hostname to bind to for the client server.'), | 'help' => pht('Hostname to bind to for the client server.'), | ||||
), | ), | ||||
array( | array( | ||||
'name' => 'client-port', | 'name' => 'client-port', | ||||
'param' => 'port', | 'param' => 'port', | ||||
'help' => pht('Port to bind to for the client server.'), | 'help' => pht('Port to bind to for the client server.'), | ||||
), | ), | ||||
)); | ); | ||||
} | } | ||||
public function execute(PhutilArgumentParser $args) { | protected function parseLaunchArguments(PhutilArgumentParser $args) { | ||||
$this->clientHost = $args->getArg('client-host'); | $this->clientHost = $args->getArg('client-host'); | ||||
$this->clientPort = $args->getArg('client-port'); | $this->clientPort = $args->getArg('client-port'); | ||||
return 0; | |||||
} | } | ||||
final public function getPIDPath() { | final public function getPIDPath() { | ||||
$path = PhabricatorEnv::getEnvConfig('notification.pidfile'); | $path = PhabricatorEnv::getEnvConfig('notification.pidfile'); | ||||
try { | try { | ||||
$dir = dirname($path); | $dir = dirname($path); | ||||
if (!Filesystem::pathExists($dir)) { | if (!Filesystem::pathExists($dir)) { | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | if ($g_future) { | ||||
$g_future = null; | $g_future = null; | ||||
} | } | ||||
Filesystem::remove($this->getPIDPath()); | Filesystem::remove($this->getPIDPath()); | ||||
exit(1); | exit(1); | ||||
} | } | ||||
final protected function setDebug($debug) { | |||||
$this->debug = $debug; | |||||
return $this; | |||||
} | |||||
public static function requireExtensions() { | public static function requireExtensions() { | ||||
self::mustHaveExtension('pcntl'); | self::mustHaveExtension('pcntl'); | ||||
self::mustHaveExtension('posix'); | self::mustHaveExtension('posix'); | ||||
} | } | ||||
private static function mustHaveExtension($ext) { | private static function mustHaveExtension($ext) { | ||||
if (!extension_loaded($ext)) { | if (!extension_loaded($ext)) { | ||||
echo pht( | echo pht( | ||||
Show All 39 Lines | final protected function willLaunch() { | ||||
} | } | ||||
// First, start the server in configuration test mode with --test. This | // First, start the server in configuration test mode with --test. This | ||||
// will let us error explicitly if there are missing modules, before we | // will let us error explicitly if there are missing modules, before we | ||||
// fork and lose access to the console. | // fork and lose access to the console. | ||||
$test_argv = $this->getServerArgv(); | $test_argv = $this->getServerArgv(); | ||||
$test_argv[] = '--test=true'; | $test_argv[] = '--test=true'; | ||||
execx( | |||||
'%s %s %Ls', | execx('%C', $this->getStartCommand($test_argv)); | ||||
$this->getNodeBinary(), | |||||
$this->getAphlictScriptPath(), | |||||
$test_argv); | |||||
} | } | ||||
private function getServerArgv() { | private function getServerArgv() { | ||||
$ssl_key = PhabricatorEnv::getEnvConfig('notification.ssl-key'); | $ssl_key = PhabricatorEnv::getEnvConfig('notification.ssl-key'); | ||||
$ssl_cert = PhabricatorEnv::getEnvConfig('notification.ssl-cert'); | $ssl_cert = PhabricatorEnv::getEnvConfig('notification.ssl-cert'); | ||||
$server_uri = PhabricatorEnv::getEnvConfig('notification.server-uri'); | $server_uri = PhabricatorEnv::getEnvConfig('notification.server-uri'); | ||||
$server_uri = new PhutilURI($server_uri); | $server_uri = new PhutilURI($server_uri); | ||||
Show All 22 Lines | private function getServerArgv() { | ||||
if ($this->clientHost) { | if ($this->clientHost) { | ||||
$server_argv[] = '--client-host='.$this->clientHost; | $server_argv[] = '--client-host='.$this->clientHost; | ||||
} | } | ||||
return $server_argv; | return $server_argv; | ||||
} | } | ||||
private function getAphlictScriptPath() { | |||||
$root = dirname(phutil_get_library_root('phabricator')); | |||||
return $root.'/support/aphlict/server/aphlict_server.js'; | |||||
} | |||||
final protected function launch() { | final protected function launch() { | ||||
$console = PhutilConsole::getConsole(); | $console = PhutilConsole::getConsole(); | ||||
if ($this->debug) { | if ($this->debug) { | ||||
$console->writeOut( | $console->writeOut( | ||||
"%s\n", | "%s\n", | ||||
pht('Starting Aphlict server in foreground...')); | pht('Starting Aphlict server in foreground...')); | ||||
} else { | } else { | ||||
Filesystem::writeFile($this->getPIDPath(), getmypid()); | Filesystem::writeFile($this->getPIDPath(), getmypid()); | ||||
} | } | ||||
$command = csprintf( | $command = $this->getStartCommand($this->getServerArgv()); | ||||
'%s %s %Ls', | |||||
$this->getNodeBinary(), | |||||
$this->getAphlictScriptPath(), | |||||
$this->getServerArgv()); | |||||
if (!$this->debug) { | if (!$this->debug) { | ||||
declare(ticks = 1); | declare(ticks = 1); | ||||
pcntl_signal(SIGINT, array($this, 'cleanup')); | pcntl_signal(SIGINT, array($this, 'cleanup')); | ||||
pcntl_signal(SIGTERM, array($this, 'cleanup')); | pcntl_signal(SIGTERM, array($this, 'cleanup')); | ||||
} | } | ||||
register_shutdown_function(array($this, 'cleanup')); | register_shutdown_function(array($this, 'cleanup')); | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | final protected function executeStartCommand() { | ||||
// When we fork, the child process will inherit its parent's set of open | // When we fork, the child process will inherit its parent's set of open | ||||
// file descriptors. If the parent process of bin/aphlict is waiting for | // file descriptors. If the parent process of bin/aphlict is waiting for | ||||
// bin/aphlict's file descriptors to close, it will be stuck waiting on | // bin/aphlict's file descriptors to close, it will be stuck waiting on | ||||
// the daemonized process. (This happens if e.g. bin/aphlict is started | // the daemonized process. (This happens if e.g. bin/aphlict is started | ||||
// in another script using passthru().) | // in another script using passthru().) | ||||
fclose(STDOUT); | fclose(STDOUT); | ||||
fclose(STDERR); | fclose(STDERR); | ||||
$this->launch(); | $this->launch(); | ||||
return 0; | return 0; | ||||
} | } | ||||
final protected function executeStopCommand() { | final protected function executeStopCommand() { | ||||
$console = PhutilConsole::getConsole(); | $console = PhutilConsole::getConsole(); | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | throw new PhutilArgumentUsageException( | ||||
pht( | pht( | ||||
'No `%s` or `%s` binary was found in %s. You must install '. | 'No `%s` or `%s` binary was found in %s. You must install '. | ||||
'Node.js to start the Aphlict server.', | 'Node.js to start the Aphlict server.', | ||||
'nodejs', | 'nodejs', | ||||
'node', | 'node', | ||||
'$PATH')); | '$PATH')); | ||||
} | } | ||||
private function getAphlictScriptPath() { | |||||
$root = dirname(phutil_get_library_root('phabricator')); | |||||
return $root.'/support/aphlict/server/aphlict_server.js'; | |||||
} | |||||
private function getStartCommand(array $server_argv) { | |||||
return csprintf( | |||||
'%s %s %Ls', | |||||
$this->getNodeBinary(), | |||||
$this->getAphlictScriptPath(), | |||||
$server_argv); | |||||
} | |||||
} | } |