1. /**
  2. * Monitors scheduled queues for errors and provides basic input validation.
  3. *
  4. * @param {Array<Object>} queues An array of queue objects. Each queue object should have:
  5. * - name: (string) The name of the queue.
  6. * - tasks: (Array<Object>) An array of task objects. Each task object should have:
  7. * - name: (string) The name of the task.
  8. * - input: (Object) The input data for the task.
  9. * - execute: (function) The function to execute for the task.
  10. * @returns {Array<string>} An array of error messages. Empty if no errors found.
  11. */
  12. function checkQueuesForErrors(queues) {
  13. const errors = [];
  14. if (!Array.isArray(queues)) {
  15. errors.push("Input must be an array of queues.");
  16. return errors;
  17. }
  18. for (const queue of queues) {
  19. if (typeof queue !== 'object' || queue === null) {
  20. errors.push(`Invalid queue object: ${String(queue)}`);
  21. continue;
  22. }
  23. if (!queue.name || typeof queue.name !== 'string') {
  24. errors.push(`Queue name is missing or invalid: ${String(queue)}`);
  25. continue;
  26. }
  27. if (!Array.isArray(queue.tasks)) {
  28. errors.push(`Queue tasks must be an array: ${String(queue)}`);
  29. continue;
  30. }
  31. for (const task of queue.tasks) {
  32. if (typeof task !== 'object' || task === null) {
  33. errors.push(`Invalid task object: ${String(task)}`);
  34. continue;
  35. }
  36. if (!task.name || typeof task.name !== 'string') {
  37. errors.push(`Task name is missing or invalid: ${String(task)}`);
  38. continue;
  39. }
  40. if (typeof task.input !== 'object' || task.input === null) {
  41. errors.push(`Task input is missing or invalid: ${String(task)}`);
  42. continue;
  43. }
  44. if (typeof task.execute !== 'function') {
  45. errors.push(`Task execute function is missing or invalid: ${String(task)}`);
  46. continue;
  47. }
  48. try {
  49. //Attempt to execute the task to catch runtime errors
  50. task.execute(task.input);
  51. } catch (e) {
  52. errors.push(`Error executing task ${task.name} in queue ${queue.name}: ${String(e)}`);
  53. }
  54. }
  55. }
  56. return errors;
  57. }

Add your comment