-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
122 lines (107 loc) · 2.94 KB
/
index.js
File metadata and controls
122 lines (107 loc) · 2.94 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const app = require("express")();
const mode = process.env.NODE_ENV || "development";
const cors = require("cors");
const bodyParser = require("body-parser");
const loggerPath = require("morgan");
const mongoose = require("mongoose");
const logger = require("./libs/logs");
function InitServer() {
this.app = app;
this.run = function (server) {
const config = require("./config/app.json")[mode];
const host = config.app.host;
const port = config.app.port;
server.use(cors());
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({ extended: true }));
if (mode == "development") {
server.use(loggerPath("dev"));
}
server.use(require("./routes/index"));
server.listen(port, host, () =>
logger.info(`Application listen on host ${host} port ${port}`)
);
};
this.initDatabase = function (callback) {
const configDB = require("./config/database.json")[mode];
const authConnetion =
configDB.password != ""
? configDB.username + ":" + configDB.password + "@"
: "";
mongoose
.connect(
`mongodb://${authConnetion}${configDB.host}:${configDB.port}/${configDB.db}`
)
.then((db) => {
logger.info(
`Database connected on host ${configDB.host} port ${configDB.port}`
);
callback(null, db);
})
.catch((err) => {
callback(err, null);
});
};
this.initRedis = function () {
const configRedis = require("./config/redis.json")[mode];
const hostRedis = configRedis.host;
const portRedis = configRedis.port;
const dbRedis = configRedis.db;
const kue = require("kue");
const queue = kue.createQueue({
redis: {
host: hostRedis,
port: portRedis,
db: dbRedis,
},
});
// LISTEN QUEUE
global.queue = queue;
kue.app.set("title", "My Application");
kue.app.listen(3001);
process.on("uncaughtException", function (err) {
// console.error( 'Something bad happened: ', err );
queue.shutdown(1000, function (err2) {
logger.danger(
`Shutdown redis queue, error : ${err2 || "nothing error !"}`
);
process.exit(0);
});
});
[
`exit`,
`SIGINT`,
`SIGUSR1`,
`SIGUSR2`,
`uncaughtException`,
`SIGTERM`,
].forEach((eventType) => {
process.once(eventType, (sig) => {
logger.danger(`Application terminated, event: ${sig}`);
queue.active(function (err, ids) {
ids.forEach(function (id) {
kue.Job.get(id, function (err, job) {
// Your application should check if job is a stuck one
job.remove();
});
});
});
queue.inactive(function (err, ids) {
ids.forEach(function (id) {
kue.Job.get(id, function (err, job) {
// Your application should check if job is a stuck one
job.remove();
});
});
});
queue.shutdown(5000, function (err) {
logger.danger(
`Shutdown redis queue, error : ${err || "nothing error !"}`
);
});
});
});
logger.info(`Redis connected on host ${hostRedis} port ${portRedis}`);
};
}
module.exports = InitServer;