/**
* Monitors scheduled queues for errors and provides basic input validation.
*
* @param {Array<Object>} queues An array of queue objects. Each queue object should have:
* - name: (string) The name of the queue.
* - tasks: (Array<Object>) An array of task objects. Each task object should have:
* - name: (string) The name of the task.
* - input: (Object) The input data for the task.
* - execute: (function) The function to execute for the task.
* @returns {Array<string>} An array of error messages. Empty if no errors found.
*/
function checkQueuesForErrors(queues) {
const errors = [];
if (!Array.isArray(queues)) {
errors.push("Input must be an array of queues.");
return errors;
}
for (const queue of queues) {
if (typeof queue !== 'object' || queue === null) {
errors.push(`Invalid queue object: ${String(queue)}`);
continue;
}
if (!queue.name || typeof queue.name !== 'string') {
errors.push(`Queue name is missing or invalid: ${String(queue)}`);
continue;
}
if (!Array.isArray(queue.tasks)) {
errors.push(`Queue tasks must be an array: ${String(queue)}`);
continue;
}
for (const task of queue.tasks) {
if (typeof task !== 'object' || task === null) {
errors.push(`Invalid task object: ${String(task)}`);
continue;
}
if (!task.name || typeof task.name !== 'string') {
errors.push(`Task name is missing or invalid: ${String(task)}`);
continue;
}
if (typeof task.input !== 'object' || task.input === null) {
errors.push(`Task input is missing or invalid: ${String(task)}`);
continue;
}
if (typeof task.execute !== 'function') {
errors.push(`Task execute function is missing or invalid: ${String(task)}`);
continue;
}
try {
//Attempt to execute the task to catch runtime errors
task.execute(task.input);
} catch (e) {
errors.push(`Error executing task ${task.name} in queue ${queue.name}: ${String(e)}`);
}
}
}
return errors;
}
Add your comment