-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
53 lines (45 loc) · 1.31 KB
/
server.js
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
const app = require("./app");
const dotenv = require("dotenv");
const cron = require('node-cron')
const User = require('./models/index').users
const { Op } = require('sequelize')
const logger = require("./utils/logger");
dotenv.config({ path: "./env" });
//CONFIGURE UNCAUGHT EXCEPTIONS
process.on("uncaughtException", (err) => {
console.log("UNCAUGHT EXCEPTION! 🔥 Shutting Down...");
console.log(err.name, err.message);
process.exit(1);
});
//START SERVER
const PORT = process.env.PORT || 3310;
// ---> CRON JOB
// DEFINE TASK SCHEDULER FOR ACCOUNT DELETION
const DeletionScheduler = cron.schedule('0 23 * * *', async () => { // RUN AT 23:00 EVERYDAY
// DELETE: ANY ACCOUNT DUE FOR DELETION
try {
const result = await User.destroy({
where: {
deletionDate: { [Op.lt]: Date.now() }
}
})
if (result) {
logger.info(`DELETED ${result} ACCOUNT(S)!`);
}
} catch (error) {
logger.error(error);
}
})
const server = app.listen(PORT, () => {
console.log(`Server is running on PORT: ${PORT}`);
// START SCHEDULER
DeletionScheduler.start()
});
//CONFIGURE UNHANDLED REJECTIONS
process.on("unhandledRejection", (err) => {
console.log("UNHANDLED REJECTION! 🔥 Shutting Down...");
console.log(err.name, err.message);
server.close(() => {
process.exit(1);
});
});