<?php
/**
* User Data Script Bootstrapper for Legacy Project
*
* This script manages the execution of user data scripts with retry logic.
*/
class UserDataBootstrapper
{
private $scriptPaths;
private $retryIntervals;
private $maxRetries;
/**
* Constructor
*
* @param array $scriptPaths Array of paths to user data scripts
* @param array $retryIntervals Array of retry intervals in seconds (one per script)
* @param int $maxRetries Maximum number of retries for each script
*/
public function __construct(array $scriptPaths, array $retryIntervals, int $maxRetries = 3)
{
$this->scriptPaths = $scriptPaths;
$this->retryIntervals = $retryIntervals;
$this->maxRetries = $maxRetries;
}
/**
* Executes a single user data script with retry logic.
*
* @param string $scriptPath Path to the script
* @param int $retryInterval Retry interval in seconds
* @return bool True on success, false on failure after all retries
*/
private function executeScriptWithRetry(string $scriptPath, int $retryInterval): bool
{
$retries = 0;
while ($retries < $this->maxRetries) {
try {
// Execute the script
$result = $this->runScript($scriptPath);
if ($result === true) {
return true; // Script executed successfully
} else {
$retries++;
echo "Script '$scriptPath' failed (attempt $retries). Retrying in $retryInterval seconds...\n";
sleep($retryInterval);
}
} catch (Exception $e) {
$retries++;
echo "Script '$scriptPath' failed (attempt $retries): " . $e->getMessage() . ". Retrying in $retryInterval seconds...\n";
sleep($retryInterval);
}
}
echo "Script '$scriptPath' failed after $this->maxRetries attempts.\n";
return false; // Script failed after all retries
}
/**
* Runs a single user data script.
*
* @param string $scriptPath Path to the script
* @return bool True on success, false on failure
* @throws Exception If the script execution fails
*/
private function runScript(string $scriptPath): bool
{
//Check if the script exists
if (!file_exists($scriptPath)) {
throw new Exception("Script file not found: " . $scriptPath);
}
// Execute the script and check for errors
$output = shell_exec("php " . escapeshellarg($scriptPath));
if ($output === null) {
throw new Exception("Script execution failed.");
}
if (strpos($output, 'Parse error') !== false) {
throw new Exception("Script execution failed with parse error.");
}
//Consider any other error condition here
return true;
}
/**
* Boots the user data scripts.
*/
public function bootstrap()
{
if (empty($this->scriptPaths) || empty($this->retryIntervals)) {
echo "Error: Script paths and retry intervals must be provided.\n";
return;
}
for ($i = 0; $i < count($this->scriptPaths); $i++) {
$scriptPath = $this->scriptPaths[$i];
$retryInterval = $this->retryIntervals[$i];
if (!$this->executeScriptWithRetry($scriptPath, $retryInterval)) {
echo "Bootstrapping failed for script: " . $scriptPath . "\n";
} else {
echo "Bootstrapping successful for script: " . $scriptPath . "\n";
}
}
}
}
// Example Usage:
// Define the paths to your user data scripts
$scriptPaths = [
'script1.php',
'script2.php',
'script3.php',
];
// Define the retry intervals in seconds for each script
$retryIntervals = [
2,
5,
1,
Add your comment