Skip to content

Commit

Permalink
feat: adding clear varnish (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Allan Chappell authored Aug 1, 2024
1 parent 3fb7ac0 commit 29e2349
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
12 changes: 12 additions & 0 deletions common/post-code-deploy/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,16 @@ else
"$HELPER_SCRIPT_PATH/deploy.sh" "$target_env"
fi

pushd "$PROJECT_ROOT" || exit 1

echo "Finding domains that use Varnish."
DOMAINS="$( ACLI_COMMAND="$acli" php "$HELPER_SCRIPT_PATH/get-env-domains.php" "$site.$target_env" )"
echo "Clearing Varnish cache for $DOMAINS"
# DOMAINS is an argument list. Purposfully not quoting it.
TMP_FILE=$(mktemp --suffix=.json)
$acli api:environments:clear-caches "$site.$target_env" $DOMAINS > "$TMP_FILE"
ACLI_MAX_TIMEOUT=60 ACLI_DELAY=5 ACLI_COMMAND="$acli" php "$HELPER_SCRIPT_PATH/wait-for-notification.php" "$TMP_FILE"

popd || exit 1

echo "Ending Build for $target_env"
19 changes: 19 additions & 0 deletions helper/get-env-domains.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

$acli = getenv('ACLI_COMMAND') ? getenv('ACLI_COMMAND') : "../../vendor/bin/acli -n";
$hostnames = [];
if (isset($argc)) {
if (isset($argv[1])) {
$site_alias = $argv[1];
$json = shell_exec($acli . " api:environments:domain-list " . escapeshellarg($site_alias));
$domain_info = json_decode($json, TRUE);
foreach ($domain_info as $domain) {
if (!empty($domain['hostname'])) {
if ($domain['flags']['active']) {
$hostnames[] = $domain['hostname'];
}
}
}
echo implode(' ', $hostnames);
}
}
20 changes: 10 additions & 10 deletions helper/wait-for-notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
if (!empty($json['_links']) && !empty($json['_links']['notification']) && !empty($json['_links']['notification']['href'])) {
$notification_id = basename($json['_links']['notification']['href']);
$message = $json['message'] ?? $message;
}
$start = time();
do {
sleep($delay);
$notification_json = shell_exec($acli . ' api:notifications:find ' . $notification_id);
$notification = json_decode($notification_json, TRUE);
echo "Task: " . $message . "\nProgress: " . $notification['progress'] . "% Status: " . $notification['status'] . "\n";
} while ($notification['status'] != 'completed' && time() <= ($start + $max_timeout));
if (time() > ($start + $max_timeout)) {
echo "Max Timeout Reached!!!\n";
$start = time();
do {
sleep($delay);
$notification_json = shell_exec($acli . ' api:notifications:find ' . $notification_id);
$notification = json_decode($notification_json, TRUE);
echo "Task: " . $message . "\nProgress: " . $notification['progress'] . "% Status: " . $notification['status'] . "\n";
} while ($notification['status'] != 'completed' && time() <= ($start + $max_timeout));
if (time() > ($start + $max_timeout)) {
echo "Max Timeout Reached!!!\n";
}
}
}
}

0 comments on commit 29e2349

Please sign in to comment.