mirror of
https://we.phorge.it/source/phorge.git
synced 2024-12-01 11:12:42 +01:00
61 lines
1.5 KiB
PHP
61 lines
1.5 KiB
PHP
|
<?php
|
||
|
|
||
|
final class DrydockLeaseUpdateWorker extends DrydockWorker {
|
||
|
|
||
|
protected function doWork() {
|
||
|
$lease_phid = $this->getTaskDataValue('leasePHID');
|
||
|
|
||
|
$hash = PhabricatorHash::digestForIndex($lease_phid);
|
||
|
$lock_key = 'drydock.lease:'.$hash;
|
||
|
|
||
|
$lock = PhabricatorGlobalLock::newLock($lock_key)
|
||
|
->lock(1);
|
||
|
|
||
|
$lease = $this->loadLease($lease_phid);
|
||
|
$this->updateLease($lease);
|
||
|
|
||
|
$lock->unlock();
|
||
|
}
|
||
|
|
||
|
private function updateLease(DrydockLease $lease) {
|
||
|
$commands = $this->loadCommands($lease->getPHID());
|
||
|
foreach ($commands as $command) {
|
||
|
if ($lease->getStatus() != DrydockLeaseStatus::STATUS_ACTIVE) {
|
||
|
// Leases can't receive commands before they activate or after they
|
||
|
// release.
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
$this->processCommand($lease, $command);
|
||
|
$command
|
||
|
->setIsConsumed(true)
|
||
|
->save();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private function processCommand(
|
||
|
DrydockLease $lease,
|
||
|
DrydockCommand $command) {
|
||
|
switch ($command->getCommand()) {
|
||
|
case DrydockCommand::COMMAND_RELEASE:
|
||
|
$this->releaseLease($lease);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private function releaseLease(DrydockLease $lease) {
|
||
|
$lease->openTransaction();
|
||
|
$lease
|
||
|
->setStatus(DrydockLeaseStatus::STATUS_RELEASED)
|
||
|
->save();
|
||
|
|
||
|
// TODO: Hold slot locks until destruction?
|
||
|
DrydockSlotLock::releaseLocks($lease->getPHID());
|
||
|
$lease->saveTransaction();
|
||
|
|
||
|
// TODO: Hook for resource release behaviors.
|
||
|
// TODO: Schedule lease destruction.
|
||
|
}
|
||
|
|
||
|
}
|