<?php
/**
* Wraps task execution to handle errors and retry with fixed intervals.
*
* @param callable $task The task to execute.
* @param int $maxRetries The maximum number of retry attempts.
* @param int $retryInterval The interval between retry attempts in seconds.
* @return bool True on success, false on failure after maxRetries.
* @throws Exception If the task throws an exception and maxRetries is reached.
*/
function wrapTask(callable $task, int $maxRetries = 3, int $retryInterval = 5): bool
{
$retries = 0;
do {
try {
return $task(); // Execute the task
} catch (\Exception $e) {
$retries++;
if ($retries > $maxRetries) {
throw $e; // Re-throw the exception if max retries reached
}
sleep($retryInterval); // Wait before retrying
}
} while (true); // Infinite loop until task succeeds or max retries are reached.
}
Add your comment