<?php
use Firebase\Functional\Functions\Functions;
use Firebase\Functional\Functions\Function;
use Firebase\Functional\Functions\FunctionOptions;
use Firebase\Functional\Functions\FunctionRunner;
use Firebase\Functional\Functions\RetryOptions;
/**
* Schedules execution of JSON payloads with retry logic.
*
* @param array $payload The JSON payload to execute.
* @param string $function_name The name of the function to execute.
* @param string $function_path The path to the function file.
* @param int $retries The number of retries.
* @param int $delay The delay between retries in seconds.
* @return bool True on success, false on failure.
*/
function scheduleAndExecute(array $payload, string $function_name, string $function_path, int $retries = 3, int $delay = 5): bool
{
try {
// Define retry options
$retry_options = new RetryOptions([
'maxAttempts' => $retries,
'delayBetweenAttempts' => $delay,
]);
// Create a function runner
$function_runner = new FunctionRunner();
// Execute the function with retry logic
$result = $function_runner->run(
$function_name,
$function_path,
$payload,
$retry_options
);
// Check if the function execution was successful
if ($result === true) {
return true;
} else {
return false;
}
} catch (\Exception $e) {
// Log the error
error_log("Error executing function: " . $e->getMessage());
return false;
}
}
/**
* Example Usage (Illustrative)
*/
// Sample payload
$data = ['key1' => 'value1', 'key2' => 'value2'];
// Function details
$functionName = 'my_function';
$functionPath = 'path/to/my_function.php'; // Replace with your function path
// Schedule and execute the function
$success = scheduleAndExecute($data, $functionName, $functionPath);
if ($success) {
echo "Function executed successfully.\n";
} else {
echo "Function execution failed.\n";
}
?>
Add your comment