All files / server/src index.ts

60% Statements 21/35
0% Branches 0/8
100% Functions 5/5
60% Lines 21/35

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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92                        5x 5x     5x 5x                   5x     5x               5x 5x       5x 5x       5x 5x             5x     5x         5x                     5x 5x 5x           5x 5x           5x  
import { Server } from "node:http";
import { startServer, closeServer } from "server/utils/server";
import { logger } from "server/utils/logger";
import { startCronJobs } from "server/utils/cron";
import { config } from "shared/config";
import { initializeDayjs } from "shared/utils/initializeDayjs";
import {
  createNotificationQueueService,
  setGlobalNotificationQueueService,
} from "./utils/notificationQueue";
import { EmailSender } from "server/features/notifications/email";
 
const startApp = async (): Promise<void> => {
  initializeDayjs();
 
  let server: Server;
  try {
    server = await startServer({
      dbConnString: config.server().dbConnString,
      port: config.server().port,
    });
  } catch (error) {
    logger.error(new Error("Starting server failed", { cause: error }));
    return;
  }
 
  const enableCronjobs =
    config.server().onlyCronjobs ||
    config.server().cronjobsAndBackendSameInstance;
 
  if (enableCronjobs) {
    logger.info("Start enabled cronjobs");
    try {
      await startCronJobs();
    } catch (error) {
      logger.error(new Error("Error starting cronjobs", { cause: error }));
    }
  }
  if (!enableCronjobs) {
    logger.info("Cronjobs not started, set ONLY_CRONJOBS to enable cronjobs");
  }
 
  // Initialize notification queue
  try {
    const notificationQueueService = createNotificationQueueService(
      new EmailSender(),
      config.server().emailNotificationQueueWorkerCount,
    );
    setGlobalNotificationQueueService(notificationQueueService);
    logger.info("Email notification queue initialized.");
  } catch (error) {
    logger.error(
      new Error("Failed to initialize notification queue!", { cause: error }),
    );
  }
 
  process.once("SIGINT", (signal: string) => {
    void handleShutdown(server, signal);
  });
  process.once("SIGTERM", (signal: string) => {
    void handleShutdown(server, signal);
  });
};
 
const handleShutdown = async (
  server: Server,
  signal: string,
): Promise<void> => {
  try {
    await closeServer(server, signal);
  } catch (error: unknown) {
    logger.error(error);
  }
};
 
const init = async (): Promise<void> => {
  if (typeof process.env.NODE_ENV === "string") {
    logger.info(`Node environment: ${process.env.NODE_ENV}`);
  } else {
    // eslint-disable-next-line no-restricted-syntax -- Server startup
    throw new TypeError("Node environment NODE_ENV missing");
  }
 
  try {
    await startApp();
  } catch (error: unknown) {
    logger.error(error);
  }
};
 
await init();