Skip to content

Commit

Permalink
Convert loopy to static methods (#1729)
Browse files Browse the repository at this point in the history
  • Loading branch information
danepowell committed Apr 17, 2024
1 parent 0dc9add commit 7a9469f
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/Command/Ssh/SshKeyCommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ protected function pollAcquiaCloudUntilSshSuccess(
OutputInterface $output
): void {
// Create a loop to periodically poll the Cloud Platform.
$loop = Loop::get();
$timers = [];
$startTime = time();
$cloudAppUuid = $this->determineCloudApplication(TRUE);
Expand All @@ -121,20 +120,20 @@ protected function pollAcquiaCloudUntilSshSuccess(
$spinner = new Spinner($output, 4);
$spinner->setMessage("Waiting for the key to become available in Cloud Platform $envName environments");
$spinner->start();
$mappings[$envName]['timer'] = $loop->addPeriodicTimer($spinner->interval(),
$mappings[$envName]['timer'] = Loop::addPeriodicTimer($spinner->interval(),
static function () use ($spinner): void {
$spinner->advance();
});
$mappings[$envName]['spinner'] = $spinner;
}
$callback = function () use ($output, $loop, &$mappings, &$timers, $startTime): void {
$callback = function () use ($output, &$mappings, &$timers, $startTime): void {
foreach ($mappings as $envName => $config) {
try {
$process = $this->sshHelper->executeCommand($config['ssh_target'], ['ls'], FALSE);
if (($process->getExitCode() === 128 && $envName === 'git') || $process->isSuccessful()) {
// SSH key is available on this host, but may be pending on others.
$config['spinner']->finish();
$loop->cancelTimer($config['timer']);
Loop::cancelTimer($config['timer']);
unset($mappings[$envName]);
}
else {
Expand All @@ -151,24 +150,24 @@ static function () use ($spinner): void {
Amplitude::getInstance()->queueEvent('SSH key upload', ['result' => 'success', 'duration' => time() - $startTime]);
$output->writeln("\n<info>Your SSH key is ready for use!</info>\n");
foreach ($timers as $timer) {
$loop->cancelTimer($timer);
Loop::cancelTimer($timer);
}
$timers = [];
}
};
// Poll Cloud every 5 seconds.
$timers[] = $loop->addPeriodicTimer(5, $callback);
$timers[] = $loop->addTimer(0.1, $callback);
$timers[] = $loop->addTimer(60 * 60, function () use ($output, $loop, &$timers): void {
$timers[] = Loop::addPeriodicTimer(5, $callback);
$timers[] = Loop::addTimer(0.1, $callback);
$timers[] = Loop::addTimer(60 * 60, static function () use ($output, &$timers): void {
// Upload timed out.
$output->writeln("\n<comment>This is taking longer than usual. It will happen eventually!</comment>\n");
Amplitude::getInstance()->queueEvent('SSH key upload', ['result' => 'timeout']);
foreach ($timers as $timer) {
$loop->cancelTimer($timer);
Loop::cancelTimer($timer);
}
$timers = [];
});
$loop->run();
Loop::run();
}

/**
Expand Down

0 comments on commit 7a9469f

Please sign in to comment.