diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 68ba4a9a14..d37db5facf 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -1023,6 +1023,7 @@ phutil_register_library_map(array( 'PhabricatorDaemonConsoleController' => 'applications/daemon/controller/PhabricatorDaemonConsoleController.php', 'PhabricatorDaemonController' => 'applications/daemon/controller/PhabricatorDaemonController.php', 'PhabricatorDaemonDAO' => 'infrastructure/daemon/storage/PhabricatorDaemonDAO.php', + 'PhabricatorDaemonEventListener' => 'applications/daemon/event/PhabricatorDaemonEventListener.php', 'PhabricatorDaemonLog' => 'infrastructure/daemon/storage/PhabricatorDaemonLog.php', 'PhabricatorDaemonLogEvent' => 'infrastructure/daemon/storage/PhabricatorDaemonLogEvent.php', 'PhabricatorDaemonLogEventsView' => 'applications/daemon/view/PhabricatorDaemonLogEventsView.php', @@ -3026,6 +3027,7 @@ phutil_register_library_map(array( 'PhabricatorDaemonConsoleController' => 'PhabricatorDaemonController', 'PhabricatorDaemonController' => 'PhabricatorController', 'PhabricatorDaemonDAO' => 'PhabricatorLiskDAO', + 'PhabricatorDaemonEventListener' => 'PhutilEventListener', 'PhabricatorDaemonLog' => 'PhabricatorDaemonDAO', 'PhabricatorDaemonLogEvent' => 'PhabricatorDaemonDAO', 'PhabricatorDaemonLogEventsView' => 'AphrontView', diff --git a/src/applications/daemon/application/PhabricatorApplicationDaemons.php b/src/applications/daemon/application/PhabricatorApplicationDaemons.php index 9a8699ab06..8180319f80 100644 --- a/src/applications/daemon/application/PhabricatorApplicationDaemons.php +++ b/src/applications/daemon/application/PhabricatorApplicationDaemons.php @@ -30,6 +30,12 @@ final class PhabricatorApplicationDaemons extends PhabricatorApplication { return false; } + public function getEventListeners() { + return array( + new PhabricatorDaemonEventListener(), + ); + } + public function getRoutes() { return array( '/daemon/' => array( diff --git a/src/applications/daemon/conduit/ConduitAPI_daemon_launched_Method.php b/src/applications/daemon/conduit/ConduitAPI_daemon_launched_Method.php index f18cda2905..c655b1826e 100644 --- a/src/applications/daemon/conduit/ConduitAPI_daemon_launched_Method.php +++ b/src/applications/daemon/conduit/ConduitAPI_daemon_launched_Method.php @@ -5,8 +5,11 @@ */ final class ConduitAPI_daemon_launched_Method extends ConduitAPIMethod { + public function getMethodStatus() { + return self::METHOD_STATUS_DEPRECATED; + } + public function shouldRequireAuthentication() { - // TODO: Lock this down once we build phantoms. return false; } diff --git a/src/applications/daemon/conduit/ConduitAPI_daemon_log_Method.php b/src/applications/daemon/conduit/ConduitAPI_daemon_log_Method.php index ed316b0f27..b549b49a55 100644 --- a/src/applications/daemon/conduit/ConduitAPI_daemon_log_Method.php +++ b/src/applications/daemon/conduit/ConduitAPI_daemon_log_Method.php @@ -5,8 +5,11 @@ */ final class ConduitAPI_daemon_log_Method extends ConduitAPIMethod { + public function getMethodStatus() { + return self::METHOD_STATUS_DEPRECATED; + } + public function shouldRequireAuthentication() { - // TODO: Lock this down once we build phantoms. return false; } diff --git a/src/applications/daemon/conduit/ConduitAPI_daemon_setstatus_Method.php b/src/applications/daemon/conduit/ConduitAPI_daemon_setstatus_Method.php index 7b034b87de..953f7b61ac 100644 --- a/src/applications/daemon/conduit/ConduitAPI_daemon_setstatus_Method.php +++ b/src/applications/daemon/conduit/ConduitAPI_daemon_setstatus_Method.php @@ -5,8 +5,11 @@ */ final class ConduitAPI_daemon_setstatus_Method extends ConduitAPIMethod { + public function getMethodStatus() { + return self::METHOD_STATUS_DEPRECATED; + } + public function shouldRequireAuthentication() { - // TODO: Lock this down once we build phantoms. return false; } diff --git a/src/applications/daemon/event/PhabricatorDaemonEventListener.php b/src/applications/daemon/event/PhabricatorDaemonEventListener.php new file mode 100644 index 0000000000..1a65a15acc --- /dev/null +++ b/src/applications/daemon/event/PhabricatorDaemonEventListener.php @@ -0,0 +1,104 @@ +listen(PhutilDaemonOverseer::EVENT_DID_LAUNCH); + $this->listen(PhutilDaemonOverseer::EVENT_DID_LOG); + $this->listen(PhutilDaemonOverseer::EVENT_DID_HEARTBEAT); + $this->listen(PhutilDaemonOverseer::EVENT_WILL_EXIT); + } + + public function handleEvent(PhutilEvent $event) { + switch ($event->getType()) { + case PhutilDaemonOverseer::EVENT_DID_LAUNCH: + $this->handleLaunchEvent($event); + break; + case PhutilDaemonOverseer::EVENT_DID_HEARTBEAT: + $this->handleHeartbeatEvent($event); + break; + case PhutilDaemonOverseer::EVENT_DID_LOG: + $this->handleLogEvent($event); + break; + case PhutilDaemonOverseer::EVENT_WILL_EXIT: + $this->handleExitEvent($event); + break; + } + } + + private function handleLaunchEvent(PhutilEvent $event) { + $id = $event->getValue('id'); + + $daemon = id(new PhabricatorDaemonLog()) + ->setDaemon($event->getValue('daemonClass')) + ->setHost(php_uname('n')) + ->setPID(getmypid()) + ->setStatus(PhabricatorDaemonLog::STATUS_RUNNING) + ->setArgv($event->getValue('argv')) + ->save(); + + $this->daemons[$id] = $daemon; + } + + private function handleHeartbeatEvent(PhutilEvent $event) { + $daemon = $this->getDaemon($event->getValue('id')); + + // Just update the timestamp. + $daemon->save(); + } + + private function handleLogEvent(PhutilEvent $event) { + $daemon = $this->getDaemon($event->getValue('id')); + + // TODO: This is a bit awkward for historical reasons, clean it up after + // removing Conduit. + $message = $event->getValue('message'); + $context = $event->getValue('context'); + if (strlen($context) && $context !== $message) { + $message = "({$context}) {$message}"; + } + + $type = $event->getValue('type'); + + $message = phutil_utf8ize($message); + + id(new PhabricatorDaemonLogEvent()) + ->setLogID($daemon->getID()) + ->setLogType($type) + ->setMessage((string)$message) + ->setEpoch(time()) + ->save(); + + switch ($type) { + case 'WAIT': + $current_status = PhabricatorDaemonLog::STATUS_WAIT; + break; + default: + $current_status = PhabricatorDaemonLog::STATUS_RUNNING; + break; + } + + if ($current_status !== $daemon->getStatus()) { + $daemon->setStatus($current_status)->save(); + } + } + + private function handleExitEvent(PhutilEvent $event) { + $id = $event->getValue('id'); + + $daemon = $this->getDaemon($id); + $daemon->setStatus(PhabricatorDaemonLog::STATUS_EXITED)->save(); + + unset($this->daemons[$id]); + } + + private function getDaemon($id) { + if (isset($this->daemons[$id])) { + return $this->daemons[$id]; + } + throw new Exception("No such daemon '{$id}'!"); + } + +} diff --git a/src/applications/daemon/management/PhabricatorDaemonManagementWorkflow.php b/src/applications/daemon/management/PhabricatorDaemonManagementWorkflow.php index 0c2062de6b..fef75d856c 100644 --- a/src/applications/daemon/management/PhabricatorDaemonManagementWorkflow.php +++ b/src/applications/daemon/management/PhabricatorDaemonManagementWorkflow.php @@ -131,8 +131,6 @@ abstract class PhabricatorDaemonManagementWorkflow $flags[] = '--daemonize'; } - $flags[] = csprintf('--conduit-uri=%s', PhabricatorEnv::getURI('/api/')); - if (!$debug) { $log_file = $this->getLogDirectory().'/daemons.log'; $flags[] = csprintf('--log=%s', $log_file); diff --git a/src/infrastructure/daemon/storage/PhabricatorDaemonLog.php b/src/infrastructure/daemon/storage/PhabricatorDaemonLog.php index cb68cd6398..c8afb2de4e 100644 --- a/src/infrastructure/daemon/storage/PhabricatorDaemonLog.php +++ b/src/infrastructure/daemon/storage/PhabricatorDaemonLog.php @@ -5,6 +5,7 @@ final class PhabricatorDaemonLog extends PhabricatorDaemonDAO { const STATUS_UNKNOWN = 'unknown'; const STATUS_RUNNING = 'run'; const STATUS_DEAD = 'dead'; + const STATUS_WAIT = 'wait'; const STATUS_EXITED = 'exit'; protected $daemon;