Changeset View
Changeset View
Standalone View
Standalone View
src/applications/drydock/storage/DrydockLease.php
<?php | <?php | ||||
final class DrydockLease extends DrydockDAO | final class DrydockLease extends DrydockDAO | ||||
implements PhabricatorPolicyInterface { | implements PhabricatorPolicyInterface { | ||||
protected $resourceID; | protected $resourceID; | ||||
protected $resourceType; | protected $resourceType; | ||||
protected $until; | protected $until; | ||||
protected $ownerPHID; | protected $ownerPHID; | ||||
protected $attributes = array(); | protected $attributes = array(); | ||||
protected $status = DrydockLeaseStatus::STATUS_PENDING; | protected $status = DrydockLeaseStatus::STATUS_PENDING; | ||||
protected $taskID; | protected $taskID; | ||||
private $resource = self::ATTACHABLE; | private $resource = self::ATTACHABLE; | ||||
private $releaseOnDestruction; | private $releaseOnDestruction; | ||||
private $isAcquired = false; | |||||
private $activateWhenAcquired = false; | |||||
/** | /** | ||||
* Flag this lease to be released when its destructor is called. This is | * Flag this lease to be released when its destructor is called. This is | ||||
* mostly useful if you have a script which acquires, uses, and then releases | * mostly useful if you have a script which acquires, uses, and then releases | ||||
* a lease, as you don't need to explicitly handle exceptions to properly | * a lease, as you don't need to explicitly handle exceptions to properly | ||||
* release the lease. | * release the lease. | ||||
*/ | */ | ||||
public function releaseOnDestruction() { | public function releaseOnDestruction() { | ||||
▲ Show 20 Lines • Show All 104 Lines • ▼ Show 20 Lines | public function release() { | ||||
$this->resource = null; | $this->resource = null; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function isActive() { | public function isActive() { | ||||
switch ($this->status) { | switch ($this->status) { | ||||
case DrydockLeaseStatus::STATUS_ACQUIRED: | |||||
case DrydockLeaseStatus::STATUS_ACTIVE: | case DrydockLeaseStatus::STATUS_ACTIVE: | ||||
case DrydockLeaseStatus::STATUS_ACQUIRING: | |||||
return true; | return true; | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
private function assertActive() { | private function assertActive() { | ||||
if (!$this->isActive()) { | if (!$this->isActive()) { | ||||
throw new Exception( | throw new Exception( | ||||
Show All 20 Lines | while (true) { | ||||
break; | break; | ||||
case DrydockLeaseStatus::STATUS_RELEASED: | case DrydockLeaseStatus::STATUS_RELEASED: | ||||
throw new Exception(pht('Lease has already been released!')); | throw new Exception(pht('Lease has already been released!')); | ||||
case DrydockLeaseStatus::STATUS_EXPIRED: | case DrydockLeaseStatus::STATUS_EXPIRED: | ||||
throw new Exception(pht('Lease has already expired!')); | throw new Exception(pht('Lease has already expired!')); | ||||
case DrydockLeaseStatus::STATUS_BROKEN: | case DrydockLeaseStatus::STATUS_BROKEN: | ||||
throw new Exception(pht('Lease has been broken!')); | throw new Exception(pht('Lease has been broken!')); | ||||
case DrydockLeaseStatus::STATUS_PENDING: | case DrydockLeaseStatus::STATUS_PENDING: | ||||
case DrydockLeaseStatus::STATUS_ACQUIRING: | case DrydockLeaseStatus::STATUS_ACQUIRED: | ||||
break; | break; | ||||
default: | default: | ||||
throw new Exception(pht('Unknown status??')); | throw new Exception(pht('Unknown status??')); | ||||
} | } | ||||
} | } | ||||
if ($unresolved) { | if ($unresolved) { | ||||
sleep(1); | sleep(1); | ||||
Show All 11 Lines | public function waitUntilActive() { | ||||
if (!$this->getID()) { | if (!$this->getID()) { | ||||
$this->queueForActivation(); | $this->queueForActivation(); | ||||
} | } | ||||
self::waitForLeases(array($this)); | self::waitForLeases(array($this)); | ||||
return $this; | return $this; | ||||
} | } | ||||
public function setActivateWhenAcquired($activate) { | |||||
$this->activateWhenAcquired = true; | |||||
return $this; | |||||
} | |||||
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_PENDING; | |||||
} | |||||
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 | |||||
->setResourceID($resource->getID()) | |||||
->setStatus($new_status) | |||||
->save(); | |||||
$this->isAcquired = true; | |||||
return $this; | |||||
} | |||||
public function isAcquiredLease() { | |||||
return $this->isAcquired; | |||||
} | |||||
/* -( PhabricatorPolicyInterface )----------------------------------------- */ | /* -( PhabricatorPolicyInterface )----------------------------------------- */ | ||||
public function getCapabilities() { | public function getCapabilities() { | ||||
return array( | return array( | ||||
PhabricatorPolicyCapability::CAN_VIEW, | PhabricatorPolicyCapability::CAN_VIEW, | ||||
); | ); | ||||
Show All 21 Lines |