1. <?php
  2. /**
  3. * Compresses timestamp output with retry intervals for monitoring.
  4. *
  5. * @param callable $data_source A function that returns the data to be timestamped.
  6. * @param int $retry_interval Seconds between retry attempts.
  7. * @param int $max_retries Maximum number of retry attempts.
  8. * @return string Compacted timestamped output.
  9. */
  10. function compactTimestampedOutput(callable $data_source, int $retry_interval, int $max_retries): string
  11. {
  12. $retries = 0;
  13. $output = '';
  14. do {
  15. $data = $data_source(); // Fetch data
  16. if ($data !== false) { // Check for data availability
  17. $timestamp = date('Y-m-d H:i:s'); // Current timestamp
  18. $output .= $timestamp . ': ' . $data . "\n"; // Format and append output
  19. $retries = 0; // Reset retry count on success
  20. } else {
  21. $retries++;
  22. if ($retries > $max_retries) {
  23. return 'ERROR: Max retries reached.'; // Handle max retries
  24. }
  25. sleep($retry_interval); // Wait before retrying
  26. }
  27. } while ($retries <= $max_retries);
  28. return $output; // Return the accumulated output
  29. }

Add your comment