projeto-hit/backend/src/server.ts

56 lines
1.1 KiB
TypeScript

import gracefulShutdown from "http-graceful-shutdown";
import app from "./app";
import { initIO, getIO } from "./libs/socket";
import { logger } from "./utils/logger";
import { StartAllWhatsAppsSessions } from "./services/WbotServices/StartAllWhatsAppsSessions";
import User from "./models/User";
const server = app.listen(process.env.PORT, () => {
logger.info(`Server started on port: ${process.env.PORT}`);
});
if (global.gc) {
console.log(">> Starting Garbage Collector...");
global.gc();
} else {
console.warn('No GC hook! Start your program as `node --expose-gc file.js`.');
}
initIO(server);
// StartAllWhatsAppsSessions();
gracefulShutdown(server);
setTimeout(async () => {
const io = getIO();
console.log('Triggered socket!')
let users = await User.findAll({ raw: true, attributes: ["id"], })
if (users && users.length > 0) {
for (let i = 0; i < users.length; i++) {
io.emit("reload_page", {
action: "update",
userId: users[i].id
});
console.log('USER ID: ', users[i].id)
await new Promise(f => setTimeout(f, 500));
}
}
}, 5000)