-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.js
70 lines (54 loc) · 2.15 KB
/
collector.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
require("dotenv").config();
const {db_connect, db_close} = require("./Database/db_handler");
const RapidAPIAutomator = require("./Library/Automators/RapidAPIAutomator");
const {logResultsToJSONFile} = require("./Library/resultsLogger");
const Logger = require("./Library/Loggers/CollectorLogger")
const CollectorEventEmitter = require("./Library/CollectorEventEmitter");
const {Scheduler, EVENT, API_TRIGGER, getNextSchedule} = require("./Library/Scheduler");
const {getJSONFromFile, getJobTypesFromFile} = require("./Library/utils");
const app = async () => {
const schedulerExpression = getNextSchedule()
const keys = await getJSONFromFile(process.env.KEYS_FILENAME);
const keySet = new Set(keys);
const jobList = await getJobTypesFromFile(process.env.JOBTYPES_FILENAME);
const Emitter = new CollectorEventEmitter();
Emitter.on(EVENT, wrapper.bind(null, keySet, jobList));
const scheduler = new Scheduler(Emitter);
scheduler.start(schedulerExpression);
handle_api_trigger(scheduler);
Logger.info("started successfully")
};
const wrapper = async (keySet, jobList) => {
try {
const automator = new RapidAPIAutomator(keySet);
const response = await automator.collect(jobList);
Logger.info("Logging results summary");
await logResultsToJSONFile("summary", new Date(Date.now()), response);
Logger.info(JSON.stringify(response));
} catch (error) {
Logger.info("Something went wrong in wrapper")
Logger.error(error.message)
}
};
async function start() {
try {
Logger.info("Starting...");
await db_connect(process.env.DB_PROD_URL);
await app();
} catch (error) {
Logger.info("Something went wrong in collector start")
Logger.error(error.message)
await db_close();
process.exit(1);
}
}
const handle_api_trigger = (scheduler) => {
process.on("message", (msg) => {
if (msg.to === "COLLECTOR" && msg.code === API_TRIGGER) {
Logger.info("API trigger received.")
scheduler.emit(getNextSchedule())
return process.send({to: "SERVER", code: 200})
}
})
}
start();