-
Notifications
You must be signed in to change notification settings - Fork 8
/
restart-server.js
61 lines (49 loc) · 1.63 KB
/
restart-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
/*
Restarts the web server if a new hash is published to the BCH blockchain.
*/
'use strict'
const shell = require('shelljs')
const kill = require('tree-kill')
// App specific libraries.
// const config = require('./config')
const BCH = require(`./src/lib/bch`)
const bch = new BCH()
// Edit the period below, which dictates how often this app checks
// the BCH blockchain for updates.
// The time is in milliseconds (ms). 60,000 ms = 1 minute
const PERIOD = 60000 * 5
// Used for debugging and iterrogating JS objects.
const util = require('util')
util.inspect.defaultOptions = { depth: 1 }
// Start the IPFS blog web server. Restart it if a new hash is published to the
// BCH network.
async function manageServer () {
try {
// Start the web server.
const server = shell.exec('node index.js', { async: true })
let pid = server.pid
// console.log(`server : ${util.inspect(server)}`)
// console.log(`pid: ${server.pid}`)
setInterval(async function () {
console.log(`Checking for updates...`)
// Check for updates. Will usually return false.
const hash = await bch.checkForUpdates(pid)
// If a hash is returned, then restart the web server.
if (hash) {
console.log(`New content published with hash ${hash}`)
console.log('Restarting server...')
kill(pid)
await sleep(5000)
const server = shell.exec('node index.js', { async: true })
pid = server.pid
}
}, PERIOD)
} catch (err) {
console.error(err)
}
}
manageServer()
// Promise based sleep function:
function sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}