Serverless function timeout concerns (#9689)

closes https://github.com/twentyhq/core-team-issues/issues/242
- unify timeout behavior between local and lambda
- add timeout in serverless entity
- set timeout default to 300s (5min)
This commit is contained in:
martmull
2025-01-17 14:49:02 +01:00
committed by GitHub
parent 679429447d
commit f8f9bb2b78
14 changed files with 100 additions and 15 deletions

View File

@ -237,7 +237,7 @@ export class LambdaDriver implements ServerlessDriver {
Role: this.lambdaRole,
Runtime: serverlessFunction.runtime,
Description: 'Lambda function to run user script',
Timeout: 900,
Timeout: serverlessFunction.timeoutSeconds,
};
const command = new CreateFunctionCommand(params);
@ -259,6 +259,7 @@ export class LambdaDriver implements ServerlessDriver {
Variables: envVariables,
},
FunctionName: serverlessFunction.id,
Timeout: serverlessFunction.timeoutSeconds,
};
const updateConfigurationCommand = new UpdateFunctionConfigurationCommand(

View File

@ -183,7 +183,17 @@ export class LocalDriver implements ServerlessDriver {
return await new Promise((resolve, reject) => {
const child = fork(listenerFile, { silent: true });
const timeoutMs = serverlessFunction.timeoutSeconds * 1_000;
const timeoutHandler = setTimeout(() => {
child.kill();
const duration = Date.now() - startTime;
reject(new Error(`Task timed out after ${duration / 1_000} seconds`));
}, timeoutMs);
child.on('message', (message: object | ServerlessExecuteError) => {
clearTimeout(timeoutHandler);
const duration = Date.now() - startTime;
if ('errorType' in message) {
@ -204,6 +214,7 @@ export class LocalDriver implements ServerlessDriver {
});
child.stderr?.on('data', (data) => {
clearTimeout(timeoutHandler);
const stackTrace = data
.toString()
.split('\n')
@ -235,11 +246,13 @@ export class LocalDriver implements ServerlessDriver {
});
child.on('error', (error) => {
clearTimeout(timeoutHandler);
reject(error);
child.kill();
});
child.on('exit', (code) => {
clearTimeout(timeoutHandler);
if (code && code !== 0) {
reject(new Error(`Child process exited with code ${code}`));
}