From b833e324bd4a4a5a9d0190fa3db06fdf6a5642f5 Mon Sep 17 00:00:00 2001 From: epriestley Date: Tue, 13 Feb 2018 03:47:45 -0800 Subject: [PATCH] While waiting for a "bin/drydock" lease to activate, entertain the user with log output Summary: Depends on D19071. Ref T13073. While the daemons are supposedly doing things, show the user any logs they generate. There's often something relevant but unearthing it can be involved. Test Plan: {F5427773} Subscribers: PHID-OPKG-gm6ozazyms6q6i22gyam Maniphest Tasks: T13073 Differential Revision: https://secure.phabricator.com/D19072 --- .../DrydockManagementLeaseWorkflow.php | 78 ++++++++++++++++++- .../drydock/storage/DrydockLease.php | 32 -------- 2 files changed, 77 insertions(+), 33 deletions(-) diff --git a/src/applications/drydock/management/DrydockManagementLeaseWorkflow.php b/src/applications/drydock/management/DrydockManagementLeaseWorkflow.php index 4992833c50..069f82339b 100644 --- a/src/applications/drydock/management/DrydockManagementLeaseWorkflow.php +++ b/src/applications/drydock/management/DrydockManagementLeaseWorkflow.php @@ -90,7 +90,7 @@ final class DrydockManagementLeaseWorkflow "%s\n", pht('Waiting for daemons to activate lease...')); - $lease->waitUntilActive(); + $this->waitUntilActive($lease); echo tsprintf( "%s\n", @@ -99,4 +99,80 @@ final class DrydockManagementLeaseWorkflow return 0; } + + private function waitUntilActive(DrydockLease $lease) { + $viewer = $this->getViewer(); + + $log_cursor = 0; + $log_types = DrydockLogType::getAllLogTypes(); + + $is_active = false; + while (!$is_active) { + $lease->reload(); + + // While we're waiting, show the user any logs which the daemons have + // generated to give them some clue about what's going on. + $logs = id(new DrydockLogQuery()) + ->setViewer($viewer) + ->withLeasePHIDs(array($lease->getPHID())) + ->setBeforeID($log_cursor) + ->execute(); + if ($logs) { + $logs = mpull($logs, null, 'getID'); + ksort($logs); + $log_cursor = last_key($logs); + } + + foreach ($logs as $log) { + $type_key = $log->getType(); + if (isset($log_types[$type_key])) { + $type_object = id(clone $log_types[$type_key]) + ->setLog($log) + ->setViewer($viewer); + + $log_data = $log->getData(); + + $type = $type_object->getLogTypeName(); + $data = $type_object->renderLog($log_data); + } else { + $type = pht('Unknown ("%s")', $type_key); + $data = null; + } + + echo tsprintf( + "<%s> %B\n", + $type, + $data); + } + + $status = $lease->getStatus(); + + switch ($status) { + case DrydockLeaseStatus::STATUS_ACTIVE: + $is_active = true; + break; + case DrydockLeaseStatus::STATUS_RELEASED: + throw new Exception(pht('Lease has already been released!')); + case DrydockLeaseStatus::STATUS_DESTROYED: + throw new Exception(pht('Lease has already been destroyed!')); + case DrydockLeaseStatus::STATUS_BROKEN: + throw new Exception(pht('Lease has been broken!')); + case DrydockLeaseStatus::STATUS_PENDING: + case DrydockLeaseStatus::STATUS_ACQUIRED: + break; + default: + throw new Exception( + pht( + 'Lease has unknown status "%s".', + $status)); + } + + if ($is_active) { + break; + } else { + sleep(1); + } + } + } + } diff --git a/src/applications/drydock/storage/DrydockLease.php b/src/applications/drydock/storage/DrydockLease.php index e60529afe4..e8cdc5b802 100644 --- a/src/applications/drydock/storage/DrydockLease.php +++ b/src/applications/drydock/storage/DrydockLease.php @@ -194,38 +194,6 @@ final class DrydockLease extends DrydockDAO return false; } - public function waitUntilActive() { - while (true) { - $lease = $this->reload(); - if (!$lease) { - throw new Exception(pht('Failed to reload lease.')); - } - - $status = $lease->getStatus(); - - switch ($status) { - case DrydockLeaseStatus::STATUS_ACTIVE: - return; - case DrydockLeaseStatus::STATUS_RELEASED: - throw new Exception(pht('Lease has already been released!')); - case DrydockLeaseStatus::STATUS_DESTROYED: - throw new Exception(pht('Lease has already been destroyed!')); - case DrydockLeaseStatus::STATUS_BROKEN: - throw new Exception(pht('Lease has been broken!')); - case DrydockLeaseStatus::STATUS_PENDING: - case DrydockLeaseStatus::STATUS_ACQUIRED: - break; - default: - throw new Exception( - pht( - 'Lease has unknown status "%s".', - $status)); - } - - sleep(1); - } - } - public function setActivateWhenAcquired($activate) { $this->activateWhenAcquired = true; return $this;