forked from lamassu/lamassu-machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watchdog.js
93 lines (79 loc) · 2.04 KB
/
watchdog.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
81
82
83
84
85
86
87
88
89
90
91
92
93
// This just looks for a script file, checks it's signature, and runs it
'use strict'
var fs = require('fs')
var cp = require('child_process')
require('./lite-logging')
var BASE = '/tmp/extract'
var DONE_PATH = BASE + '/done.txt'
var SCRIPT_PATH = BASE + '/package/updatescript.js'
var RUNNING_PATH = BASE + '/running.txt'
var TIMEOUT = 600000
var child = null
var t0 = null
var running = false
var platform = process.argv[2] || 'N7G1'
var model = process.argv[3] || (platform === 'upboard' ? 'gaia' : null)
process.on('SIGUSR2', function () {
// USR1 is reserved by node
console.log('Got SIGUSR2. Immune.')
})
process.on('uncaughtException', console.log)
process.on('unhandledRejection', console.log)
process.on('exit', () => console.log('lamassu-watchdog exiting'))
setInterval(watch, 1000)
setInterval(function () {
if (running) return
process.exit(0)
}, 600000)
function watch () {
if (running) {
var interval = Date.now() - t0
if (interval > TIMEOUT) {
kill()
}
return
}
var exists = fs.existsSync(DONE_PATH)
if (exists) executeScript()
}
function kill () {
console.log('killing child on timeout')
if (child) child.kill('SIGINT')
}
function executeScript () {
// TODO: check sig, and make sure we only run this once to completion
if (running) return
var exists = fs.existsSync(SCRIPT_PATH)
if (!exists) {
console.error('Script file not present: %s', SCRIPT_PATH)
return
}
var success = start()
if (!success) {
console.log("Can't run, there's a new update")
return
}
console.log('in execute')
child = cp.fork(SCRIPT_PATH, [platform, model])
child.on('error', function (err) {
cleanUp()
console.log(err)
})
child.on('exit', function () {
cleanUp()
console.log('done')
})
}
function start () {
t0 = Date.now()
running = true
fs.unlinkSync(DONE_PATH)
fs.writeFileSync(RUNNING_PATH, 'RUNNING\n')
return fs.existsSync(RUNNING_PATH) // check for race conditions
}
function cleanUp () {
t0 = null
child = null
fs.unlinkSync(RUNNING_PATH)
running = false
}