forked from DefiLlama/sushi-analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (45 loc) · 1.36 KB
/
index.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
const http = require('http')
const PORT = process.env.PORT || 5000
const HOUR = 3600 * 1e3
const fs = require('fs')
const { execSync, spawn } = require('child_process')
const dataFile = 'data.json'
const updateLog = 'updateLog.json'
const server = http.createServer(async (req, res) => {
//response headers
res.writeHead(200, { 'Content-Type': 'application/json' });
//set the response
res.write(fs.readFileSync(dataFile))
//end the response
res.end();
})
server.listen(PORT, () => {
console.log(`server started on port: ${PORT}`);
})
let i = 1
updateData()
setInterval(updateData, HOUR)
function clearData() {
console.log('clearing all data')
fs.writeFileSync(dataFile, JSON.stringify({}))
}
async function updateData() {
if (i % 16 === 0) clearData()
i++
execSync('bash post-install.sh')
console.log(new Date(), '[pre-update all]', i)
await new Promise((resolve, reject) => {
const updateProcess = spawn('node', ['--max-old-space-size=1800', 'updateData.js']);
updateProcess.stdout.pipe(process.stdout)
updateProcess.stderr.pipe(process.stderr)
updateProcess.on('error', (error) => {
console.log(`error: ${error.message}`);
reject(error)
});
updateProcess.on('close', code => {
console.log(`child process exited with code ${code}`)
resolve()
})
})
console.log(new Date(), '[post-update all]', i)
}