-
Notifications
You must be signed in to change notification settings - Fork 1
/
StepExecutor.js
196 lines (178 loc) · 6.08 KB
/
StepExecutor.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const path = require('path')
const fork = require('child_process').fork
const program = path.resolve(path.join(__dirname, 'runtime.js'))
const chokidar = require('chokidar')
const async = require('neo-async')
const errio = require('errio')
const bunyan = require('bunyan')
const osLocale = require('os-locale')
const LogStream = require('../../../utils/logstream')
const childProcessOptions = {stdio: [process.stdin, process.stdout, process.stderr, 'ipc'], detached: true, windowsHide: true}
class StepExecutor {
constructor (log, appSettings) {
this.log = log
this.openCalls = {}
this.openTimeouts = {}
this.calls = 0
this.childProcess = null
this.stepTimeout = 8000
this.appSettings = appSettings
this.watcherOptions = {
ignored: [
'node_modules/**',
'**/.*/**',
'extensions/**/pipelines/**'
],
ignoreInitial: true
}
const streams = []
if (process.env.INTEGRATION_TEST === 'true') {
streams.push({level: process.env.LOG_LEVEL || 'debug', stream: process.stdout})
} else {
streams.push({stream: new LogStream(osLocale.sync().replace('_', '-')), type: 'raw', level: 'debug'})
}
// Init special step logger with its own log level
this.stepLogger = bunyan.createLogger({
name: '\u0008',
streams
})
this.stepLogger.plain = console.log
}
start () {
return new Promise((resolve, reject) => {
if (this.childProcess) return reject(new Error('childProcess already running'))
this.childProcess = fork(program, [], childProcessOptions)
this.childProcess.on('error', err => this.log.error(err, 'Step Executor Error'))
this.childProcess.on('exit', (code, signal) => this.onExit(code, signal))
this.childProcess.on('disconnect', () => this.log.debug('child process IPC disconnected'))
this.childProcess.on('message', msg => {
if (msg.ready) {
this.log.info('Runtime started')
return resolve()
}
this.onMessage(msg)
})
this.childProcess.send({ready: true})
})
}
startWatcher () {
return new Promise((resolve, reject) => {
const extensionDirs = path.join(this.appSettings.getApplicationFolder(), 'extensions', '**')
const extensionSteps = path.join(extensionDirs, 'extension', '*.js')
const extensionStepsInSubfolders = path.join(extensionDirs, 'extension', '**', '*.js')
this.watcher = chokidar.watch([extensionSteps, extensionStepsInSubfolders], this.watcherOptions)
let queued = false
this.watcher.on('all', () => {
if (queued) return
queued = true
setTimeout(() => {
this.log.info('Extension file was changed, restarting')
this.stop()
.then(() => {
return this.start()
})
.then(() => {
queued = false
})
.catch((err) => {
queued = false
return this.log.warn(err, 'Watcher could not restart childProcess')
})
}, 500)
})
this.watcher.on('ready', () => resolve())
})
}
stopWatcher () {
return new Promise((resolve, reject) => {
if (!this.watcher) return resolve()
this.watcher.removeAllListeners()
this.watcher.close()
async.retry({times: 5, interval: 10}, (acb) => {
if (this.watcher.closed) return acb()
acb(new Error('Not disconnected'))
}, (err) => {
if (err) return reject(err)
resolve()
})
})
}
onExit (code, signal) {
const stop = this.childProcess.stop
this.childProcess = null
if (code === null) return this.log.info('Runtime stopped')
this.log.error({code}, 'Runtime crashed' + (!stop ? ', restarting' : ''))
if (stop) return
this.start((err) => {
if (err) this.log.error(err)
})
}
stop () {
return new Promise((resolve) => {
if (!this.childProcess) return resolve()
this.log.debug('Shutting down Step Executor')
this.childProcess.on('exit', () => resolve())
this.childProcess.disconnect()
this.childProcess.kill('SIGINT')
})
}
/**
* @param {object} msg
* @param {string} msg.type
* @param {Array} msg.arguments
* @param {string} [msg.level]
* @param {number} [msg.callId]
* @param {object} [msg.output]
* @param {object} [msg.err]
*/
onMessage (msg) {
switch (msg.type) {
case 'log':
const level = msg.level || 'debug'
msg.arguments.unshift(`[${this.latestStepMeta.path}]:`)
this.stepLogger[level](...msg.arguments)
break
case 'systemLog':
const systemLogLevel = msg.level || 'debug'
this.stepLogger[systemLogLevel](...msg.arguments)
break
case 'output':
if (!this.openCalls[msg.callId]) {
this.log.warn(msg, 'Got message from step runtime but no callback there, maybe timeouted or double used')
break
}
clearTimeout(this.openTimeouts[msg.callId])
delete this.openTimeouts[msg.callId]
this.openCalls[msg.callId](msg.err, msg.output)
delete this.openCalls[msg.callId]
break
default:
this.log.warn(msg, 'Unknown message from step runtime')
}
}
/**
* @param {Object} input
* @param {Object} stepMeta
* @param {String} stepMeta.id
* @param {String} stepMeta.path
* @param {Error|null} stepMeta.catchableError
* @param {Boolean} stepMeta.isErrorCatching
* @param {Object} stepMeta.meta
* @param cb
*/
execute (input, stepMeta, cb) {
if (!this.childProcess || !this.childProcess.connected) return cb(new Error('Process not running, please restart or try again in a few seconds'))
const callId = this.calls++
this.openCalls[callId] = cb
this.openTimeouts[callId] = setTimeout(() => {
delete this.openCalls[callId]
delete this.openTimeouts[callId]
const err = new Error(`Step '${stepMeta.path}' timeout`)
err.code = 'ETIMEOUT'
cb(errio.toObject(err))
}, this.stepTimeout)
this.latestStepMeta = stepMeta
this.childProcess.send({input, stepMeta, callId})
}
}
module.exports = StepExecutor