1. /**
  2. * Compares configuration values with a timeout for monitoring purposes.
  3. *
  4. * @param {object} configValues - An object containing the configuration values to compare.
  5. * @param {object} expectedValues - An object containing the expected values for comparison.
  6. * @param {number} timeout - The timeout in milliseconds for the comparison.
  7. * @param {function} callback - A callback function to execute if the values do not match within the timeout.
  8. * @returns {Promise<boolean>} A promise that resolves to true if the values match within the timeout,
  9. * and false otherwise.
  10. */
  11. async function compareConfigValuesWithTimeout(configValues, expectedValues, timeout, callback) {
  12. const startTime = Date.now();
  13. const checkValue = (key) => {
  14. if (configValues[key] !== expectedValues[key]) {
  15. return false;
  16. }
  17. return true;
  18. };
  19. for (const key in expectedValues) {
  20. if (!checkValue(key)) {
  21. if (Date.now() - startTime > timeout) {
  22. await callback(false, key); // Execute callback if timeout is reached
  23. }
  24. return false; // Return false immediately if a mismatch is found
  25. }
  26. }
  27. return true; // Return true if all values match within the timeout
  28. }

Add your comment