Skip to content

Commit

Permalink
feat: add local dev support for Go scheduled functions (#4491)
Browse files Browse the repository at this point in the history
* fix: detection for Go scheduled functions

* fix: incompatible fs import

* chore: fix conventional message error

* fix: parse schedule from config

* fix: unsupported optional chaining

* fix: broken function test without config

* chore: fix prettier warnings

* fix: additional checks in NetlifyFunction

* feat: assert valid next_run in test

* chore: update names and tests
  • Loading branch information
karagulamos committed Mar 29, 2022
1 parent 3df8f95 commit 1693824
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/lib/functions/netlify-function.js
Expand Up @@ -41,7 +41,9 @@ class NetlifyFunction {
// Determines whether this is a background function based on the function
// name.
this.isBackground = name.endsWith(BACKGROUND_SUFFIX)
this.schedule = null

const functionConfig = config.functions && config.functions[name]
this.schedule = functionConfig && functionConfig.schedule

// List of the function's source files. This starts out as an empty set
// and will get populated on every build.
Expand Down Expand Up @@ -91,7 +93,7 @@ class NetlifyFunction {

this.buildData = buildData
this.srcFiles = srcFilesSet
this.schedule = schedule
this.schedule = schedule || this.schedule

return { srcFilesDiff }
} catch (error) {
Expand Down
1 change: 1 addition & 0 deletions src/lib/functions/registry.js
Expand Up @@ -177,6 +177,7 @@ class FunctionsRegistry {
buildGoSource: true,
buildRustSource: env.NETLIFY_EXPERIMENTAL_BUILD_RUST_SOURCE === 'true',
},
config: this.config,
})

// Before registering any functions, we look for any functions that were on
Expand Down
1 change: 1 addition & 0 deletions tests/integration/20.command.functions.test.js
Expand Up @@ -31,6 +31,7 @@ test('should return the correct function url for a NetlifyFunction object', (t)
const ntlFunction = new NetlifyFunction({
name: functionName,
settings: { functionsPort: port },
config: { functions: { [functionName]: {} } },
})

t.is(ntlFunction.url, functionUrl)
Expand Down
74 changes: 74 additions & 0 deletions tests/integration/630.serving-functions-go.test.js
Expand Up @@ -112,3 +112,77 @@ test('Updates a Go function when a file is modified', async (t) => {
}
})
})

// Reproduction test to verify the abscence/presence of a Go scheduled function
test('Detects a Go scheduled function using netlify-toml config', async (t) => {
const [execaMock, removeExecaMock] = await createExecaMock(`
const assert = require('assert')
const handler = (...args) => {
if (args[0].includes('local-functions-proxy')) {
const { body } = JSON.parse(args[1][1])
const { next_run } = JSON.parse(body)
assert.ok(next_run)
const response = {
statusCode: 200
}
return {
stderr: '',
stdout: JSON.stringify(response)
}
}
}
module.exports = (...args) => ({
...handler(...args) || {},
stderr: { pipe: () => {} }
})
`)

await withSiteBuilder('go-scheduled-function', async (builder) => {
try {
await builder
.withNetlifyToml({
config: {
build: { publish: 'public' },
functions: { directory: 'src/', 'go-scheduled-function': { schedule: '@daily' } },
},
})
.withContentFiles([
{
path: 'go.mod',
content: `<mock go.mod>`,
},
{
path: 'go.sum',
content: `<mock go.sum>`,
},
{
path: 'src/go-scheduled-function/main.go',
content: `<mock main.go>`,
},
])
.buildAsync()

await withDevServer(
{
cwd: builder.directory,
env: execaMock,
},
async ({ port }) => {
const response = await got(`http://localhost:${port}/.netlify/functions/go-scheduled-function`)

t.regex(response.body, /You performed an HTTP request/)
t.regex(response.body, /Your function returned `body`/)

t.is(response.statusCode, 200)
},
)
} finally {
await removeExecaMock()
}
})
})

1 comment on commit 1693824

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

Package size: 380 MB

Please sign in to comment.