Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve handling of scheduled v2 functions #6443

Merged
merged 1 commit into from Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/lib/functions/server.ts
Expand Up @@ -201,6 +201,12 @@ export const createHandler = function (options: GetFunctionsServerOptions): Requ

handleBackgroundFunctionResult(functionName, error)
} else if (await func.isScheduled()) {
// In production, scheduled functions always receive POST requests, so we
// have to emulate that here, even if a user has triggered a GET request
// as part of their tests. If we don't do this, we'll hit problems when
// we send the invocation body in a request that can't have a body.
event.httpMethod = 'POST'

const { error, result } = await func.invoke(
{
...event,
Expand All @@ -219,9 +225,18 @@ export const createHandler = function (options: GetFunctionsServerOptions): Requ

handleScheduledFunction({
error,
result,
request,
response,

// When we handle the result of invoking a scheduled function, we'll warn
// people in case their function returned a body or headers, since those
// will have no practical effect in production. However, in v2 functions
// we don't currently have a good way of asserting whether the body we're
// seeing has been actually produced by user code or by the bootstrap, so
// we risk printing that warn unnecessarily, which causes more harm than
// good. Until we find a way of making this detection better, ignore the
// invocation result entirely for v2 functions.
result: func.runtimeAPIVersion === 1 ? result : {},
})
} else {
const { error, result } = await func.invoke(event, clientContext)
Expand Down
@@ -0,0 +1,7 @@
export default async () => {
console.log('Hello!')
}

export const config = {
schedule: '* * * * *',
}
Expand Up @@ -7,7 +7,7 @@ import { pause } from '../../utils/pause.js'
describe('scheduled functions', () => {
setupFixtureTests('dev-server-with-functions', { devServer: true }, () => {
test<FixtureTestContext>('should emulate next_run for scheduled functions', async ({ devServer }) => {
const response = await fetch(`http://localhost:${devServer.port}/.netlify/functions/scheduled-isc`, {})
const response = await fetch(`http://localhost:${devServer.port}/.netlify/functions/scheduled-v2`, {})

expect(response.status).toBe(200)
})
Expand Down