From 9099485a712500c08fc4e00cfb49b5430c2e865d Mon Sep 17 00:00:00 2001 From: epriestley Date: Wed, 22 Mar 2017 18:58:08 -0700 Subject: [PATCH] Allow the PullLocal daemon to hibernate, and wake it when repositories need an update Summary: Ref T12298. This allows the PullLocal daemon to hibernate like the Trigger daemon, but automatically wakes it back up when it needs to do something. Test Plan: - Ran `bin/phd debug pulllocal --trace`. - Saw the daemon hibernate after doing a checkup on repositories. - Saw periodic queries to look for new update messages. - After clicking "Update Now" in the web UI to schedule an update, saw the daemon wake up immediately. Reviewers: chad Reviewed By: chad Maniphest Tasks: T12298 Differential Revision: https://secure.phabricator.com/D17540 --- src/__phutil_library_map__.php | 2 + .../PhabricatorRepositoryPullLocalDaemon.php | 13 ++++++- ...ricatorRepositoryPullLocalDaemonModule.php | 38 +++++++++++++++++++ .../PhabricatorDaemonOverseerModule.php | 29 +++----------- 4 files changed, 57 insertions(+), 25 deletions(-) create mode 100644 src/applications/repository/daemon/PhabricatorRepositoryPullLocalDaemonModule.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 56150ceb2f..f137d1b221 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -3692,6 +3692,7 @@ phutil_register_library_map(array( 'PhabricatorRepositoryPullEventPHIDType' => 'applications/repository/phid/PhabricatorRepositoryPullEventPHIDType.php', 'PhabricatorRepositoryPullEventQuery' => 'applications/repository/query/PhabricatorRepositoryPullEventQuery.php', 'PhabricatorRepositoryPullLocalDaemon' => 'applications/repository/daemon/PhabricatorRepositoryPullLocalDaemon.php', + 'PhabricatorRepositoryPullLocalDaemonModule' => 'applications/repository/daemon/PhabricatorRepositoryPullLocalDaemonModule.php', 'PhabricatorRepositoryPushEvent' => 'applications/repository/storage/PhabricatorRepositoryPushEvent.php', 'PhabricatorRepositoryPushEventPHIDType' => 'applications/repository/phid/PhabricatorRepositoryPushEventPHIDType.php', 'PhabricatorRepositoryPushEventQuery' => 'applications/repository/query/PhabricatorRepositoryPushEventQuery.php', @@ -8987,6 +8988,7 @@ phutil_register_library_map(array( 'PhabricatorRepositoryPullEventPHIDType' => 'PhabricatorPHIDType', 'PhabricatorRepositoryPullEventQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', 'PhabricatorRepositoryPullLocalDaemon' => 'PhabricatorDaemon', + 'PhabricatorRepositoryPullLocalDaemonModule' => 'PhutilDaemonOverseerModule', 'PhabricatorRepositoryPushEvent' => array( 'PhabricatorRepositoryDAO', 'PhabricatorPolicyInterface', diff --git a/src/applications/repository/daemon/PhabricatorRepositoryPullLocalDaemon.php b/src/applications/repository/daemon/PhabricatorRepositoryPullLocalDaemon.php index fe19849163..4d4b961765 100644 --- a/src/applications/repository/daemon/PhabricatorRepositoryPullLocalDaemon.php +++ b/src/applications/repository/daemon/PhabricatorRepositoryPullLocalDaemon.php @@ -228,7 +228,10 @@ final class PhabricatorRepositoryPullLocalDaemon continue; } - $this->waitForUpdates($min_sleep, $retry_after); + $should_hibernate = $this->waitForUpdates($min_sleep, $retry_after); + if ($should_hibernate) { + break; + } } } @@ -492,6 +495,10 @@ final class PhabricatorRepositoryPullLocalDaemon while (($sleep_until - time()) > 0) { $sleep_duration = ($sleep_until - time()); + if ($this->shouldHibernate($sleep_duration)) { + return true; + } + $this->log( pht( 'Sleeping for %s more second(s)...', @@ -501,7 +508,7 @@ final class PhabricatorRepositoryPullLocalDaemon if ($this->shouldExit()) { $this->log(pht('Awakened from sleep by graceful shutdown!')); - return; + return false; } if ($this->loadRepositoryUpdateMessages()) { @@ -509,6 +516,8 @@ final class PhabricatorRepositoryPullLocalDaemon break; } } + + return false; } } diff --git a/src/applications/repository/daemon/PhabricatorRepositoryPullLocalDaemonModule.php b/src/applications/repository/daemon/PhabricatorRepositoryPullLocalDaemonModule.php new file mode 100644 index 0000000000..45dc49d9af --- /dev/null +++ b/src/applications/repository/daemon/PhabricatorRepositoryPullLocalDaemonModule.php @@ -0,0 +1,38 @@ +getPoolDaemonClass(); + if ($class != 'PhabricatorRepositoryPullLocalDaemon') { + return false; + } + + if ($this->shouldThrottle($class, 1)) { + return false; + } + + $table = new PhabricatorRepositoryStatusMessage(); + $table_name = $table->getTableName(); + $conn = $table->establishConnection('r'); + + $row = queryfx_one( + $conn, + 'SELECT id FROM %T WHERE statusType = %s + AND id > %d ORDER BY id DESC LIMIT 1', + $table_name, + PhabricatorRepositoryStatusMessage::TYPE_NEEDS_UPDATE, + $this->cursor); + + if (!$row) { + return false; + } + + $this->cursor = (int)$row['id']; + return true; + } + +} diff --git a/src/infrastructure/daemon/overseer/PhabricatorDaemonOverseerModule.php b/src/infrastructure/daemon/overseer/PhabricatorDaemonOverseerModule.php index aa238a4bf0..608151c348 100644 --- a/src/infrastructure/daemon/overseer/PhabricatorDaemonOverseerModule.php +++ b/src/infrastructure/daemon/overseer/PhabricatorDaemonOverseerModule.php @@ -10,18 +10,9 @@ final class PhabricatorDaemonOverseerModule extends PhutilDaemonOverseerModule { private $configVersion; - private $timestamp; - - public function __construct() { - $this->timestamp = PhabricatorTime::getNow(); - } public function shouldReloadDaemons() { - $now = PhabricatorTime::getNow(); - $ago = ($now - $this->timestamp); - - // Don't check more than once every 10 seconds. - if ($ago < 10) { + if ($this->shouldThrottle('reload', 10)) { return false; } @@ -47,25 +38,17 @@ final class PhabricatorDaemonOverseerModule } /** - * Update the configuration version and timestamp. + * Check and update the configuration version. * * @return bool True if the daemons should restart, otherwise false. */ private function updateConfigVersion() { - $config_version = $this->loadConfigVersion(); - $this->timestamp = PhabricatorTime::getNow(); + $old_version = $this->configVersion; + $new_version = $this->loadConfigVersion(); - if (!$this->configVersion) { - $this->configVersion = $config_version; - return false; - } + $this->configVersion = $new_version; - if ($this->configVersion != $config_version) { - $this->configVersion = $config_version; - return true; - } - - return false; + return ($old_version != $new_version); } }