-
-
Notifications
You must be signed in to change notification settings - Fork 793
/
HandlerRunner.js
208 lines (182 loc) · 5.53 KB
/
HandlerRunner.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
197
198
199
200
201
202
203
204
205
206
207
208
import debugLog from '../../debugLog.js'
import { logWarning } from '../../serverlessLog.js'
import {
supportedNodejs,
supportedPython,
supportedRuby,
supportedJava,
supportedGo,
} from '../../config/index.js'
import { satisfiesVersionRange } from '../../utils/index.js'
export default class HandlerRunner {
#env = null
#funOptions = null
#options = null
#runner = null
constructor(funOptions, options, env, v3Utils) {
this.#env = env
this.#funOptions = funOptions
this.#options = options
if (v3Utils) {
this.log = v3Utils.log
this.progress = v3Utils.progress
this.writeText = v3Utils.writeText
this.v3Utils = v3Utils
}
}
async _loadRunner() {
const { useDocker, useChildProcesses, useWorkerThreads, allowCache } =
this.#options
const { functionKey, handlerName, handlerPath, runtime, timeout } =
this.#funOptions
if (this.log) {
this.log.debug(`Loading handler... (${handlerPath})`)
} else {
debugLog(`Loading handler... (${handlerPath})`)
}
if (useDocker) {
// https://github.com/lambci/docker-lambda/issues/329
if (runtime === 'nodejs14.x') {
if (this.log) {
this.log.warning(
'"nodejs14.x" runtime is not supported with docker. See https://github.com/lambci/docker-lambda/issues/329',
)
} else {
logWarning(
'"nodejs14.x" runtime is not supported with docker. See https://github.com/lambci/docker-lambda/issues/329',
)
}
throw new Error('Unsupported runtime')
}
if (runtime === 'python3.9') {
if (this.log) {
this.log.warning('"python3.9" runtime is not supported with docker.')
} else {
logWarning('"python3.9" runtime is not supported with docker.')
}
throw new Error('Unsupported runtime')
}
const dockerOptions = {
host: this.#options.dockerHost,
hostServicePath: this.#options.dockerHostServicePath,
layersDir: this.#options.layersDir,
network: this.#options.dockerNetwork,
readOnly: this.#options.dockerReadOnly,
}
const { default: DockerRunner } = await import('./docker-runner/index.js')
return new DockerRunner(
this.#funOptions,
this.#env,
dockerOptions,
this.v3Utils,
)
}
if (supportedNodejs.has(runtime)) {
if (useChildProcesses) {
const { default: ChildProcessRunner } = await import(
'./child-process-runner/index.js'
)
return new ChildProcessRunner(
this.#funOptions,
this.#env,
allowCache,
this.v3Utils,
)
}
if (useWorkerThreads) {
// worker threads
this._verifyWorkerThreadCompatibility()
const { default: WorkerThreadRunner } = await import(
'./worker-thread-runner/index.js'
)
return new WorkerThreadRunner(this.#funOptions, this.#env, allowCache)
}
const { default: InProcessRunner } = await import(
'./in-process-runner/index.js'
)
return new InProcessRunner(
functionKey,
handlerPath,
handlerName,
this.#env,
timeout,
allowCache,
)
}
if (supportedGo.has(runtime)) {
const { default: GoRunner } = await import('./go-runner/index.js')
return new GoRunner(this.#funOptions, this.#env, this.v3Utils)
}
if (supportedPython.has(runtime)) {
const { default: PythonRunner } = await import('./python-runner/index.js')
return new PythonRunner(
this.#funOptions,
this.#env,
allowCache,
this.v3Utils,
)
}
if (supportedRuby.has(runtime)) {
const { default: RubyRunner } = await import('./ruby-runner/index.js')
return new RubyRunner(
this.#funOptions,
this.#env,
allowCache,
this.v3Utils,
)
}
if (supportedJava.has(runtime)) {
const { default: JavaRunner } = await import('./java-runner/index.js')
return new JavaRunner(
this.#funOptions,
this.#env,
allowCache,
this.v3Utils,
)
}
// TODO FIXME
throw new Error('Unsupported runtime')
}
_verifyWorkerThreadCompatibility() {
const { node: currentVersion } = process.versions
const requiredVersionRange = '>=11.7.0'
const versionIsSatisfied = satisfiesVersionRange(
currentVersion,
requiredVersionRange,
)
// we're happy
if (!versionIsSatisfied) {
if (this.log) {
this.log.warning(
`"worker threads" require node.js version ${requiredVersionRange}, but found version ${currentVersion}.
To use this feature you have to update node.js to a later version.
`,
)
} else {
logWarning(
`"worker threads" require node.js version ${requiredVersionRange}, but found version ${currentVersion}.
To use this feature you have to update node.js to a later version.
`,
)
}
throw new Error(
'"worker threads" are not supported with this node.js version',
)
}
}
// TEMP TODO FIXME
isDockerRunner() {
return this.#runner && this.#runner.constructor.name === 'DockerRunner'
}
// () => Promise<void>
cleanup() {
// TODO console.log('handler runner cleanup')
return this.#runner.cleanup()
}
async run(event, context) {
if (this.#runner == null) {
this.#runner = await this._loadRunner()
}
return this.#runner.run(event, context)
}
}