-
-
Notifications
You must be signed in to change notification settings - Fork 794
/
HandlerRunner.js
142 lines (119 loc) · 3.61 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
import debugLog from '../../debugLog.js'
import { logWarning } from '../../serverlessLog.js'
import {
supportedNodejs,
supportedPython,
supportedRuby,
supportedJava,
} 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) {
this.#env = env
this.#funOptions = funOptions
this.#options = options
}
async _loadRunner() {
const {
useDocker,
useChildProcesses,
useWorkerThreads,
allowCache,
} = this.#options
const {
functionKey,
handlerName,
handlerPath,
runtime,
timeout,
} = this.#funOptions
debugLog(`Loading handler... (${handlerPath})`)
if (useDocker) {
const dockerOptions = {
readOnly: this.#options.dockerReadOnly,
layersDir: this.#options.layersDir,
}
const { default: DockerRunner } = await import('./docker-runner/index.js')
return new DockerRunner(this.#funOptions, this.#env, dockerOptions)
}
if (supportedNodejs.has(runtime)) {
if (useChildProcesses) {
const { default: ChildProcessRunner } = await import(
'./child-process-runner/index.js'
)
return new ChildProcessRunner(this.#funOptions, this.#env, allowCache)
}
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 (supportedPython.has(runtime)) {
const { default: PythonRunner } = await import('./python-runner/index.js')
return new PythonRunner(this.#funOptions, this.#env, allowCache)
}
if (supportedRuby.has(runtime)) {
const { default: RubyRunner } = await import('./ruby-runner/index.js')
return new RubyRunner(this.#funOptions, this.#env, allowCache)
}
if (supportedJava.has(runtime)) {
const { default: JavaRunner } = await import('./java-runner/index.js')
return new JavaRunner(this.#funOptions, this.#env, allowCache)
}
// 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) {
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)
}
}