Differential D21628 Diff 51485 src/applications/drydock/blueprint/DrydockAlmanacServiceHostBlueprintImplementation.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/drydock/blueprint/DrydockAlmanacServiceHostBlueprintImplementation.php
Show All 28 Lines | public function canAnyBlueprintEverAllocateResourceForLease( | ||||
DrydockLease $lease) { | DrydockLease $lease) { | ||||
return true; | return true; | ||||
} | } | ||||
public function canEverAllocateResourceForLease( | public function canEverAllocateResourceForLease( | ||||
DrydockBlueprint $blueprint, | DrydockBlueprint $blueprint, | ||||
DrydockLease $lease) { | DrydockLease $lease) { | ||||
$services = $this->loadServices($blueprint); | $services = $this->loadServices($blueprint); | ||||
$bindings = $this->loadAllBindings($services); | $bindings = $this->getActiveBindings($services); | ||||
if (!$bindings) { | if (!$bindings) { | ||||
// If there are no devices bound to the services for this blueprint, | // If there are no devices bound to the services for this blueprint, | ||||
// we can not allocate resources. | // we can not allocate resources. | ||||
return false; | return false; | ||||
} | } | ||||
return true; | return true; | ||||
▲ Show 20 Lines • Show All 171 Lines • ▼ Show 20 Lines | if (!$this->services) { | ||||
$blueprint->getBlueprintName())); | $blueprint->getBlueprintName())); | ||||
} | } | ||||
$viewer = $this->getViewer(); | $viewer = $this->getViewer(); | ||||
$services = id(new AlmanacServiceQuery()) | $services = id(new AlmanacServiceQuery()) | ||||
->setViewer($viewer) | ->setViewer($viewer) | ||||
->withPHIDs($service_phids) | ->withPHIDs($service_phids) | ||||
->withServiceTypes($this->getAlmanacServiceTypes()) | ->withServiceTypes($this->getAlmanacServiceTypes()) | ||||
->needBindings(true) | ->needActiveBindings(true) | ||||
->execute(); | ->execute(); | ||||
$services = mpull($services, null, 'getPHID'); | $services = mpull($services, null, 'getPHID'); | ||||
if (count($services) != count($service_phids)) { | if (count($services) != count($service_phids)) { | ||||
$missing_phids = array_diff($service_phids, array_keys($services)); | $missing_phids = array_diff($service_phids, array_keys($services)); | ||||
throw new Exception( | throw new Exception( | ||||
pht( | pht( | ||||
'Some of the Almanac Services defined by this blueprint '. | 'Some of the Almanac Services defined by this blueprint '. | ||||
'could not be loaded. They may be invalid, no longer exist, '. | 'could not be loaded. They may be invalid, no longer exist, '. | ||||
'or be of the wrong type: %s.', | 'or be of the wrong type: %s.', | ||||
implode(', ', $missing_phids))); | implode(', ', $missing_phids))); | ||||
} | } | ||||
$this->services = $services; | $this->services = $services; | ||||
} | } | ||||
return $this->services; | return $this->services; | ||||
} | } | ||||
private function loadAllBindings(array $services) { | private function getActive(array $services) { | ||||
assert_instances_of($services, 'AlmanacService'); | assert_instances_of($services, 'AlmanacService'); | ||||
$bindings = array_mergev(mpull($services, 'getBindings')); | $bindings = array_mergev(mpull($services, 'getActiveBindings')); | ||||
return mpull($bindings, null, 'getPHID'); | return mpull($bindings, null, 'getPHID'); | ||||
} | } | ||||
private function loadFreeBindings(DrydockBlueprint $blueprint) { | private function loadFreeBindings(DrydockBlueprint $blueprint) { | ||||
if ($this->freeBindings === null) { | if ($this->freeBindings === null) { | ||||
$viewer = $this->getViewer(); | $viewer = $this->getViewer(); | ||||
$pool = id(new DrydockResourceQuery()) | $pool = id(new DrydockResourceQuery()) | ||||
Show All 10 Lines | if ($this->freeBindings === null) { | ||||
$allocated_phids = array(); | $allocated_phids = array(); | ||||
foreach ($pool as $resource) { | foreach ($pool as $resource) { | ||||
$allocated_phids[] = $resource->getAttribute('almanacBindingPHID'); | $allocated_phids[] = $resource->getAttribute('almanacBindingPHID'); | ||||
} | } | ||||
$allocated_phids = array_fuse($allocated_phids); | $allocated_phids = array_fuse($allocated_phids); | ||||
$services = $this->loadServices($blueprint); | $services = $this->loadServices($blueprint); | ||||
$bindings = $this->loadAllBindings($services); | $bindings = $this->getActiveBindings($services); | ||||
$free = array(); | $free = array(); | ||||
foreach ($bindings as $binding) { | foreach ($bindings as $binding) { | ||||
// Don't consider disabled bindings to be available. | |||||
if ($binding->getIsDisabled()) { | |||||
continue; | |||||
} | |||||
if (empty($allocated_phids[$binding->getPHID()])) { | if (empty($allocated_phids[$binding->getPHID()])) { | ||||
$free[] = $binding; | $free[] = $binding; | ||||
} | } | ||||
} | } | ||||
$this->freeBindings = $free; | $this->freeBindings = $free; | ||||
} | } | ||||
Show All 37 Lines |