diff --git a/src/applications/repository/daemon/PhabricatorRepositoryPullLocalDaemon.php b/src/applications/repository/daemon/PhabricatorRepositoryPullLocalDaemon.php --- a/src/applications/repository/daemon/PhabricatorRepositoryPullLocalDaemon.php +++ b/src/applications/repository/daemon/PhabricatorRepositoryPullLocalDaemon.php @@ -102,44 +102,59 @@ $retry_after, array_keys($pullable)); - // Figure out which repositories we need to queue for an update. foreach ($pullable as $id => $repository) { - $monogram = $repository->getMonogram(); + $now = PhabricatorTime::getNow(); + $display_name = $repository->getDisplayName(); if (isset($futures[$id])) { - $this->log(pht('Repository "%s" is currently updating.', $monogram)); + $this->log( + pht( + 'Repository "%s" is currently updating.', + $display_name)); continue; } if (isset($queue[$id])) { - $this->log(pht('Repository "%s" is already queued.', $monogram)); - continue; - } - - $after = idx($retry_after, $id, 0); - if ($after > time()) { $this->log( pht( - 'Repository "%s" is not due for an update for %s second(s).', - $monogram, - new PhutilNumber($after - time()))); + 'Repository "%s" is already queued.', + $display_name)); continue; } + $after = idx($retry_after, $id); if (!$after) { + $smart_wait = $repository->loadUpdateInterval($min_sleep); + $last_update = $this->loadLastUpdate($repository); + + $after = $last_update + $smart_wait; + $retry_after[$id] = $after; + $this->log( pht( - 'Scheduling repository "%s" for an initial update.', - $monogram)); - } else { + 'Scheduling repository "%s" with an update window of %s '. + 'second(s). Last update was %s second(s) ago.', + $display_name, + new PhutilNumber($smart_wait), + new PhutilNumber($now - $last_update))); + } + + if ($after > time()) { $this->log( pht( - 'Scheduling repository "%s" for an update (%s seconds overdue).', - $monogram, - new PhutilNumber(time() - $after))); + 'Repository "%s" is not due for an update for %s second(s).', + $display_name, + new PhutilNumber($after - $now))); + continue; } + $this->log( + pht( + 'Scheduling repository "%s" for an update (%s seconds overdue).', + $display_name, + new PhutilNumber($now - $after))); + $queue[$id] = $after; } @@ -157,8 +172,11 @@ continue; } - $monogram = $repository->getMonogram(); - $this->log(pht('Starting update for repository "%s".', $monogram)); + $display_name = $repository->getDisplayName(); + $this->log( + pht( + 'Starting update for repository "%s".', + $display_name)); unset($queue[$id]); $futures[$id] = $this->buildUpdateFuture( @@ -299,6 +317,32 @@ /** * @task pull */ + private function loadLastUpdate(PhabricatorRepository $repository) { + $table = new PhabricatorRepositoryStatusMessage(); + $conn = $table->establishConnection('r'); + + $epoch = queryfx_one( + $conn, + 'SELECT MAX(epoch) last_update FROM %T + WHERE repositoryID = %d + AND statusType IN (%Ls)', + $table->getTableName(), + $repository->getID(), + array( + PhabricatorRepositoryStatusMessage::TYPE_INIT, + PhabricatorRepositoryStatusMessage::TYPE_FETCH, + )); + + if ($epoch) { + return (int)$epoch['last_update']; + } + + return PhabricatorTime::getNow(); + } + + /** + * @task pull + */ private function loadPullableRepositories( array $include, array $exclude, @@ -385,9 +429,9 @@ ExecFuture $future, $min_sleep) { - $monogram = $repository->getMonogram(); + $display_name = $repository->getDisplayName(); - $this->log(pht('Resolving update for "%s".', $monogram)); + $this->log(pht('Resolving update for "%s".', $display_name)); try { list($stdout, $stderr) = $future->resolvex(); @@ -395,17 +439,18 @@ $proxy = new PhutilProxyException( pht( 'Error while updating the "%s" repository.', - $repository->getMonogram()), + $display_name), $ex); phlog($proxy); - return time() + $min_sleep; + $smart_wait = $repository->loadUpdateInterval($min_sleep); + return PhabricatorTime::getNow() + $smart_wait; } if (strlen($stderr)) { $stderr_msg = pht( 'Unexpected output while updating repository "%s": %s', - $monogram, + $display_name, $stderr); phlog($stderr_msg); } @@ -416,10 +461,10 @@ pht( 'Based on activity in repository "%s", considering a wait of %s '. 'seconds before update.', - $repository->getMonogram(), + $display_name, new PhutilNumber($smart_wait))); - return time() + $smart_wait; + return PhabricatorTime::getNow() + $smart_wait; } diff --git a/src/applications/repository/storage/PhabricatorRepository.php b/src/applications/repository/storage/PhabricatorRepository.php --- a/src/applications/repository/storage/PhabricatorRepository.php +++ b/src/applications/repository/storage/PhabricatorRepository.php @@ -1783,7 +1783,7 @@ $smart_wait = $minimum; } - return $smart_wait; + return (int)$smart_wait; }