1. <?php
  2. /**
  3. * User Data Script Bootstrapper for Legacy Project
  4. *
  5. * This script manages the execution of user data scripts with retry logic.
  6. */
  7. class UserDataBootstrapper
  8. {
  9. private $scriptPaths;
  10. private $retryIntervals;
  11. private $maxRetries;
  12. /**
  13. * Constructor
  14. *
  15. * @param array $scriptPaths Array of paths to user data scripts
  16. * @param array $retryIntervals Array of retry intervals in seconds (one per script)
  17. * @param int $maxRetries Maximum number of retries for each script
  18. */
  19. public function __construct(array $scriptPaths, array $retryIntervals, int $maxRetries = 3)
  20. {
  21. $this->scriptPaths = $scriptPaths;
  22. $this->retryIntervals = $retryIntervals;
  23. $this->maxRetries = $maxRetries;
  24. }
  25. /**
  26. * Executes a single user data script with retry logic.
  27. *
  28. * @param string $scriptPath Path to the script
  29. * @param int $retryInterval Retry interval in seconds
  30. * @return bool True on success, false on failure after all retries
  31. */
  32. private function executeScriptWithRetry(string $scriptPath, int $retryInterval): bool
  33. {
  34. $retries = 0;
  35. while ($retries < $this->maxRetries) {
  36. try {
  37. // Execute the script
  38. $result = $this->runScript($scriptPath);
  39. if ($result === true) {
  40. return true; // Script executed successfully
  41. } else {
  42. $retries++;
  43. echo "Script '$scriptPath' failed (attempt $retries). Retrying in $retryInterval seconds...\n";
  44. sleep($retryInterval);
  45. }
  46. } catch (Exception $e) {
  47. $retries++;
  48. echo "Script '$scriptPath' failed (attempt $retries): " . $e->getMessage() . ". Retrying in $retryInterval seconds...\n";
  49. sleep($retryInterval);
  50. }
  51. }
  52. echo "Script '$scriptPath' failed after $this->maxRetries attempts.\n";
  53. return false; // Script failed after all retries
  54. }
  55. /**
  56. * Runs a single user data script.
  57. *
  58. * @param string $scriptPath Path to the script
  59. * @return bool True on success, false on failure
  60. * @throws Exception If the script execution fails
  61. */
  62. private function runScript(string $scriptPath): bool
  63. {
  64. //Check if the script exists
  65. if (!file_exists($scriptPath)) {
  66. throw new Exception("Script file not found: " . $scriptPath);
  67. }
  68. // Execute the script and check for errors
  69. $output = shell_exec("php " . escapeshellarg($scriptPath));
  70. if ($output === null) {
  71. throw new Exception("Script execution failed.");
  72. }
  73. if (strpos($output, 'Parse error') !== false) {
  74. throw new Exception("Script execution failed with parse error.");
  75. }
  76. //Consider any other error condition here
  77. return true;
  78. }
  79. /**
  80. * Boots the user data scripts.
  81. */
  82. public function bootstrap()
  83. {
  84. if (empty($this->scriptPaths) || empty($this->retryIntervals)) {
  85. echo "Error: Script paths and retry intervals must be provided.\n";
  86. return;
  87. }
  88. for ($i = 0; $i < count($this->scriptPaths); $i++) {
  89. $scriptPath = $this->scriptPaths[$i];
  90. $retryInterval = $this->retryIntervals[$i];
  91. if (!$this->executeScriptWithRetry($scriptPath, $retryInterval)) {
  92. echo "Bootstrapping failed for script: " . $scriptPath . "\n";
  93. } else {
  94. echo "Bootstrapping successful for script: " . $scriptPath . "\n";
  95. }
  96. }
  97. }
  98. }
  99. // Example Usage:
  100. // Define the paths to your user data scripts
  101. $scriptPaths = [
  102. 'script1.php',
  103. 'script2.php',
  104. 'script3.php',
  105. ];
  106. // Define the retry intervals in seconds for each script
  107. $retryIntervals = [
  108. 2,
  109. 5,
  110. 1,

Add your comment