Skip to content

The open alternative to Salesforce, designed for AI.

Skip logic function jobs whose application was uninstalled (#27321)

martmullc29a6c04cada
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-trigger/__tests__/logic-function-job-runner.service.spec.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-trigger/__tests__/logic-function-job-runner.service.spec.ts
new file mode 100644
index 00000000000..86ba9f67ac5
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-trigger/__tests__/logic-function-job-runner.service.spec.ts
@@ -0,0 +1,85 @@
+import {
+ LogicFunctionExecutionException,
+ LogicFunctionExecutionExceptionCode,
+ type LogicFunctionExecutorService,
+} from 'src/engine/core-modules/logic-function/logic-function-executor/logic-function-executor.service';
+import { LogicFunctionJobRunnerService } from 'src/engine/core-modules/logic-function/logic-function-trigger/logic-function-job-runner.service';
+import { type LogicFunctionEntity } from 'src/engine/metadata-modules/logic-function/logic-function.entity';
+import { type WorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/workspace-scoped-repository';
+
+describe('LogicFunctionJobRunnerService', () => {
+ const execute = jest.fn();
+ const existsBy = jest.fn();
+ const persistRetryCount = jest.fn();
+ const logicFunctionJobRunnerService = new LogicFunctionJobRunnerService(
+ { execute } as unknown as LogicFunctionExecutorService,
+ {
+ existsBy,
+ } as unknown as WorkspaceScopedRepository<LogicFunctionEntity>,
+ );
+ const notFoundError = new LogicFunctionExecutionException(
+ 'Logic function with id logic-function-id not found',
+ LogicFunctionExecutionExceptionCode.LOGIC_FUNCTION_NOT_FOUND,
+ );
+
+ const run = () =>
+ logicFunctionJobRunnerService.run({
+ logicFunctionPayload: {
+ logicFunctionId: 'logic-function-id',
+ workspaceId: 'workspace-id',
+ },
+ retryLimit: 3,
+ persistRetryCount,
+ });
+
+ const expectExecutedOnce = () => {
+ expect(execute).toHaveBeenCalledTimes(1);
+ expect(execute).toHaveBeenCalledWith({
+ logicFunctionId: 'logic-function-id',
+ workspaceId: 'workspace-id',
+ payload: {},
+ userId: undefined,
+ userWorkspaceId: undefined,
+ retry: { retryCount: 0, maxRetries: 3 },
+ });
+ };
+
+ beforeEach(() => {
+ jest.clearAllMocks();
+ });
+
+ it('skips the job when its application was uninstalled', async () => {
+ execute.mockRejectedValue(notFoundError);
+ existsBy.mockResolvedValue(false);
+
+ await expect(run()).resolves.toBeUndefined();
+ expectExecutedOnce();
+ expect(existsBy).toHaveBeenCalledTimes(1);
+ expect(existsBy).toHaveBeenCalledWith('workspace-id', {
+ id: 'logic-function-id',
+ });
+ expect(persistRetryCount).not.toHaveBeenCalled();
+ });
+
+ it('fails the job when the logic function still exists in the database', async () => {
+ execute.mockRejectedValue(notFoundError);
+ existsBy.mockResolvedValue(true);
+
+ await expect(run()).rejects.toBe(notFoundError);
+ expectExecutedOnce();
+ expect(existsBy).toHaveBeenCalledTimes(1);
+ });
+
+ it('fails the job on other execution errors', async () => {
+ const rateLimitError = new LogicFunctionExecutionException(
+ 'Logic function execution rate limit exceeded',
+ LogicFunctionExecutionExceptionCode.RATE_LIMIT_EXCEEDED,
+ );
+
+ execute.mockRejectedValue(rateLimitError);
+
+ await expect(run()).rejects.toBe(rateLimitError);
+ expectExecutedOnce();
+ expect(existsBy).not.toHaveBeenCalled();
+ });
+});
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-trigger/logic-function-job-runner.service.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-trigger/logic-function-job-runner.service.ts
index b6a48b5b0c3..dd5b2de3d2d 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-trigger/logic-function-job-runner.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-trigger/logic-function-job-runner.service.ts
@@ -3,13 +3,20 @@ import { Injectable, Logger } from '@nestjs/common';
import { RetryableLogicFunctionError } from 'twenty-shared/logic-function';
import { isUsageRefusedError } from 'src/engine/core-modules/billing/utils/is-usage-refused-error.util';
-import { LogicFunctionExecutorService } from 'src/engine/core-modules/logic-function/logic-function-executor/logic-function-executor.service';
+import {
+ LogicFunctionExecutionException,
+ LogicFunctionExecutionExceptionCode,
+ LogicFunctionExecutorService,
+} from 'src/engine/core-modules/logic-function/logic-function-executor/logic-function-executor.service';
import { LOGIC_FUNCTION_APPLICATION_RETRY_LIMIT } from 'src/engine/core-modules/logic-function/logic-function-trigger/constants/logic-function-application-retry-limit.constant';
import { isRetryableLogicFunctionExecutionError } from 'src/engine/core-modules/logic-function/logic-function-trigger/utils/is-retryable-logic-function-execution-error.util';
+import { LogicFunctionEntity } from 'src/engine/metadata-modules/logic-function/logic-function.entity';
import {
LogicFunctionException,
LogicFunctionExceptionCode,
} from 'src/engine/metadata-modules/logic-function/logic-function.exception';
+import { InjectWorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/inject-workspace-scoped-repository.decorator';
+import { WorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/workspace-scoped-repository';
export type LogicFunctionJobPayload = {
logicFunctionId: string;
@@ -26,6 +33,8 @@ export class LogicFunctionJobRunnerService {
constructor(
private readonly logicFunctionExecutorService: LogicFunctionExecutorService,
+ @InjectWorkspaceScopedRepository(LogicFunctionEntity)
+ private readonly logicFunctionRepository: WorkspaceScopedRepository<LogicFunctionEntity>,
) {}
async run({
@@ -77,6 +86,26 @@ export class LogicFunctionJobRunnerService {
return;
}
+ // Delayed or retried jobs can outlive their application: once it is
+ // uninstalled the job has nothing left to run, so retrying is pointless.
+ // The metadata cache can lag right after an install, so only the
+ // database tells an uninstall apart from a stale cache miss.
+ if (
+ error instanceof LogicFunctionExecutionException &&
+ error.code ===
+ LogicFunctionExecutionExceptionCode.LOGIC_FUNCTION_NOT_FOUND &&
+ !(await this.logicFunctionRepository.existsBy(
+ logicFunctionPayload.workspaceId,
+ { id: logicFunctionPayload.logicFunctionId },
+ ))
+ ) {
+ this.logger.log(
+ `Skipping function ${logicFunctionPayload.logicFunctionId} (workspace ${logicFunctionPayload.workspaceId}): ${error.message}`,
+ );
+
+ return;
+ }
+
if (isUsageRefusedError(error)) {
this.logger.warn(
`Skipping function ${logicFunctionPayload.logicFunctionId} (workspace ${logicFunctionPayload.workspaceId}): ${error.message}`,