import { env } from "./config/env.js";
import { prisma } from "./db/prisma.js";
import { closeRedisConnection } from "./lib/redis.js";
import { app } from "./app.js";
import { startSettlementWorker } from "./modules/workers/settlement.worker.js";

const server = app.listen(env.PORT, () => {
  console.log(`API запущен на порту ${env.PORT}`);
});

const interval = startSettlementWorker();

async function shutdown() {
  clearInterval(interval);
  server.close();
  await closeRedisConnection();
  await prisma.$disconnect();
  process.exit(0);
}

process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
