1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-09-16 15:28:48 +02:00

When bin/drydock lease is interrupted, release leases

Summary:
Depends on D19072. Ref T13073. Currently, you can leave leases stranded by using `^C` to interrupt the script. Handle signals and release leases on destruction if they haven't activated yet.

Also, print out more useful information before and after activation.

Test Plan: Mashed ^C while runnning `bin/drydock lease ... --trace`, saw the lease release.

Subscribers: yelirekim, PHID-OPKG-gm6ozazyms6q6i22gyam

Maniphest Tasks: T13073

Differential Revision: https://secure.phabricator.com/D19073
This commit is contained in:
epriestley 2018-02-13 04:14:48 -08:00
parent b833e324bd
commit 07028cfc30
6 changed files with 54 additions and 8 deletions

View file

@ -2,7 +2,7 @@
<?php
$root = dirname(dirname(dirname(__FILE__)));
require_once $root.'/scripts/__init_script__.php';
require_once $root.'/scripts/init/init-script-with-signals.php';
$args = new PhutilArgumentParser($argv);
$args->setTagline(pht('manage drydock software resources'));

View file

@ -0,0 +1,11 @@
<?php
// Initialize a script that will handle signals.
if (function_exists('pcntl_async_signals')) {
pcntl_async_signals(true);
} else {
declare(ticks = 1);
}
require_once dirname(__FILE__).'/init-script.php';

View file

@ -206,7 +206,7 @@ final class DrydockWorkingCopyBlueprintImplementation
}
// Destroy the lease on the host.
$lease->releaseOnDestruction();
$lease->setReleaseOnDestruction(true);
if ($lease->isActive()) {
// Destroy the working copy on disk.

View file

@ -84,21 +84,51 @@ final class DrydockManagementLeaseWorkflow
$lease->setUntil($until);
}
// If something fatals or the user interrupts the process (for example,
// with "^C"), release the lease. We'll cancel this below, if the lease
// actually activates.
$lease->setReleaseOnDestruction(true);
// TODO: This would probably be better handled with PhutilSignalRouter,
// but it currently doesn't route SIGINT. We're initializing it to setup
// SIGTERM handling and make eventual migration easier.
$router = PhutilSignalRouter::getRouter();
pcntl_signal(SIGINT, array($this, 'didReceiveInterrupt'));
$t_start = microtime(true);
$lease->queueForActivation();
echo tsprintf(
"%s\n",
"%s\n\n __%s__\n\n%s\n",
pht('Queued lease for activation:'),
PhabricatorEnv::getProductionURI($lease->getURI()),
pht('Waiting for daemons to activate lease...'));
$this->waitUntilActive($lease);
// Now that we've survived activation and the lease is good, make it
// durable.
$lease->setReleaseOnDestruction(false);
$t_end = microtime(true);
echo tsprintf(
"%s\n",
pht('Activated lease "%s".', $lease->getID()));
"%s\n\n %s\n\n%s\n",
pht(
'Activation complete. This lease is permanent until manually '.
'released with:'),
pht('$ ./bin/drydock release-lease --id %d', $lease->getID()),
pht(
'Lease activated in %sms.',
new PhutilNumber((int)(($t_end - $t_start) * 1000))));
return 0;
}
public function didReceiveInterrupt($signo) {
// Doing this makes us run destructors, particularly the "release on
// destruction" trigger on the lease.
exit(128 + $signo);
}
private function waitUntilActive(DrydockLease $lease) {
$viewer = $this->getViewer();

View file

@ -36,8 +36,8 @@ final class DrydockLease extends DrydockDAO
* a lease, as you don't need to explicitly handle exceptions to properly
* release the lease.
*/
public function releaseOnDestruction() {
$this->releaseOnDestruction = true;
public function setReleaseOnDestruction($release) {
$this->releaseOnDestruction = $release;
return $this;
}
@ -436,6 +436,11 @@ final class DrydockLease extends DrydockDAO
return $this;
}
public function getURI() {
$id = $this->getID();
return "/drydock/lease/{$id}/";
}
/* -( PhabricatorPolicyInterface )----------------------------------------- */

View file

@ -62,7 +62,7 @@ final class DrydockRepositoryOperationUpdateWorker
DrydockCommandInterface::INTERFACE_TYPE);
// No matter what happens here, destroy the lease away once we're done.
$lease->releaseOnDestruction(true);
$lease->setReleaseOnDestruction(true);
$operation->applyOperation($interface);