1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-01-23 13:08:18 +01:00

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
This commit is contained in:
epriestley 2017-03-22 18:58:08 -07:00
parent 9326b4d131
commit 9099485a71
4 changed files with 57 additions and 25 deletions

View file

@ -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',

View file

@ -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;
}
}

View file

@ -0,0 +1,38 @@
<?php
final class PhabricatorRepositoryPullLocalDaemonModule
extends PhutilDaemonOverseerModule {
private $cursor = 0;
public function shouldWakePool(PhutilDaemonPool $pool) {
$class = $pool->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;
}
}

View file

@ -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);
}
}