<?php
/**
* Compresses timestamp output with retry intervals for monitoring.
*
* @param callable $data_source A function that returns the data to be timestamped.
* @param int $retry_interval Seconds between retry attempts.
* @param int $max_retries Maximum number of retry attempts.
* @return string Compacted timestamped output.
*/
function compactTimestampedOutput(callable $data_source, int $retry_interval, int $max_retries): string
{
$retries = 0;
$output = '';
do {
$data = $data_source(); // Fetch data
if ($data !== false) { // Check for data availability
$timestamp = date('Y-m-d H:i:s'); // Current timestamp
$output .= $timestamp . ': ' . $data . "\n"; // Format and append output
$retries = 0; // Reset retry count on success
} else {
$retries++;
if ($retries > $max_retries) {
return 'ERROR: Max retries reached.'; // Handle max retries
}
sleep($retry_interval); // Wait before retrying
}
} while ($retries <= $max_retries);
return $output; // Return the accumulated output
}
Add your comment