-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
80 lines (65 loc) · 2.39 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
if(!process.env.PROD)
require('dotenv').config()
const redisQueue = require("./helpers/redisQueue")
const {sleep} = require('./helpers/utils')
const AutoSaver = require('./autoSave')
const AutoCull = require('./autoCull')
const saveQueueName = process.env.AUTO_SAVE_QUEUE || "auto-save-queue"
const cullQueueName = process.env.AUTO_CULL_QUEUE || "auto-cull-queue"
async function run(){
console.log("Connecting to redis...")
await redisQueue.connect()
console.log("Done")
// Autosave
if(!process.env.AUTO_SAVE_OFF)
startAutoSave().then()
// Auto-cull
if(!process.env.AUTO_CULL_OFF)
startAutoCull().then()
}
const startAutoSave = async ()=>{
console.log("Started auto-save")
const autoSaver = new AutoSaver()
await autoSaver.generateToken()
while(true){
const task = JSON.parse(`${await redisQueue.pop(saveQueueName)}`)
if(!task) {
await sleep(parseInt(process.env.AUTOSAVE_INTERVAL)*1000)
continue
}
const timeDelta = (new Date() - new Date(task.timestamp))/1000;
if(parseInt(process.env.AUTOSAVE_INTERVAL) > timeDelta)
await sleep((parseInt(process.env.AUTOSAVE_INTERVAL) - timeDelta)*1000)
await autoSaver.save(task.roomId)
if(await redisQueue.get(task.roomId))
await redisQueue.push(saveQueueName, {
roomId: task.roomId,
timestamp: new Date().toISOString()
})
}
}
const startAutoCull = async ()=>{
console.log("Starting auto-cull")
const autoCull = new AutoCull()
await autoCull.connect()
while(true){
const task = JSON.parse(`${await redisQueue.pop(cullQueueName)}`)
if(!task) {
await sleep(parseInt(process.env.AUTOCULL_INTERVAL)*1000)
continue
}
const timeDelta = (new Date() - new Date(task.timestamp))/1000;
if(parseInt(process.env.AUTOCULL_INTERVAL) > timeDelta)
await sleep((parseInt(process.env.AUTOCULL_INTERVAL) - timeDelta)*1000)
const {shouldCull, isRunning} = await autoCull.checkAndCull(task.roomId, task.shouldCull)
if(isRunning)
await redisQueue.push(cullQueueName, {
roomId: task.roomId,
shouldCull,
timestamp: new Date().toISOString()
})
else
await redisQueue.del(task.roomId)
}
}
run().then();