Differential D21689 Diff 51651 src/applications/harbormaster/management/HarbormasterManagementRestartWorkflow.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/harbormaster/management/HarbormasterManagementRestartWorkflow.php
Show All 26 Lines | final class HarbormasterManagementRestartWorkflow | ||||
public function execute(PhutilArgumentParser $args) { | public function execute(PhutilArgumentParser $args) { | ||||
$viewer = $this->getViewer(); | $viewer = $this->getViewer(); | ||||
$ids = $args->getArg('id'); | $ids = $args->getArg('id'); | ||||
$active = $args->getArg('active'); | $active = $args->getArg('active'); | ||||
if (!$ids && !$active) { | if (!$ids && !$active) { | ||||
throw new PhutilArgumentUsageException( | throw new PhutilArgumentUsageException( | ||||
pht('Use --id or --active to select builds.')); | pht('Use "--id" or "--active" to select builds.')); | ||||
} if ($ids && $active) { | } if ($ids && $active) { | ||||
throw new PhutilArgumentUsageException( | throw new PhutilArgumentUsageException( | ||||
pht('Use one of --id or --active to select builds, but not both.')); | pht('Use one of "--id" or "--active" to select builds, but not both.')); | ||||
} | } | ||||
$query = id(new HarbormasterBuildQuery()) | $query = id(new HarbormasterBuildQuery()) | ||||
->setViewer($viewer); | ->setViewer($viewer); | ||||
if ($ids) { | if ($ids) { | ||||
$query->withIDs($ids); | $query->withIDs($ids); | ||||
} else { | } else { | ||||
$query->withBuildStatuses( | $query->withBuildStatuses( | ||||
HarbormasterBuildStatus::getActiveStatusConstants()); | HarbormasterBuildStatus::getActiveStatusConstants()); | ||||
} | } | ||||
$builds = $query->execute(); | $builds = $query->execute(); | ||||
$console = PhutilConsole::getConsole(); | |||||
$count = count($builds); | $count = count($builds); | ||||
if (!$count) { | if (!$count) { | ||||
$console->writeOut("%s\n", pht('No builds to restart.')); | $this->logSkip( | ||||
pht('SKIP'), | |||||
pht('No builds to restart.')); | |||||
return 0; | return 0; | ||||
} | } | ||||
$prompt = pht('Restart %s build(s)?', new PhutilNumber($count)); | $prompt = pht('Restart %s build(s)?', new PhutilNumber($count)); | ||||
if (!phutil_console_confirm($prompt)) { | if (!phutil_console_confirm($prompt)) { | ||||
$console->writeOut("%s\n", pht('Cancelled.')); | throw new ArcanistUserAbortException(); | ||||
return 1; | |||||
} | } | ||||
$app_phid = id(new PhabricatorHarbormasterApplication())->getPHID(); | |||||
$editor = id(new HarbormasterBuildTransactionEditor()) | |||||
->setActor($viewer) | |||||
->setActingAsPHID($app_phid) | |||||
->setContentSource($this->newContentSource()); | |||||
foreach ($builds as $build) { | foreach ($builds as $build) { | ||||
$console->writeOut( | $this->logInfo( | ||||
"<bg:blue> %s </bg> %s\n", | |||||
pht('RESTARTING'), | pht('RESTARTING'), | ||||
pht('Build %d: %s', $build->getID(), $build->getName())); | pht('Build %d: %s', $build->getID(), $build->getName())); | ||||
if (!$build->canRestartBuild()) { | if (!$build->canRestartBuild()) { | ||||
$console->writeOut( | $this->logWarn( | ||||
"<bg:yellow> %s </bg> %s\n", | |||||
pht('INVALID'), | pht('INVALID'), | ||||
pht('Cannot be restarted.')); | pht('Build can not be restarted.')); | ||||
continue; | continue; | ||||
} | } | ||||
$xactions = array(); | |||||
$xactions[] = id(new HarbormasterBuildTransaction()) | $build->sendMessage( | ||||
->setTransactionType(HarbormasterBuildTransaction::TYPE_COMMAND) | $viewer, | ||||
->setNewValue(HarbormasterBuildCommand::COMMAND_RESTART); | HarbormasterBuildCommand::COMMAND_RESTART); | ||||
try { | |||||
$editor->applyTransactions($build, $xactions); | $this->logOkay( | ||||
} catch (Exception $e) { | pht('QUEUED'), | ||||
$message = phutil_console_wrap($e->getMessage(), 2); | pht('Sent a restart message to build.')); | ||||
$console->writeOut( | |||||
"<bg:red> %s </bg>\n%s\n", | |||||
pht('FAILED'), | |||||
$message); | |||||
continue; | |||||
} | |||||
$console->writeOut("<bg:green> %s </bg>\n", pht('SUCCESS')); | |||||
} | } | ||||
return 0; | return 0; | ||||
} | } | ||||
} | } |