Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LOPS-2119 - Wait for a healthcheck before releasing the command #2564

Merged
merged 11 commits into from
Apr 9, 2024
2 changes: 2 additions & 0 deletions config/constants.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,5 @@ TERMINUS_VCR_MODE: null
TERMINUS_CLIENT_OPTIONS:
debug: false
http_errors: false

TERMINUS_WAIT_FOR_WAKE_REPEAT: 25
10 changes: 9 additions & 1 deletion src/Commands/Site/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Pantheon\Terminus\Commands\WorkflowProcessingTrait;
use Pantheon\Terminus\Exceptions\TerminusException;
use Pantheon\Terminus\Helpers\Traits\WaitForWakeTrait;
use Pantheon\Terminus\Models\Environment;

/**
* Class CreateCommand.
Expand All @@ -13,6 +15,7 @@
class CreateCommand extends SiteCommand
{
use WorkflowProcessingTrait;
use WaitForWakeTrait;

/**
* Creates a new site.
Expand All @@ -33,6 +36,7 @@ class CreateCommand extends SiteCommand
*
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Pantheon\Terminus\Exceptions\TerminusException
* @throws \Exception
*/

public function create($site_name, $label, $upstream_id, $options = ['org' => null, 'region' => null,])
Expand Down Expand Up @@ -72,7 +76,11 @@ public function create($site_name, $label, $upstream_id, $options = ['org' => nu
if ($site = $this->getSiteById($workflow->get('waiting_for_task')->site_id)) {
$this->log()->notice('Deploying CMS...');
$this->processWorkflow($site->deployProduct($upstream->id));
$this->log()->notice('Deployed CMS');
$this->log()->notice('Waiting for site availability...');
$env = $site->getEnvironments()->get('dev');
if ($env instanceof Environment) {
$this->waitForWake($env, $this->logger);
}
}
}
}
40 changes: 40 additions & 0 deletions src/Helpers/Traits/WaitForWakeTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Pantheon\Terminus\Helpers\Traits;

use Pantheon\Terminus\Config\ConfigAwareTrait;
use Pantheon\Terminus\Exceptions\TerminusException;
use Pantheon\Terminus\Models\Environment;
use Psr\Log\LoggerInterface;

trait WaitForWakeTrait
{
use ConfigAwareTrait;

/**
* Waits for the site to wake up.
*
* @param Environment $env
* @param LoggerInterface $logger
* @throws TerminusException
*/
public function waitForWake(Environment $env, LoggerInterface $logger)
{
$waits = 0;
do {
$woke = $env->wake();
if (($woke['success'] ?? false) === true) {
break;
}
// if success is empty, then the site is still waking up.
// Allow user to set the number of retries if the site is still waking up.
// Default should be 25 times, once per second.
if ($waits > $this->getConfig()->get("wait_for_wake_repeat", 25)) {
throw new TerminusException('Could not confirm that the site is working; there might be a problem.');
}
sleep(1);
$waits++;
} while (true);
$logger->notice('Your site has been created successfully!');
}
}
6 changes: 3 additions & 3 deletions src/Models/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
use Pantheon\Terminus\Collections\Domains;
use Pantheon\Terminus\Collections\EnvironmentMetrics;
use Pantheon\Terminus\Collections\Workflows;
use Pantheon\Terminus\Helpers\LocalMachineHelper;
use Pantheon\Terminus\Exceptions\TerminusException;
use Pantheon\Terminus\Friends\SiteInterface;
use Pantheon\Terminus\Friends\SiteTrait;
use Pantheon\Terminus\Exceptions\TerminusException;
use Pantheon\Terminus\Helpers\LocalMachineHelper;

/**
* Class Environment
Expand Down Expand Up @@ -1000,7 +1000,7 @@ public function sftpConnectionInfo()
*
* @return array
*/
public function wake()
public function wake(): array
{
$domains = array_filter(
$this->getDomains()->all(),
Expand Down
Loading