From 319a9cefdea2b0fe88f7c4e22334ab96faac8bd4 Mon Sep 17 00:00:00 2001 From: epriestley Date: Tue, 3 May 2016 05:57:09 -0700 Subject: [PATCH] When creating a repository with EditEngine, allocate it onto a random cluster service Summary: Ref T10748. This copies existing code in the `CreateController` which will eventually be removed. Test Plan: - Created a new repository with the EditPro workflow. - Saw it come up into the cluster properly. Reviewers: chad Reviewed By: chad Maniphest Tasks: T10748 Differential Revision: https://secure.phabricator.com/D15835 --- .../editor/DiffusionRepositoryEditEngine.php | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/applications/diffusion/editor/DiffusionRepositoryEditEngine.php b/src/applications/diffusion/editor/DiffusionRepositoryEditEngine.php index f6ba777edc..f421a1af18 100644 --- a/src/applications/diffusion/editor/DiffusionRepositoryEditEngine.php +++ b/src/applications/diffusion/editor/DiffusionRepositoryEditEngine.php @@ -45,6 +45,44 @@ final class DiffusionRepositoryEditEngine $repository->setVersionControlSystem($vcs); } + // Pick a random open service to allocate this repository on, if any exist. + // If there are no services, we aren't in cluster mode and will allocate + // locally. If there are services but none permit allocations, we fail. + + // Eventually we can make this more flexible, but this rule is a reasonable + // starting point as we begin to deploy cluster services. + + $services = id(new AlmanacServiceQuery()) + ->setViewer(PhabricatorUser::getOmnipotentUser()) + ->withServiceTypes( + array( + AlmanacClusterRepositoryServiceType::SERVICETYPE, + )) + ->needProperties(true) + ->execute(); + if ($services) { + // Filter out services which do not permit new allocations. + foreach ($services as $key => $possible_service) { + if ($possible_service->getAlmanacPropertyValue('closed')) { + unset($services[$key]); + } + } + + if (!$services) { + throw new Exception( + pht( + 'This install is configured in cluster mode, but all available '. + 'repository cluster services are closed to new allocations. '. + 'At least one service must be open to allow new allocations to '. + 'take place.')); + } + + shuffle($services); + $service = head($services); + + $repository->setAlmanacServicePHID($service->getPHID()); + } + return $repository; }