Skip to content

Commit 1693824

Browse files
authored
feat: add local dev support for Go scheduled functions (#4491)
* 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
1 parent 3df8f95 commit 1693824

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

src/lib/functions/netlify-function.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ class NetlifyFunction {
4141
// Determines whether this is a background function based on the function
4242
// name.
4343
this.isBackground = name.endsWith(BACKGROUND_SUFFIX)
44-
this.schedule = null
44+
45+
const functionConfig = config.functions && config.functions[name]
46+
this.schedule = functionConfig && functionConfig.schedule
4547

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

9294
this.buildData = buildData
9395
this.srcFiles = srcFilesSet
94-
this.schedule = schedule
96+
this.schedule = schedule || this.schedule
9597

9698
return { srcFilesDiff }
9799
} catch (error) {

src/lib/functions/registry.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ class FunctionsRegistry {
177177
buildGoSource: true,
178178
buildRustSource: env.NETLIFY_EXPERIMENTAL_BUILD_RUST_SOURCE === 'true',
179179
},
180+
config: this.config,
180181
})
181182

182183
// Before registering any functions, we look for any functions that were on

tests/integration/20.command.functions.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ test('should return the correct function url for a NetlifyFunction object', (t)
3131
const ntlFunction = new NetlifyFunction({
3232
name: functionName,
3333
settings: { functionsPort: port },
34+
config: { functions: { [functionName]: {} } },
3435
})
3536

3637
t.is(ntlFunction.url, functionUrl)

tests/integration/630.serving-functions-go.test.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,77 @@ test('Updates a Go function when a file is modified', async (t) => {
112112
}
113113
})
114114
})
115+
116+
// Reproduction test to verify the abscence/presence of a Go scheduled function
117+
test('Detects a Go scheduled function using netlify-toml config', async (t) => {
118+
const [execaMock, removeExecaMock] = await createExecaMock(`
119+
const assert = require('assert')
120+
121+
const handler = (...args) => {
122+
if (args[0].includes('local-functions-proxy')) {
123+
const { body } = JSON.parse(args[1][1])
124+
const { next_run } = JSON.parse(body)
125+
126+
assert.ok(next_run)
127+
128+
const response = {
129+
statusCode: 200
130+
}
131+
132+
return {
133+
stderr: '',
134+
stdout: JSON.stringify(response)
135+
}
136+
}
137+
}
138+
139+
module.exports = (...args) => ({
140+
...handler(...args) || {},
141+
stderr: { pipe: () => {} }
142+
})
143+
`)
144+
145+
await withSiteBuilder('go-scheduled-function', async (builder) => {
146+
try {
147+
await builder
148+
.withNetlifyToml({
149+
config: {
150+
build: { publish: 'public' },
151+
functions: { directory: 'src/', 'go-scheduled-function': { schedule: '@daily' } },
152+
},
153+
})
154+
.withContentFiles([
155+
{
156+
path: 'go.mod',
157+
content: `<mock go.mod>`,
158+
},
159+
{
160+
path: 'go.sum',
161+
content: `<mock go.sum>`,
162+
},
163+
{
164+
path: 'src/go-scheduled-function/main.go',
165+
content: `<mock main.go>`,
166+
},
167+
])
168+
.buildAsync()
169+
170+
await withDevServer(
171+
{
172+
cwd: builder.directory,
173+
env: execaMock,
174+
},
175+
async ({ port }) => {
176+
const response = await got(`http://localhost:${port}/.netlify/functions/go-scheduled-function`)
177+
178+
t.regex(response.body, /You performed an HTTP request/)
179+
t.regex(response.body, /Your function returned `body`/)
180+
181+
t.is(response.statusCode, 200)
182+
},
183+
)
184+
} finally {
185+
await removeExecaMock()
186+
}
187+
})
188+
})

0 commit comments

Comments
 (0)