All files / src/services queueService.server.ts

65.9% Statements 58/88
100% Branches 6/6
92.85% Functions 13/14
76.56% Lines 49/64

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 652x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 28x 4x 4x 2x 4x 6x 6x 6x 6x 4x 4x 4x 4x 4x 4x     40x   4x 40x 3x       3x       4x 2x   2x     4x    
// src/services/queueService.server.ts
import { logger } from './logger.server';
import { connection } from './redis.server';
import {
  flyerQueue,
  emailQueue,
  analyticsQueue,
  weeklyAnalyticsQueue,
  cleanupQueue,
  tokenCleanupQueue,
  receiptQueue,
  expiryAlertQueue,
  barcodeQueue,
} from './queues.server';
 
// Re-export everything for backward compatibility where possible
export { connection } from './redis.server';
export * from './queues.server';
 
// We do NOT export workers here anymore to prevent side effects.
// Consumers needing workers must import from './workers.server'.
 
/**
 * A function to gracefully shut down all queues and connections.
 * This is for the API process which only uses queues.
 * For worker processes, use the gracefulShutdown from workers.server.ts
 */
export const gracefulShutdown = async (signal: string) => {
  logger.info(`[Shutdown] Received ${signal}. Closing all queues...`);
  let exitCode = 0; // Default to success
 
  const resources = [
    { name: 'flyerQueue', close: () => flyerQueue.close() },
    { name: 'emailQueue', close: () => emailQueue.close() },
    { name: 'analyticsQueue', close: () => analyticsQueue.close() },
    { name: 'cleanupQueue', close: () => cleanupQueue.close() },
    { name: 'weeklyAnalyticsQueue', close: () => weeklyAnalyticsQueue.close() },
    { name: 'tokenCleanupQueue', close: () => tokenCleanupQueue.close() },
    { name: 'receiptQueue', close: () => receiptQueue.close() },
    { name: 'expiryAlertQueue', close: () => expiryAlertQueue.close() },
    { name: 'barcodeQueue', close: () => barcodeQueue.close() },
    { name: 'redisConnection', close: () => connection.quit() },
  ];

  const results = await Promise.allSettled(resources.map((r) => r.close()));

  results.forEach((result, index) => {
    if (result.status === 'rejected') {
      logger.error(
        { err: result.reason, resource: resources[index].name },
        `[Shutdown] Error closing resource.`,
      );
      exitCode = 1; // Mark shutdown as failed
    }
  });

  if (exitCode === 0) {
    logger.info('[Shutdown] All queues and connections closed successfully.');
  } else {
    logger.warn('[Shutdown] Graceful shutdown completed with errors.');
  }

  process.exit(exitCode);
};