-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
87 lines (81 loc) · 3.04 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
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
require("dotenv").config({ path: "./config.env" });
const mongoose = require("mongoose");
const app = require("./app");
const { AwcWeatherMetarSchema } = require("./models/weather/awcWeatherModel");
const schedule = require("node-schedule");
const { SecondaryConnection } = require("./secondaryDbConnection");
const RedisClient = require("./redis/RedisClient");
const { CronJob } = require("cron");
const { importMetarsToDB, importVatsimTrafficsToDb } = require("./index");
const logger = require("./logger/index");
const REDIS_VATSIM_URL =
process.env.NODE_ENV === "production"
? process.env.REDISCLOUD_VATSIM_TRAFFIC_URL
: process.env.REDISCLOUD_VATSIM_TRAFFIC_DEV;
const trafficRedisClient = new RedisClient();
const metarRedisClient = new RedisClient();
const Latest_AwcWeatherModel = SecondaryConnection.model(
"AwcWeatherMetarModel_Latest",
AwcWeatherMetarSchema
);
mongoose.set("strictQuery", false); //to avoid 'strictQuery' deprecation warning
const mongooseOptions = {
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 30000, // 30 seconds timeout
socketTimeoutMS: 45000 // 45 seconds timeout
};
mongoose.connect(`${process.env.DATABASE}`, mongooseOptions).then(() => {
logger.info("DB connected");
(async () => {
try {
await metarRedisClient.createRedisNodeConnection(process.env.REDISCLOUD_METAR_URL);
logger.info("Metar redis connected.");
await trafficRedisClient.createRedisNodeConnection(REDIS_VATSIM_URL);
logger.info("Vatsim Traffic Redis connected");
} catch (e) {
logger.error("Error connecting to Redis:%O", e);
}
})();
//Update FAA ATIS every 60 minutes
// schedule.scheduleJob("0 * * * *", async () => {
// try {
// await importFaaAtisToDB();
// } catch (e) {
// logger.error("Error occurred in scheduleJob:importFaaAtisToDB():", e);
// }
// });
schedule.scheduleJob("*/10 * * * *", async () => {
try {
await importMetarsToDB(Latest_AwcWeatherModel, metarRedisClient);
} catch (e) {
logger.error("Error occurred in scheduleJob:importMetarsToDB():", e);
}
});
//every 12 hours
// schedule.scheduleJob("0 0 0/12 1/1 * ? *", async () => {
// try {
// await importVatsimEventsToDb();
// } catch (e) {
// logger.error("Error occurred in scheduleJob:importVatsimEventsToDb():", e);
// }
// });
// every 20 seconds
CronJob.from({
cronTime: "*/30 * * * * *",
onTick: async () => {
try {
await importVatsimTrafficsToDb(trafficRedisClient);
} catch (e) {
logger.error("Error occurred in CronJob:importVatsimTrafficsToDB():%O", e);
}
},
start: true,
timeZone: "America/Los_Angeles",
runOnInit: true
});
});
const port = process.env.PORT || 80;
app.listen(port, () => {
logger.info(`Express starts on port ${port}`);
});