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

6181 workflows create a custom code executor #6235

Merged
merged 49 commits into from
Jul 17, 2024

Conversation

martmull
Copy link
Contributor

@martmull martmull commented Jul 12, 2024

Closes #6181

Testing

export const handler = async (event: object, context: object) => {
  return { test: 'toto', data: event['data'] };
}
  • play those requests in Altair:
    mutation UpsertFunction($file: Upload!) {
    upsertFunction(name: "toto", file: $file)
    }

mutation ExecFunction {
executeFunction(name:"toto", payload: {data: "titi"})
}

  • it will run the local driver, add those env variable to test with lambda driver
CUSTOM_CODE_ENGINE_DRIVER_TYPE=lambda
LAMBDA_REGION=eu-west-2
LAMBDA_ROLE=<ASK_ME>

@martmull martmull linked an issue Jul 12, 2024 that may be closed by this pull request
Copy link

github-actions bot commented Jul 12, 2024

Warnings
⚠️ Changes were made to the environment variables, but not to the documentation - Please review your changes and check if a change needs to be documented!

Generated by 🚫 dangerJS against 2ec302a

@martmull martmull marked this pull request as ready for review July 12, 2024 09:59
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

  • Added dependencies for AWS Lambda and file handling in package.json
  • Introduced CUSTOM_CODE_ENGINE_DRIVER_TYPE in .env.example
  • Integrated FunctionModule into core-engine.module.ts
  • Made FileUploadModule globally available in file-upload.module.ts
  • Added Function entry to FileFolder enum in file-folder.interface.ts

29 file(s) reviewed, 16 comment(s)
Edit PR Review Bot Settings

if (code && code !== 0) {
reject(new Error(`Child process exited with code ${code}`));
fs.unlink(tmpFilePath);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🧠 logic: Ensure fs.unlink handles errors to avoid unhandled promise rejections.

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

(updates since last review)

  • Fixed typos in import paths across multiple files for FunctionWorkspaceEntity
  • Implemented AWS Lambda driver for custom code execution (lambda.driver.ts)
  • Implemented local driver for custom code execution (local.driver.ts)
  • Added new fields and relations in function.workspace-entity.ts to support custom code executor
  • Ensured correct initialization of injected driver in custom-code-engine.service.ts

9 file(s) reviewed, 6 comment(s)
Edit PR Review Bot Settings

this.fileUploadService = options.fileUploadService;
}

async upsert(file: FileUpload) {
Copy link
Contributor

Choose a reason for hiding this comment

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

🪶 style: Consider adding error handling for file reading and TypeScript compilation.

}

