private function buildLeaseBox(DrydockResource $resource)
 {
     $viewer = $this->getViewer();
     $leases = id(new DrydockLeaseQuery())->setViewer($viewer)->withResourcePHIDs(array($resource->getPHID()))->withStatuses(array(DrydockLeaseStatus::STATUS_PENDING, DrydockLeaseStatus::STATUS_ACQUIRED, DrydockLeaseStatus::STATUS_ACTIVE))->setLimit(100)->execute();
     $id = $resource->getID();
     $leases_uri = "resource/{$id}/leases/query/all/";
     $leases_uri = $this->getApplicationURI($leases_uri);
     $lease_header = id(new PHUIHeaderView())->setHeader(pht('Active Leases'))->addActionLink(id(new PHUIButtonView())->setTag('a')->setHref($leases_uri)->setIconFont('fa-search')->setText(pht('View All Leases')));
     $lease_list = id(new DrydockLeaseListView())->setUser($viewer)->setLeases($leases)->render()->setNoDataString(pht('This resource has no active leases.'));
     return id(new PHUIObjectBoxView())->setHeader($lease_header)->setObjectList($lease_list);
 }
 private function releaseResource(DrydockResource $resource)
 {
     if ($resource->getStatus() != DrydockResourceStatus::STATUS_ACTIVE) {
         // If we had multiple release commands
         // This command is only meaningful to resources in the "Open" state.
         return;
     }
     $viewer = $this->getViewer();
     $drydock_phid = id(new PhabricatorDrydockApplication())->getPHID();
     $resource->openTransaction();
     $resource->setStatus(DrydockResourceStatus::STATUS_RELEASED)->save();
     // TODO: Hold slot locks until destruction?
     DrydockSlotLock::releaseLocks($resource->getPHID());
     $resource->saveTransaction();
     $statuses = array(DrydockLeaseStatus::STATUS_PENDING, DrydockLeaseStatus::STATUS_ACQUIRED, DrydockLeaseStatus::STATUS_ACTIVE);
     $leases = id(new DrydockLeaseQuery())->setViewer($viewer)->withResourcePHIDs(array($resource->getPHID()))->withStatuses($statuses)->execute();
     foreach ($leases as $lease) {
         $command = DrydockCommand::initializeNewCommand($viewer)->setTargetPHID($lease->getPHID())->setAuthorPHID($drydock_phid)->setCommand(DrydockCommand::COMMAND_RELEASE)->save();
         $lease->scheduleUpdate();
     }
     PhabricatorWorker::scheduleTask('DrydockResourceDestroyWorker', array('resourcePHID' => $resource->getPHID()), array('objectPHID' => $resource->getPHID()));
 }
 private function destroyResource(DrydockResource $resource)
 {
     $status = $resource->getStatus();
     switch ($status) {
         case DrydockResourceStatus::STATUS_RELEASED:
         case DrydockResourceStatus::STATUS_BROKEN:
             break;
         default:
             throw new PhabricatorWorkerPermanentFailureException(pht('Unable to destroy resource ("%s"), resource has the wrong ' . 'status ("%s").', $resource->getPHID(), $status));
     }
     $blueprint = $resource->getBlueprint();
     $blueprint->destroyResource($resource);
     $resource->setStatus(DrydockResourceStatus::STATUS_DESTROYED)->save();
 }
 /**
  * Make sure that a lease was really acquired properly.
  *
  * @param DrydockBlueprint Blueprint which created the resource.
  * @param DrydockResource Resource which was acquired.
  * @param DrydockLease The lease which was supposedly acquired.
  * @return void
  * @task acquire
  */
 private function validateAcquiredLease(DrydockBlueprint $blueprint, DrydockResource $resource, DrydockLease $lease)
 {
     if (!$lease->isAcquiredLease()) {
         throw new Exception(pht('Blueprint "%s" (of type "%s") is not properly implemented: it ' . 'returned from "%s" without acquiring a lease.', $blueprint->getBlueprintName(), $blueprint->getClassName(), 'acquireLease()'));
     }
     $lease_phid = $lease->getResourcePHID();
     $resource_phid = $resource->getPHID();
     if ($lease_phid !== $resource_phid) {
         throw new Exception(pht('Blueprint "%s" (of type "%s") is not properly implemented: it ' . 'returned from "%s" with a lease acquired on the wrong resource.', $blueprint->getBlueprintName(), $blueprint->getClassName(), 'acquireLease()'));
     }
 }
示例#5
0
 public function acquireOnResource(DrydockResource $resource)
 {
     $expect_status = DrydockLeaseStatus::STATUS_PENDING;
     $actual_status = $this->getStatus();
     if ($actual_status != $expect_status) {
         throw new Exception(pht('Trying to acquire a lease on a resource which is in the wrong ' . 'state: status must be "%s", actually "%s".', $expect_status, $actual_status));
     }
     if ($this->activateWhenAcquired) {
         $new_status = DrydockLeaseStatus::STATUS_ACTIVE;
     } else {
         $new_status = DrydockLeaseStatus::STATUS_ACQUIRED;
     }
     if ($new_status == DrydockLeaseStatus::STATUS_ACTIVE) {
         if ($resource->getStatus() == DrydockResourceStatus::STATUS_PENDING) {
             throw new Exception(pht('Trying to acquire an active lease on a pending resource. ' . 'You can not immediately activate leases on resources which ' . 'need time to start up.'));
         }
     }
     $this->openTransaction();
     $this->setResourcePHID($resource->getPHID())->setStatus($new_status)->save();
     DrydockSlotLock::acquireLocks($this->getPHID(), $this->slotLocks);
     $this->slotLocks = array();
     $this->saveTransaction();
     $this->isAcquired = true;
     if ($new_status == DrydockLeaseStatus::STATUS_ACTIVE) {
         $this->didActivate();
     }
     return $this;
 }
 private function getLeaseSlotLock(DrydockResource $resource)
 {
     $resource_phid = $resource->getPHID();
     return "almanac.host.lease({$resource_phid})";
 }
 private function getLeaseSlotLock(DrydockResource $resource)
 {
     $resource_phid = $resource->getPHID();
     return "workingcopy.lease({$resource_phid})";
 }
 /**
  * @task break
  */
 private function breakResource(DrydockResource $resource, Exception $ex)
 {
     switch ($resource->getStatus()) {
         case DrydockResourceStatus::STATUS_BROKEN:
         case DrydockResourceStatus::STATUS_RELEASED:
         case DrydockResourceStatus::STATUS_DESTROYED:
             // If the resource was already broken, just throw a normal exception.
             // This will retry the task eventually.
             throw new PhutilProxyException(pht('Unexpected failure while destroying resource ("%s").', $resource->getPHID()), $ex);
     }
     $resource->setStatus(DrydockResourceStatus::STATUS_BROKEN)->save();
     $resource->scheduleUpdate();
     $resource->logEvent(DrydockResourceActivationFailureLogType::LOGCONST, array('class' => get_class($ex), 'message' => $ex->getMessage()));
     throw new PhabricatorWorkerPermanentFailureException(pht('Permanent failure while activating resource ("%s"): %s', $resource->getPHID(), $ex->getMessage()));
 }
示例#9
0
 protected function reclaimResource(DrydockResource $resource, DrydockLease $lease)
 {
     $viewer = $this->getViewer();
     $command = DrydockCommand::initializeNewCommand($viewer)->setTargetPHID($resource->getPHID())->setAuthorPHID($lease->getPHID())->setCommand(DrydockCommand::COMMAND_RECLAIM)->save();
     $resource->scheduleUpdate();
     return $this;
 }