api: boot background worker queues
We might need to place it somewhere else though
Pedro Lucas Porcellis porcellis@eletrotupi.com · 5 months ago
9491cee07e5a589cb6ccb77be219b7fff62b924e Browse filesParents
d5dbfbdChanged files
1
Additions
+12
Deletions
0
api/src/createApp.ts
1 1
import express from 'express';
2 2
import cors from 'cors';
3 3
import bodyParser from 'body-parser';
4 4
import morgan from 'morgan';
5 +
import mainRouter from '@app/routes/main';
5 6
import userRouter from '@app/routes/users';
6 7
import authRouter from '@app/routes/auth';
7 8
import { errorHandler } from '@app/middleware/errorHandler';
8 9
import { PrismaClient } from '@prisma/client';
10 +
import { bootWorkers, closeAllWorkers, closeAllQueues } from '@app/lib/queue';
9 11
10 12
export function createApp() {
11 13
const app = express();
14 +
bootWorkers();
12 15
13 16
// Configure CORS
14 17
app.use(cors({
15 18
origin: process.env.FRONTEND_URL || '*',
16 19
credentials: true
21 24
// Only use morgan in non-test environments
22 25
if (process.env.NODE_ENV !== 'test') {
23 26
app.use(morgan('combined'));
24 27
}
25 28
29 +
app.get('/', mainRouter);
26 30
app.use("/users", userRouter);
27 31
app.use("/auth", authRouter);
28 32
29 33
app.use(errorHandler); // always last
30 34
35 +
process.on('SIGTERM', shutdown);
36 +
process.on('SIGINT', shutdown);
37 +
31 38
return app;
32 39
}
40 +
41 +
const shutdown = async () => {
42 +
await Promise.all([closeAllWorkers(), closeAllQueues()]);
43 +
process.exit(0);
44 +
};