Skip to content

Commit

Permalink
feat: add reload handler flag
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk committed Jul 16, 2022
1 parent acac5bb commit f662dc5
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ All CLI options are optional:
--noTimeout -t Disables the timeout feature.
--prefix -p Adds a prefix to every path, to send your requests to http://localhost:3000/[prefix]/[your_path] instead. Default: ''
--printOutput Turns on logging of your lambda outputs in the terminal.
--reloadHandler Reloads handler with each request.
--resourceRoutes Turns on loading of your HTTP proxy settings from serverless.yml
--useChildProcesses Run handlers in a child process
--useDocker Run handlers in a docker container.
Expand Down
4 changes: 4 additions & 0 deletions src/config/commandOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ export default {
type: 'boolean',
usage: 'Outputs your lambda response to the terminal.',
},
reloadHandler: {
type: 'boolean',
usage: 'Reloads handler with each request.',
},
resourceRoutes: {
type: 'boolean',
usage: 'Turns on loading of your HTTP proxy settings from serverless.yml.',
Expand Down
1 change: 1 addition & 0 deletions src/config/defaultOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default {
noTimeout: false,
prefix: '',
printOutput: false,
reloadHandler: false,
resourceRoutes: false,
useChildProcesses: false,
useDocker: false,
Expand Down
14 changes: 8 additions & 6 deletions src/lambda/LambdaFunctionPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,15 @@ export default class LambdaFunctionPool {
return lambdaFunction
}

// find any IDLE
lambdaFunction = Array.from(lambdaFunctions).find(
({ status }) => status === 'IDLE',
)
if (!this.#options.reloadHandler) {
// find any IDLE
lambdaFunction = Array.from(lambdaFunctions).find(
({ status }) => status === 'IDLE',
)

if (lambdaFunction != null) {
return lambdaFunction
if (lambdaFunction != null) {
return lambdaFunction
}
}

// we don't have any IDLE instances
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import { promisify } from 'node:util'
import {
joinUrl,
setup,
teardown,
} from '../../../integration/_testHelpers/index.js'

const setTimeoutPromise = promisify(setTimeout)

const __dirname = dirname(fileURLToPath(import.meta.url))

describe.only('run mode with worker threads', function desc() {
beforeEach(() =>
setup({
servicePath: resolve(__dirname),
}),
)

afterEach(() => teardown())

it('should always create a new lambda instance', async () => {
const url = joinUrl(env.TEST_BASE_URL, 'dev/foo')

const results = []

// eslint-disable-next-line no-unused-vars
for (const _ of Array(100)) {
// eslint-disable-next-line no-await-in-loop
await setTimeoutPromise(10)
results.push(fetch(url))
}

const responses = await Promise.all(results)

responses.forEach((response) => {
assert.equal(response.status, 200)
})

const jsons = await Promise.all(
responses.map((response) => response.json()),
)

jsons.forEach((json) => {
assert.deepEqual(json, 1)
})
})
})
26 changes: 26 additions & 0 deletions tests/lambda-run-mode/worker-threads/reload-handler/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
service: run-mode-tests

configValidationMode: error

plugins:
- ../../../../

custom:
serverless-offline:
reloadHandler: true

provider:
memorySize: 128
name: aws
region: us-east-1 # default
runtime: nodejs16.x
stage: dev
versionFunctions: false

functions:
foo:
events:
- http:
method: get
path: foo
handler: src/handler.foo
14 changes: 14 additions & 0 deletions tests/lambda-run-mode/worker-threads/reload-handler/src/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

const { stringify } = JSON

let counter = 0

exports.foo = async function foo() {
counter += 1

return {
body: stringify(counter),
statusCode: 200,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}

0 comments on commit f662dc5

Please sign in to comment.