Skip to content

Commit

Permalink
refactor: move splitHandlerPathAndName call to runners
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk committed Aug 11, 2022
1 parent de92b9e commit 690325b
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 23 deletions.
5 changes: 1 addition & 4 deletions src/lambda/LambdaFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
DEFAULT_LAMBDA_TIMEOUT,
supportedRuntimes,
} from '../config/index.js'
import { createUniqueId, splitHandlerPathAndName } from '../utils/index.js'
import { createUniqueId } from '../utils/index.js'

const { ceil } = Math
const { entries, fromEntries } = Object
Expand Down Expand Up @@ -70,7 +70,6 @@ export default class LambdaFunction {
const servicepath = resolve(servicePath, options.location || '')

const { handler, name, package: functionPackage = {} } = functionDefinition
const [handlerPath, handlerName] = splitHandlerPathAndName(handler)

const memorySize =
functionDefinition.memorySize ||
Expand Down Expand Up @@ -140,8 +139,6 @@ export default class LambdaFunction {
? resolve(servicepath, functionPackage.artifact)
: undefined,
handler,
handlerName,
handlerPath: resolve(this.#codeDir, handlerPath),
layers: functionDefinition.layers || [],
provider,
runtime,
Expand Down
3 changes: 2 additions & 1 deletion src/lambda/handler-runner/HandlerRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class HandlerRunner {
async #loadRunner() {
const { useChildProcesses, useDocker, useInProcess } = this.#options

const { functionKey, handler, runtime, servicePath, timeout } =
const { codeDir, functionKey, handler, runtime, servicePath, timeout } =
this.#funOptions

log.debug(`Loading handler... (${handler})`)
Expand Down Expand Up @@ -77,6 +77,7 @@ export default class HandlerRunner {
timeout,
handler,
servicePath,
codeDir,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const __dirname = dirname(fileURLToPath(import.meta.url))
const childProcessHelperPath = resolve(__dirname, 'childProcessHelper.js')

export default class ChildProcessRunner {
#codeDir = null

#env = null

#functionKey = null
Expand All @@ -18,8 +20,9 @@ export default class ChildProcessRunner {
#timeout = null

constructor(funOptions, env) {
const { functionKey, handler, servicePath, timeout } = funOptions
const { codeDir, functionKey, handler, servicePath, timeout } = funOptions

this.#codeDir = codeDir
this.#env = env
this.#functionKey = functionKey
this.#handler = handler
Expand All @@ -34,7 +37,7 @@ export default class ChildProcessRunner {
async run(event, context) {
const childProcess = execaNode(
childProcessHelperPath,
[this.#functionKey, this.#handler, this.#servicePath],
[this.#functionKey, this.#handler, this.#servicePath, this.#codeDir],
{
env: this.#env,
stdio: 'inherit',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ process.on('uncaughtException', (err) => {
})
})

const [, , functionKey, handler, servicePath] = argv
const [, , functionKey, handler, servicePath, codeDir] = argv

process.on('message', async (messageData) => {
const { context, event, timeout } = messageData
Expand All @@ -33,6 +33,7 @@ process.on('message', async (messageData) => {
timeout,
handler,
servicePath,
codeDir,
)

const result = await inProcessRunner.run(event, context)
Expand Down
4 changes: 3 additions & 1 deletion src/lambda/handler-runner/go-runner/GoRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import process, { chdir, cwd } from 'node:process'
import { parse as pathParse, resolve, sep } from 'node:path'
import { log } from '@serverless/utils/log.js'
import { execa } from 'execa'
import { splitHandlerPathAndName } from '../../../utils/index.js'

const { parse, stringify } = JSON

Expand All @@ -23,7 +24,8 @@ export default class GoRunner {
#tmpPath = null

constructor(funOptions, env) {
const { handlerPath, codeDir } = funOptions
const { handler, codeDir } = funOptions
const [handlerPath] = splitHandlerPathAndName(handler)

this.#codeDir = codeDir
this.#env = env
Expand Down
11 changes: 9 additions & 2 deletions src/lambda/handler-runner/in-process-runner/InProcessRunner.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { join } from 'node:path'
import { performance } from 'node:perf_hooks'
import process from 'node:process'
import { load } from './aws-lambda-ric/UserFunction.js'
Expand All @@ -6,6 +7,8 @@ const { floor } = Math
const { assign } = Object

export default class InProcessRunner {
#codeDir = null

#env = null

#functionKey = null
Expand All @@ -16,7 +19,8 @@ export default class InProcessRunner {

#timeout = null

constructor(functionKey, env, timeout, handler, servicePath) {
constructor(functionKey, env, timeout, handler, servicePath, codeDir) {
this.#codeDir = codeDir
this.#env = env
this.#functionKey = functionKey
this.#handler = handler
Expand All @@ -35,7 +39,10 @@ export default class InProcessRunner {
// e.g. process.env.foo = 1 should be coerced to '1' (string)
assign(process.env, this.#env)

const handler = await load(this.#servicePath, this.#handler)
const handler = await load(
this.#servicePath,
join(this.#codeDir, this.#handler),
)

let callback

Expand Down
14 changes: 5 additions & 9 deletions src/lambda/handler-runner/python-runner/PythonRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import process, { cwd } from 'node:process'
import readline from 'node:readline'
import { fileURLToPath } from 'node:url'
import { log } from '@serverless/utils/log.js'
import { splitHandlerPathAndName } from '../../../utils/index.js'

const { parse, stringify } = JSON
const { assign, hasOwn } = Object
Expand All @@ -16,18 +17,13 @@ export default class PythonRunner {

#env = null

#handlerName = null

#handlerPath = null

#runtime = null

constructor(funOptions, env) {
const { handlerName, handlerPath, runtime } = funOptions
const { handler, runtime } = funOptions
const [handlerPath, handlerName] = splitHandlerPathAndName(handler)

this.#env = env
this.#handlerName = handlerName
this.#handlerPath = handlerPath
this.#runtime = platform() === 'win32' ? 'python.exe' : runtime

if (process.env.VIRTUAL_ENV) {
Expand All @@ -47,8 +43,8 @@ export default class PythonRunner {
[
'-u',
resolve(__dirname, 'invoke.py'),
relative(cwd(), this.#handlerPath),
this.#handlerName,
relative(cwd(), handlerPath),
handlerName,
],
{
env: assign(process.env, this.#env),
Expand Down
5 changes: 4 additions & 1 deletion src/lambda/handler-runner/ruby-runner/RubyRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { cwd } from 'node:process'
import { fileURLToPath } from 'node:url'
import { log } from '@serverless/utils/log.js'
import { execa } from 'execa'
import { splitHandlerPathAndName } from '../../../utils/index.js'

const { parse, stringify } = JSON
const { hasOwn } = Object
Expand All @@ -20,7 +21,9 @@ export default class RubyRunner {
#handlerPath = null

constructor(funOptions, env) {
const { handlerName, handlerPath } = funOptions
const [handlerPath, handlerName] = splitHandlerPathAndName(
funOptions.handler,
)

this.#env = env
this.#handlerName = handlerName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ export default class WorkerThreadRunner {
constructor(funOptions /* options */, env) {
// this._options = options

const { functionKey, handler, servicePath, timeout } = funOptions
const { codeDir, functionKey, handler, servicePath, timeout } = funOptions

this.#workerThread = new Worker(workerThreadHelperPath, {
// don't pass process.env from the main process!
env,
workerData: {
codeDir,
functionKey,
handler,
servicePath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { env } from 'node:process'
import { parentPort, workerData } from 'node:worker_threads'
import InProcessRunner from '../in-process-runner/index.js'

const { functionKey, handler, servicePath, timeout } = workerData
const { functionKey, handler, servicePath, timeout, codeDir } = workerData

parentPort.on('message', async (messageData) => {
const { context, event, port } = messageData
Expand All @@ -14,6 +14,7 @@ parentPort.on('message', async (messageData) => {
timeout,
handler,
servicePath,
codeDir,
)

let result
Expand Down

0 comments on commit 690325b

Please sign in to comment.