1. /**
  2. * Extracts task queue values for staging environments with basic sanity checks.
  3. *
  4. * @returns {Promise<object>} An object containing staging environment task queues and their values.
  5. * Returns an empty object if no staging environments are found.
  6. * @throws {Error} If there's an error accessing configuration or processing data.
  7. */
  8. async function getStagingTaskQueues() {
  9. try {
  10. // 1. Fetch configuration data (e.g., environment variables, config file)
  11. const config = await fetchConfig(); // Replace with your config fetching logic
  12. // 2. Filter for staging environments
  13. const stagingEnvironments = config?.environments?.filter(
  14. (env) => env.environment === 'staging'
  15. );
  16. if (!stagingEnvironments || stagingEnvironments.length === 0) {
  17. console.warn('No staging environments configured.');
  18. return {}; // Return empty object if no staging environments are found
  19. }
  20. // 3. Extract task queue values for each staging environment
  21. const stagingTaskQueues = {};
  22. for (const env of stagingEnvironments) {
  23. const taskQueues = env.taskQueues;
  24. if (!taskQueues || !Array.isArray(taskQueues)) {
  25. console.warn(`Invalid task queues for environment ${env.environment}. Skipping.`);
  26. continue;
  27. }
  28. const validatedQueues = taskQueues.map(queue => {
  29. //Basic sanity check: Ensure queue name is a string
  30. if (typeof queue !== 'string') {
  31. console.warn(`Invalid queue name: ${queue} for environment ${env.environment}. Skipping.`);
  32. return null;
  33. }
  34. return queue.trim(); //remove leading/trailing whitespace
  35. }).filter(queue => queue !== null); //filter out invalid queue names
  36. if (validatedQueues.length > 0) {
  37. stagingTaskQueues[env.environment] = validatedQueues;
  38. }
  39. }
  40. return stagingTaskQueues;
  41. } catch (error) {
  42. console.error('Error fetching or processing staging task queues:', error);
  43. throw error; // Re-throw the error for handling upstream
  44. }
  45. }
  46. /**
  47. * Placeholder function to fetch configuration data. Replace with your actual
  48. * configuration loading mechanism (e.g., reading environment variables,
  49. * reading a JSON file, calling an API).
  50. * @returns {Promise<object>} Configuration data.
  51. */
  52. async function fetchConfig() {
  53. // Replace with your actual configuration loading logic.
  54. //Example:
  55. return new Promise((resolve) => {
  56. setTimeout(() => {
  57. resolve({
  58. environments: [
  59. { environment: 'production', taskQueues: ['queue1', 'queue2'] },
  60. { environment: 'staging', taskQueues: ['stagingQueue1', 'stagingQueue2', 123, ' queue3 '] },
  61. { environment: 'development', taskQueues: ['devQueue1'] },
  62. ],
  63. });
  64. }, 50);
  65. });
  66. }
  67. export default getStagingTaskQueues;

Add your comment