-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
64 lines (53 loc) · 1.37 KB
/
app.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
const express = require("express");
const app = express();
const PORT = process.env.PORT || 3000;
const Listener = require("./src/Listener");
const Sync = require("./src/Sync");
const emitter = require('events');
// Routes
app.get("/", (req, res) => {
res.send("YourEvent(s) Hook")
});
// Listen
app.listen(PORT, async (error) => {
if (!error) {
console.log(`Server is Successfully Running, and App is listening on port ${PORT}`)
await main()
} else {
console.log("Error occurred, server can't start", error);
}
});
async function main() {
await workflow();
emitter.on('sync-past-events', async () => {
await workflow();
});
emitter.emit('sync-past-events');
setInterval(() => {
emitter.emit('sync-past-events');
}, Number(process.env.APP_SYNC_INTERVAL_MINUTES) * 60 * 1000);
}
// -- Wrapped Workflow -- //
let listeners = []
async function workflow() {
await unsubscribeListeners(listeners);
setEventListeners();
await syncPastEvents();
}
function setEventListeners() {
console.log('Setting Event Listeners')
listeners.push(Listener.eventListenerYourEvent())
}
async function syncPastEvents() {
console.log('Running Sync Past Event');
try {
await Sync.syncPastEvents()
} catch (error) {
console.error(error);
}
}
async function unsubscribeListeners(listeners) {
for (const l of listeners) {
await l.unsubscribe();
}
}