-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
161 lines (148 loc) · 3.44 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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const axios = require('axios')
const debug = require('@ff0000-ad-tech/debug')
var log = debug('PM')
// options
let options = {}
// if creative server is not reachable
let csAvailable = true
// prepare
const prepare = (_options) => {
options = Object.assign(
{
api: null, // path to creative-server api
key: null, // `/${ctype}/${size}/${index}`,
watch: {
start: null, // `/watch-start`,
stop: null, // `/watch-stop`,
complete: null // `/watch-complete`,
},
processing: {
start: null, // `/processing-start`,
stop: null // `/processing-stop`
},
error: null // `/error`
},
_options
)
}
const getCmd = (name) => {
try {
switch (name) {
case 'api':
return options.api
case 'watch-start':
return options.api + options.watch.start + options.key
case 'watch-stop':
return options.api + options.watch.stop + options.key
case 'watch-complete':
return options.api + options.watch.complete + options.key
case 'processing-start':
return options.api + options.processing.start + options.key
case 'processing-stop':
return options.api + options.processing.stop + options.key
case 'error-dispatch':
return options.api + options.error.dispatch + options.key
case 'error-reset':
return options.api + options.error.reset + options.key
}
} catch (err) {
return
}
}
// prepare interrupt
const prepareInterrupt = () => {
const cleanup = async () => {
const processExit = () => {
log('Goodbye~')
process.exit()
}
await stopWatching(processExit, processExit)
}
process.on('SIGINT', cleanup)
process.on('SIGUSR1', cleanup)
process.on('SIGUSR2', cleanup)
process.on('SIGTERM', cleanup)
// process.on('SIGKILL', cleanup)
process.on('exit', (code) => {
log(`Exit code: ${code}`)
})
process.on('uncaughtException', (err) => {
log(err)
cleanup()
})
}
// comm
const executeReq = async (req, { desc, cb, errCb } = {}) => {
// give up making reqs to creative-server if previously unreachable
if (!csAvailable) {
errCb && errCb()
return
}
desc && log(desc)
log(req)
try {
await axios({ url: req, method: 'get', timeout: 2000 })
cb && cb()
} catch (err) {
log(`Unable to connect to Creative-Server: ${err.message}`)
csAvailable = false
errCb && errCb()
}
}
// start watching
const startWatching = async () => {
const cmd = getCmd('watch-start')
if (cmd) {
prepareInterrupt()
await executeReq(`${cmd}/${process.pid}`, { desc: 'Requesting Creative-Server to watch' })
}
}
// stop watching
const stopWatching = async (cb, errCb) => {
const cmd = getCmd('watch-stop')
if (cmd) {
await executeReq(`${cmd}/${process.pid}`, {
desc: 'Requesting Creative-Server to stop watching',
cb: () => {
process.stdin.destroy() // release the process to terminate on its own
cb && cb()
},
errCb
})
}
}
// complete watching
const completeWatch = async () => {
const cmd = getCmd('watch-complete')
if (cmd) {
await executeReq(cmd, { desc: 'Inform Creative-Server process is complete' })
}
}
// processing
const setProcessing = async (toggle) => {
let cmd = getCmd('processing-start')
if (!toggle) {
cmd = getCmd('processing-stop')
}
if (cmd) {
await executeReq(cmd)
}
}
// erroring
const setError = async (toggle) => {
let cmd = getCmd('error-dispatch')
if (!toggle) {
cmd = getCmd('error-reset')
}
if (cmd) {
await executeReq(cmd)
}
}
module.exports = {
prepare,
startWatching,
stopWatching,
completeWatch,
setProcessing,
setError
}