-
Notifications
You must be signed in to change notification settings - Fork 220
/
sendToWorker.js
164 lines (138 loc) · 4.36 KB
/
sendToWorker.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
const axios = require('axios')
const serializator = require('serializator')
const { setTimeout } = require('timers/promises')
module.exports = (reporter, { originUrl, reportTimeoutMargin, remote = false } = {}) => {
return function sendToWorker (url, data, _options = {}) {
const options = { ..._options }
const customHttpOptions = reporter.dockerManager.getWorkerHttpOptions({ remote })
const newHttpOptions = {
...options.httpOptions,
...customHttpOptions,
headers: Object.assign({}, options.httpOptions?.headers, customHttpOptions?.headers)
}
if (originUrl != null) {
options.originUrl = originUrl
}
options.httpOptions = newHttpOptions
if (options.timeout != null && reportTimeoutMargin != null) {
options.timeout = options.timeout + reportTimeoutMargin
}
return _sendToWorker(url, data, options)
}
}
async function _sendToWorker (url, _data, { executeMain, timeout, originUrl, systemAction, httpOptions = {}, signal }) {
let data = { ..._data, timeout, systemAction }
if (originUrl != null) {
data.originUrl = originUrl
}
let isDone = false
async function run () {
while (true && !isDone) {
if (signal?.aborted) {
const e = new Error('Worker aborted')
e.code = 'WORKER_ABORTED'
throw e
}
const stringBody = serializator.serialize(data)
let res
try {
const requestConfig = {
method: 'POST',
url,
maxContentLength: Infinity,
maxBodyLength: Infinity,
responseType: 'text',
transformResponse: [data => data],
headers: {
'Content-Type': 'text/plain',
'Content-Length': Buffer.byteLength(stringBody),
...httpOptions.headers
},
data: stringBody,
timeout: timeout
}
if (httpOptions.auth) {
requestConfig.auth = httpOptions.auth
}
res = await axios(requestConfig)
} catch (err) {
if (isDone) {
return
}
isDone = true
if (!err.response?.data) {
const error = new Error('Error when communicating with worker: ' + err.message)
error.needRestart = true
throw error
}
let workerError = null
try {
const errorData = JSON.parse(err.response.data)
workerError = new Error(errorData.message)
Object.assign(workerError, errorData)
} catch (e) {
const error = new Error('Error when communicating with worker: ' + err.response.data)
error.needRestart = true
throw error
}
throw workerError
}
if (isDone) {
return
}
if (res.status === 201) {
isDone = true
return serializator.parse(res.data)
}
if (res.status !== 200) {
isDone = true
const e = new Error('Unexpected response from worker: ' + res.data)
e.needRestart = true
throw e
}
let mainDataResponse = {}
try {
mainDataResponse = await executeMain(serializator.parse(res.data))
} catch (err) {
mainDataResponse.error = {
...err,
message: err.message,
stack: err.stack
}
}
data = {
systemAction: 'callback-response',
// we need just the request identification in the worker
req: { context: { rootId: data.req.context.rootId } },
data: mainDataResponse
}
}
}
let timeoutController
if (timeout) {
// eslint-disable-next-line
timeoutController = new AbortController()
}
if (!timeout) {
return run()
}
// we handle the timeout using promises to avoid loosing stack in case of error
// mixing new Promise with async await leads to loosing it. we ensure that when run ends
// we clean the timeout to avoid keeping handlers in memory longer than needed and allow
// the node.js process to not have the timeout process pending
return Promise.race([
run().then((result) => {
timeoutController?.abort()
return result
}, (err) => {
timeoutController?.abort()
throw err
}),
setTimeout(timeout, undefined, { signal: timeoutController.signal }).then(() => {
isDone = true
const e = new Error('Timeout when communicating with worker')
e.needRestart = true
throw e
})
])
}