async upsert({ createReadStream, filename, mimetype }: FileUpload) {
const typescriptCode = await readFileContent(createReadStream());
Copy link
Contributor

Choose a reason for hiding this comment

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

🧠 logic: Ensure readFileContent handles potential errors from the stream.


const { path: builtSourcePath } = await this.fileUploadService.uploadFile({
file: javascriptCode,
filename: '.js',
Copy link
Contributor

Choose a reason for hiding this comment

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

🪶 style: Filename should include the original filename with a .js extension for clarity.

child.on('message', (message: object) => {
resolve(message);
child.kill();
fs.unlink(tmpFilePath);
Copy link
Contributor

Choose a reason for hiding this comment

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

🧠 logic: Ensure fs.unlink handles errors to avoid unhandled promise rejections.

child.on('error', (error) => {
reject(error);
child.kill();
fs.unlink(tmpFilePath);
Copy link
Contributor

Choose a reason for hiding this comment

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

🧠 logic: Ensure fs.unlink handles errors to avoid unhandled promise rejections.

if (code && code !== 0) {
reject(new Error(`Child process exited with code ${code}`));
fs.unlink(tmpFilePath);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🧠 logic: Ensure fs.unlink handles errors to avoid unhandled promise rejections.

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

(updates since last review)

  • Removed JavaScript code upload in packages/twenty-server/src/engine/integrations/custom-code-engine/drivers/lambda.driver.ts
  • Set builtSourcePath to undefined in LambdaDriver class
  • Updated filename handling logic in packages/twenty-server/src/engine/integrations/custom-code-engine/drivers/local.driver.ts
  • Improved temporary file creation and cleanup in local.driver.ts

2 file(s) reviewed, 6 comment(s)
Edit PR Review Bot Settings

@martmull martmull force-pushed the 6181-workflows-create-a-custom-code-executor branch from a99bc5f to 48599a5 Compare July 12, 2024 13:24
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

(updates since last review)

  • Introduced optional builtSourcePath in custom-code-engine-driver.interface.ts
  • Removed JavaScript code upload in lambda.driver.ts
  • Set builtSourcePath to undefined in LambdaDriver class
  • Updated filename handling logic in local.driver.ts to ensure correct JavaScript filename generation
  • Handled upload of compiled JavaScript file with correct filename in local.driver.ts

3 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

(updates since last review)

  • Renamed upsert to generateExecutable in packages/twenty-server/src/engine/core-modules/function/function.service.ts
  • Updated generateExecutable method in packages/twenty-server/src/engine/integrations/custom-code-engine/custom-code-engine.service.ts
  • Modified generateExecutable method in packages/twenty-server/src/engine/integrations/custom-code-engine/drivers/interfaces/custom-code-engine-driver.interface.ts
  • Enhanced Lambda function deployment logic in packages/twenty-server/src/engine/integrations/custom-code-engine/drivers/lambda.driver.ts
  • Improved TypeScript compilation and execution in packages/twenty-server/src/engine/integrations/custom-code-engine/drivers/local.driver.ts

5 file(s) reviewed, 3 comment(s)
Edit PR Review Bot Settings

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

(updates since last review)

  • Modified upsertFunction to update existing functions by name in packages/twenty-server/src/engine/core-modules/function/function.service.ts
  • Minor refactoring for readability and maintainability in packages/twenty-server/src/engine/core-modules/function/function.service.ts

1 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings

Copy link
Contributor

@Devessier Devessier left a comment

Choose a reason for hiding this comment

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

That's impressive what you have made in a few days. Congrats!

I left a few comments about implementation details.

Copy link
Member

@charlesBochet charlesBochet left a comment

Choose a reason for hiding this comment

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

That's great. Overall, I have 4 concerns:

  • functionMetadata is not the right naming actually. Ideally it should be function but this is a keyword reserved in the language. So I think we should actually find something else: customFunction, functionBlock, codeFunction?
  • code-engine is confusing with the twenty-engine. Maybe code-runner ? not a fan as we clearly have a build/compile and a run/execute phase
    -,I think we should not pass the file-storage to the drivers
  • and drivers should be in the integrations folder

@FelixMalfait
Copy link
Member

I don't like customFunction (everything is custom), ok for codeFunction on my side, I don't love it but don't have anything better to offer

@martmull
Copy link
Contributor Author

@charlesBochet @FelixMalfait what do you think about Script or CodeSnippet to rename Function? CodeFunction seems pleonastic to me

@martmull
Copy link
Contributor Author

looks like we go for serverlessFunction

@martmull martmull force-pushed the 6181-workflows-create-a-custom-code-executor branch 2 times, most recently from 7aa6df7 to b461901 Compare July 17, 2024 12:25
@martmull martmull force-pushed the 6181-workflows-create-a-custom-code-executor branch from b461901 to fca54cb Compare July 17, 2024 13:54
@martmull martmull force-pushed the 6181-workflows-create-a-custom-code-executor branch from 272adc5 to 00be140 Compare July 17, 2024 15:40
Copy link
Member

@charlesBochet charlesBochet left a comment

Choose a reason for hiding this comment

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

Another masterpiece!

@charlesBochet charlesBochet merged commit 47ddc7b into main Jul 17, 2024
12 of 13 checks passed
@charlesBochet charlesBochet deleted the 6181-workflows-create-a-custom-code-executor branch July 17, 2024 15:53
@FelixMalfait
Copy link
Member

Bravo @martmull !!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Workflows] Create a custom code executor
5 participants