Skip to content

The open alternative to Salesforce, designed for AI.

Name dependency layers per application and move functions onto them on their next run (#29085)

Paul Rastoin9f92876414cb
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-package/application-dependency-files.service.ts b/packages/twenty-server/src/engine/core-modules/application/application-package/application-dependency-files.service.ts
index b54908a6f4f..802597a235a 100644
--- a/packages/twenty-server/src/engine/core-modules/application/application-package/application-dependency-files.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/application/application-package/application-dependency-files.service.ts
@@ -2,10 +2,10 @@ import { Injectable } from '@nestjs/common';
import { FileFolder } from 'twenty-shared/types';
-import {
- assertMatchingDependencyChecksumsOrThrow,
- type DependencyChecksums,
-} from 'src/engine/core-modules/application/application-package/utils/assert-matching-dependency-checksums-or-throw.util';
+import { assertMatchingDependencyChecksumsOrThrow } from 'src/engine/core-modules/application/application-package/utils/assert-matching-dependency-checksums-or-throw.util';
+import { computeApplicationPackageChecksums } from 'src/engine/core-modules/application/application-package/utils/compute-application-package-checksums.util';
+import { computeLegacyApplicationPackageChecksums } from 'src/engine/core-modules/application/application-package/utils/compute-legacy-application-package-checksums.util';
+import { type DependencyChecksums } from 'src/engine/core-modules/application/application-package/utils/is-matching-dependency-checksums.util';
import { FileStorageService } from 'src/engine/core-modules/file-storage/services/file-storage.service';
import { streamToBuffer } from 'src/utils/stream-to-buffer';
@@ -14,6 +14,15 @@ type ApplicationIdentifier = {
applicationUniversalIdentifier: string;
};
+type StoredDependencyFiles = {
+ packageJsonContent: string;
+ yarnLockContent: string | null;
+ storedFilesChecksums: {
+ packageJsonChecksum: string;
+ yarnLockChecksum: string | null;
+ };
+};
+
@Injectable()
export class ApplicationDependencyFilesService {
constructor(private readonly fileStorageService: FileStorageService) {}
@@ -24,25 +33,21 @@ export class ApplicationDependencyFilesService {
declaredChecksums,
}: ApplicationIdentifier & {
declaredChecksums: DependencyChecksums;
- }): Promise<{
- packageJsonContent: string;
- yarnLockContent: string | null;
- storedFilesChecksums: {
- packageJsonChecksum: string;
- yarnLockChecksum: string | null;
- };
- }> {
- const { packageJsonContent, yarnLockContent } =
+ }): Promise<StoredDependencyFiles> {
+ const { packageJsonContent, yarnLockContent, storedFilesChecksums } =
await this.getDependencyContents({
workspaceId,
applicationUniversalIdentifier,
});
- const storedFilesChecksums = assertMatchingDependencyChecksumsOrThrow({
+ assertMatchingDependencyChecksumsOrThrow({
applicationUniversalIdentifier,
declaredChecksums,
- packageJsonContent,
- yarnLockContent,
+ storedFilesChecksums,
+ legacyStoredFilesChecksums: computeLegacyApplicationPackageChecksums({
+ packageJsonContent,
+ yarnLockContent,
+ }),
});
return { packageJsonContent, yarnLockContent, storedFilesChecksums };
@@ -51,10 +56,7 @@ export class ApplicationDependencyFilesService {
private async getDependencyContents({
workspaceId,
applicationUniversalIdentifier,
- }: ApplicationIdentifier): Promise<{
- packageJsonContent: string;
- yarnLockContent: string | null;
- }> {
+ }: ApplicationIdentifier): Promise<StoredDependencyFiles> {
const readDependencyFile = async (resourcePath: string) =>
(
await streamToBuffer(
@@ -79,6 +81,13 @@ export class ApplicationDependencyFilesService {
isYarnLockStored ? readDependencyFile('yarn.lock') : null,
]);
- return { packageJsonContent, yarnLockContent };
+ return {
+ packageJsonContent,
+ yarnLockContent,
+ storedFilesChecksums: computeApplicationPackageChecksums({
+ packageJsonContent,
+ yarnLockContent,
+ }),
+ };
}
}
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-package/utils/__tests__/assert-matching-dependency-checksums-or-throw.util.spec.ts b/packages/twenty-server/src/engine/core-modules/application/application-package/utils/__tests__/assert-matching-dependency-checksums-or-throw.util.spec.ts
index cefcebaef6c..29033e9a139 100644
--- a/packages/twenty-server/src/engine/core-modules/application/application-package/utils/__tests__/assert-matching-dependency-checksums-or-throw.util.spec.ts
+++ b/packages/twenty-server/src/engine/core-modules/application/application-package/utils/__tests__/assert-matching-dependency-checksums-or-throw.util.spec.ts
@@ -12,15 +12,23 @@ const STORED_FILES = {
const STORED_FILES_CHECKSUMS = computeApplicationPackageChecksums(STORED_FILES);
+const LEGACY_STORED_FILES_CHECKSUMS =
+ computeLegacyApplicationPackageChecksums(STORED_FILES);
+
+const STORED_CHECKSUMS = {
+ storedFilesChecksums: STORED_FILES_CHECKSUMS,
+ legacyStoredFilesChecksums: LEGACY_STORED_FILES_CHECKSUMS,
+};
+
describe('assertMatchingDependencyChecksumsOrThrow', () => {
- it('returns the checksums of the stored files when the declared ones match', () => {
- expect(
+ it('accepts declared checksums that describe the stored files', () => {
+ expect(() =>
assertMatchingDependencyChecksumsOrThrow({
applicationUniversalIdentifier: APPLICATION_UNIVERSAL_IDENTIFIER,
declaredChecksums: STORED_FILES_CHECKSUMS,
- ...STORED_FILES,
+ ...STORED_CHECKSUMS,
}),
- ).toEqual(STORED_FILES_CHECKSUMS);
+ ).not.toThrow();
});
it('refuses a package.json checksum that does not describe the stored file', () => {
@@ -31,7 +39,7 @@ describe('assertMatchingDependencyChecksumsOrThrow', () => {
...STORED_FILES_CHECKSUMS,
packageJsonChecksum: 'other-package-json',
},
- ...STORED_FILES,
+ ...STORED_CHECKSUMS,
}),
).toThrow(ApplicationException);
});
@@ -44,7 +52,7 @@ describe('assertMatchingDependencyChecksumsOrThrow', () => {
...STORED_FILES_CHECKSUMS,
yarnLockChecksum: 'other-yarn-lock',
},
- ...STORED_FILES,
+ ...STORED_CHECKSUMS,
}),
).toThrow(ApplicationException);
});
@@ -57,34 +65,42 @@ describe('assertMatchingDependencyChecksumsOrThrow', () => {
...STORED_FILES_CHECKSUMS,
yarnLockChecksum: null,
},
- ...STORED_FILES,
+ ...STORED_CHECKSUMS,
}),
).toThrow(ApplicationException);
});
it('ignores the declared yarn.lock checksum when no yarn.lock is stored', () => {
- expect(
+ const storedFilesWithoutYarnLock = {
+ packageJsonContent: STORED_FILES.packageJsonContent,
+ yarnLockContent: null,
+ };
+
+ expect(() =>
assertMatchingDependencyChecksumsOrThrow({
applicationUniversalIdentifier: APPLICATION_UNIVERSAL_IDENTIFIER,
declaredChecksums: {
...STORED_FILES_CHECKSUMS,
yarnLockChecksum: 'declared-yarn-lock',
},
- packageJsonContent: STORED_FILES.packageJsonContent,
- yarnLockContent: null,
+ storedFilesChecksums: computeApplicationPackageChecksums(
+ storedFilesWithoutYarnLock,
+ ),
+ legacyStoredFilesChecksums: computeLegacyApplicationPackageChecksums(
+ storedFilesWithoutYarnLock,
+ ),
}),
- ).toEqual({ ...STORED_FILES_CHECKSUMS, yarnLockChecksum: null });
+ ).not.toThrow();
});
- it('accepts the md5 checksums of older CLIs and returns the current ones', () => {
- expect(
+ it('accepts the md5 checksums of older CLIs', () => {
+ expect(() =>
assertMatchingDependencyChecksumsOrThrow({
applicationUniversalIdentifier: APPLICATION_UNIVERSAL_IDENTIFIER,
- declaredChecksums:
- computeLegacyApplicationPackageChecksums(STORED_FILES),
- ...STORED_FILES,
+ declaredChecksums: LEGACY_STORED_FILES_CHECKSUMS,
+ ...STORED_CHECKSUMS,
}),
- ).toEqual(STORED_FILES_CHECKSUMS);
+ ).not.toThrow();
});
it('refuses a pair that mixes both formulas', () => {
@@ -93,11 +109,9 @@ describe('assertMatchingDependencyChecksumsOrThrow', () => {
applicationUniversalIdentifier: APPLICATION_UNIVERSAL_IDENTIFIER,
declaredChecksums: {
packageJsonChecksum: STORED_FILES_CHECKSUMS.packageJsonChecksum,
- yarnLockChecksum:
- computeLegacyApplicationPackageChecksums(STORED_FILES)
- .yarnLockChecksum,
+ yarnLockChecksum: LEGACY_STORED_FILES_CHECKSUMS.yarnLockChecksum,
},
- ...STORED_FILES,
+ ...STORED_CHECKSUMS,
}),
).toThrow(ApplicationException);
});
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-package/utils/assert-matching-dependency-checksums-or-throw.util.ts b/packages/twenty-server/src/engine/core-modules/application/application-package/utils/assert-matching-dependency-checksums-or-throw.util.ts
index 4f7c3518170..a95e2d63c4f 100644
--- a/packages/twenty-server/src/engine/core-modules/application/application-package/utils/assert-matching-dependency-checksums-or-throw.util.ts
+++ b/packages/twenty-server/src/engine/core-modules/application/application-package/utils/assert-matching-dependency-checksums-or-throw.util.ts
@@ -1,59 +1,26 @@
-import { isDefined } from 'twenty-shared/utils';
-
-import { computeApplicationPackageChecksums } from 'src/engine/core-modules/application/application-package/utils/compute-application-package-checksums.util';
-import { computeLegacyApplicationPackageChecksums } from 'src/engine/core-modules/application/application-package/utils/compute-legacy-application-package-checksums.util';
+import {
+ type DependencyChecksums,
+ isMatchingDependencyChecksums,
+} from 'src/engine/core-modules/application/application-package/utils/is-matching-dependency-checksums.util';
import {
ApplicationException,
ApplicationExceptionCode,
} from 'src/engine/core-modules/application/application.exception';
-export type DependencyChecksums = {
- packageJsonChecksum: string | null;
- yarnLockChecksum: string | null;
-};
-
-const isMatchingDependencyChecksums = ({
- declaredChecksums,
- storedFilesChecksums,
-}: {
- declaredChecksums: DependencyChecksums;
- storedFilesChecksums: DependencyChecksums;
-}): boolean =>
- storedFilesChecksums.packageJsonChecksum ===
- declaredChecksums.packageJsonChecksum &&
- (!isDefined(storedFilesChecksums.yarnLockChecksum) ||
- storedFilesChecksums.yarnLockChecksum ===
- declaredChecksums.yarnLockChecksum);
-
export const assertMatchingDependencyChecksumsOrThrow = ({
applicationUniversalIdentifier,
declaredChecksums,
- packageJsonContent,
- yarnLockContent,
+ storedFilesChecksums,
+ legacyStoredFilesChecksums,
}: {
applicationUniversalIdentifier: string;
declaredChecksums: DependencyChecksums;
- packageJsonContent: string;
- yarnLockContent: string | null;
-}): { packageJsonChecksum: string; yarnLockChecksum: string | null } => {
- const storedFilesChecksums = computeApplicationPackageChecksums({
- packageJsonContent,
- yarnLockContent,
- });
-
- const acceptedStoredFilesChecksums = [
- storedFilesChecksums,
- computeLegacyApplicationPackageChecksums({
- packageJsonContent,
- yarnLockContent,
- }),
- ];
-
- const isMatching = acceptedStoredFilesChecksums.some((acceptedChecksums) =>
- isMatchingDependencyChecksums({
- declaredChecksums,
- storedFilesChecksums: acceptedChecksums,
- }),
+ storedFilesChecksums: DependencyChecksums;
+ legacyStoredFilesChecksums: DependencyChecksums;
+}): void => {
+ const isMatching = [storedFilesChecksums, legacyStoredFilesChecksums].some(
+ (filesChecksums) =>
+ isMatchingDependencyChecksums({ declaredChecksums, filesChecksums }),
);
if (!isMatching) {
@@ -62,6 +29,4 @@ export const assertMatchingDependencyChecksumsOrThrow = ({
ApplicationExceptionCode.INVALID_INPUT,
);
}
-
- return storedFilesChecksums;
};
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-package/utils/is-matching-dependency-checksums.util.ts b/packages/twenty-server/src/engine/core-modules/application/application-package/utils/is-matching-dependency-checksums.util.ts
new file mode 100644
index 00000000000..c0ec385f4e0
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/application/application-package/utils/is-matching-dependency-checksums.util.ts
@@ -0,0 +1,18 @@
+import { isDefined } from 'twenty-shared/utils';
+
+export type DependencyChecksums = {
+ packageJsonChecksum: string | null;
+ yarnLockChecksum: string | null;
+};
+
+export const isMatchingDependencyChecksums = ({
+ declaredChecksums,
+ filesChecksums,
+}: {
+ declaredChecksums: DependencyChecksums;
+ filesChecksums: DependencyChecksums;
+}): boolean =>
+ filesChecksums.packageJsonChecksum ===
+ declaredChecksums.packageJsonChecksum &&
+ (!isDefined(filesChecksums.yarnLockChecksum) ||
+ filesChecksums.yarnLockChecksum === declaredChecksums.yarnLockChecksum);
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda.driver.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda.driver.ts
index 08ca5ad30e1..5e830e2f536 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda.driver.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda.driver.ts
@@ -69,6 +69,7 @@ export class LambdaDriver implements LogicFunctionDriver {
options.sdkClientArchiveService,
options.cacheLockService,
options.workspaceCacheService,
+ options.coreEntityCacheService,
);
this.executorManager = new LambdaExecutorManagerService(
options,
@@ -126,7 +127,6 @@ export class LambdaDriver implements LogicFunctionDriver {
async execute({
flatLogicFunction,
flatApplication,
- applicationUniversalIdentifier,
payload,
context,
env,
@@ -155,7 +155,6 @@ export class LambdaDriver implements LogicFunctionDriver {
await this.executorManager.buildExecutor({
flatLogicFunction,
flatApplication,
- applicationUniversalIdentifier,
});
buildExecutorMs = Date.now() - buildStart;
@@ -182,7 +181,7 @@ export class LambdaDriver implements LogicFunctionDriver {
executorPayload.code =
await this.logicFunctionResourceService.getBuiltCode({
workspaceId: flatLogicFunction.workspaceId,
- applicationUniversalIdentifier,
+ applicationUniversalIdentifier: flatApplication.universalIdentifier,
builtHandlerPath: flatLogicFunction.builtHandlerPath,
});
getBuiltCodeMs = Date.now() - fetchStart;
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/legacy-deps-layer/dependency-checksums-seeded-before-2-54.constant.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/legacy-deps-layer/dependency-checksums-seeded-before-2-54.constant.ts
new file mode 100644
index 00000000000..3eec3ab65a0
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/legacy-deps-layer/dependency-checksums-seeded-before-2-54.constant.ts
@@ -0,0 +1,18 @@
+// Only for the current release (2.54): Workspace apps still on the files 2.52
+// or 2.53 gave them also share a layer. The 2.52 upgrade wrote a snapshot
+// frozen before the body-parser, nodemailer and twenty-ui bumps, and the
+// defaults changed again in 2.53, so each set has its own checksums
+export const DEPENDENCY_CHECKSUMS_SEEDED_BEFORE_2_54 = {
+ v2_52UpgradeSnapshot: {
+ packageJsonChecksum: '7d84e53bcbc345d9652b048470f59695',
+ yarnLockChecksum: '6a09947c24d8cba9011da22936fe3e87',
+ },
+ v2_52Defaults: {
+ packageJsonChecksum: '444fb064df1febd5f30c8bcadf58e536',
+ yarnLockChecksum: '225201a4fd2b60ab92c865fce0fd18d6',
+ },
+ v2_53Defaults: {
+ packageJsonChecksum: '444fb064df1febd5f30c8bcadf58e536',
+ yarnLockChecksum: '5ed70f81bf547755d85b6dd28614a42a',
+ },
+};
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-executor-manager.service.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-executor-manager.service.ts
index 15790910582..f2bdf9395c6 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-executor-manager.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-executor-manager.service.ts
@@ -53,7 +53,6 @@ import { type WorkspaceCacheService } from 'src/engine/workspace-cache/services/
type ExecutorBuildContext = {
flatLogicFunction: FlatLogicFunction;
flatApplication: FlatApplication;
- applicationUniversalIdentifier: string;
};
export class LambdaExecutorManagerService {
@@ -180,7 +179,7 @@ export class LambdaExecutorManagerService {
}
async installPrebuiltBundle(context: ExecutorBuildContext): Promise<void> {
- const { flatLogicFunction, applicationUniversalIdentifier } = context;
+ const { flatLogicFunction, flatApplication } = context;
if (!isNonEmptyString(flatLogicFunction.checksum)) {
throw new LogicFunctionException(
@@ -205,7 +204,7 @@ export class LambdaExecutorManagerService {
const compiledCode =
await this.logicFunctionResourceService.getBuiltCode({
workspaceId: flatLogicFunction.workspaceId,
- applicationUniversalIdentifier,
+ applicationUniversalIdentifier: flatApplication.universalIdentifier,
builtHandlerPath: flatLogicFunction.builtHandlerPath,
});
@@ -287,7 +286,7 @@ export class LambdaExecutorManagerService {
canSkip: boolean;
lambdaExecutor: GetFunctionCommandOutput | undefined;
}> {
- const { flatApplication, applicationUniversalIdentifier } = context;
+ const { flatApplication } = context;
const lambdaExecutor = await this.getLambdaExecutor(
context.flatLogicFunction,
);
@@ -298,11 +297,10 @@ export class LambdaExecutorManagerService {
isDefined(lambdaExecutor) &&
isActive &&
!flatApplication.isSdkLayerStale &&
- this.layerManager.hasExpectedLayers({
+ (await this.layerManager.hasExpectedLayers({
lambdaExecutor,
flatApplication,
- applicationUniversalIdentifier,
- });
+ }));
return { canSkip, lambdaExecutor };
}
@@ -310,7 +308,6 @@ export class LambdaExecutorManagerService {
private async ensureExecutor({
flatLogicFunction,
flatApplication,
- applicationUniversalIdentifier,
lambdaExecutor,
}: ExecutorBuildContext & {
lambdaExecutor: GetFunctionCommandOutput | undefined;
@@ -318,10 +315,7 @@ export class LambdaExecutorManagerService {
let depsLayerArn: string;
try {
- depsLayerArn = await this.layerManager.ensureDepsLayer({
- flatApplication,
- applicationUniversalIdentifier,
- });
+ depsLayerArn = await this.layerManager.ensureDepsLayer(flatApplication);
} catch (error) {
if (
error instanceof LogicFunctionException &&
@@ -345,10 +339,7 @@ export class LambdaExecutorManagerService {
let sdkLayerArn: string;
try {
- sdkLayerArn = await this.layerManager.ensureSdkLayer({
- flatApplication,
- applicationUniversalIdentifier,
- });
+ sdkLayerArn = await this.layerManager.ensureSdkLayer(flatApplication);
} catch (error) {
if (
error instanceof LogicFunctionException &&
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-layer-manager.service.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-layer-manager.service.ts
index 68f5dbc3806..a15cfc170ea 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-layer-manager.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-layer-manager.service.ts
@@ -11,7 +11,15 @@ import { EMPTY_YARN_LOCK_CONTENT } from 'twenty-shared/application';
import { isDefined } from 'twenty-shared/utils';
import { type ApplicationDependencyFilesService } from 'src/engine/core-modules/application/application-package/application-dependency-files.service';
+import { WORKSPACE_CUSTOM_APPLICATION_DEPENDENCIES_DIRECTORY_NAME } from 'src/engine/core-modules/application/application-package/constants/workspace-custom-application-dependencies-directory-name';
+import { getDefaultApplicationPackageFields } from 'src/engine/core-modules/application/application-package/utils/get-default-application-package-fields.util';
+import { isMatchingDependencyChecksums } from 'src/engine/core-modules/application/application-package/utils/is-matching-dependency-checksums.util';
+import {
+ ApplicationException,
+ ApplicationExceptionCode,
+} from 'src/engine/core-modules/application/application.exception';
import { type FlatApplication } from 'src/engine/core-modules/application/types/flat-application.type';
+import { type CoreEntityCacheService } from 'src/engine/core-entity-cache/services/core-entity-cache.service';
import { type CacheLockService } from 'src/engine/core-modules/cache-lock/cache-lock.service';
import {
BUILD_LOCK_RETRY_MS,
@@ -21,10 +29,13 @@ import {
SDK_LAYER_PREFIX_IN_ZIP,
} from 'src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/constants/lambda-driver.constant';
import { type LambdaAwsClientService } from 'src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-aws-client.service';
+import { DEPENDENCY_CHECKSUMS_SEEDED_BEFORE_2_54 } from 'src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/legacy-deps-layer/dependency-checksums-seeded-before-2-54.constant';
import { type LambdaToolFunctionsService } from 'src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-tool-functions.service';
import { type LambdaDriverOptions } from 'src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/types/lambda-driver.type';
import { getLambdaDepsLayerName } from 'src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/get-lambda-deps-layer-name.util';
+import { getLambdaLayerNameFromArn } from 'src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/get-lambda-layer-name-from-arn.util';
import { getLambdaSdkLayerName } from 'src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/get-lambda-sdk-layer-name.util';
+import { getLambdaWorkspaceCustomSharedDepsLayerName } from 'src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/get-lambda-workspace-custom-shared-deps-layer-name.util';
import { reprefixLambdaZipEntries } from 'src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/reprefix-lambda-zip-entries.util';
import { sanitizeApplicationPackageJson } from 'src/engine/core-modules/logic-function/logic-function-resource/utils/sanitize-application-package-json.util';
import { type SdkClientArchiveService } from 'src/engine/core-modules/sdk-client/sdk-client-archive.service';
@@ -35,9 +46,9 @@ import {
LogicFunctionExceptionCode,
} from 'src/engine/metadata-modules/logic-function/logic-function.exception';
-type LayerAppContext = {
- flatApplication: FlatApplication;
- applicationUniversalIdentifier: string;
+type DepsLayerDependencies = {
+ packageJson: string;
+ yarnLock: string | null;
};
const LAYER_LOCK_OPTIONS = {
@@ -60,54 +71,130 @@ export class LambdaLayerManagerService {
private readonly sdkClientArchiveService: SdkClientArchiveService,
private readonly cacheLockService: CacheLockService,
private readonly workspaceCacheService: WorkspaceCacheService,
+ private readonly coreEntityCacheService: CoreEntityCacheService,
) {}
- async ensureDepsLayer(context: LayerAppContext): Promise<string> {
- const layerName = getLambdaDepsLayerName({
- flatApplication: context.flatApplication,
+ async ensureDepsLayer(flatApplication: FlatApplication): Promise<string> {
+ if (await this.isWorkspaceCustomApplication(flatApplication)) {
+ return this.ensureWorkspaceCustomDepsLayer(flatApplication);
+ }
+
+ return this.ensureApplicationDepsLayer(flatApplication);
+ }
+
+ private async getDepsLayerNames(
+ flatApplication: FlatApplication,
+ ): Promise<string[]> {
+ const applicationDepsLayerName = getLambdaDepsLayerName({
+ flatApplication,
namespace: this.options.resourceNamespace,
});
+ if (!(await this.isWorkspaceCustomApplication(flatApplication))) {
+ return [applicationDepsLayerName];
+ }
+
+ return [
+ this.getWorkspaceCustomSharedDepsLayerName(flatApplication),
+ applicationDepsLayerName,
+ ];
+ }
+
+ private async ensureWorkspaceCustomDepsLayer(
+ flatApplication: FlatApplication,
+ ): Promise<string> {
+ const layerName =
+ this.getWorkspaceCustomSharedDepsLayerName(flatApplication);
const existingArn = await this.awsClient.getExistingLayerArn(layerName);
if (isDefined(existingArn)) {
return existingArn;
}
- const { packageJson, yarnLock } =
- await this.getVerifiedDependencies(context);
+ if (!(await this.canBuildWorkspaceCustomSharedDepsLayer(flatApplication))) {
+ return this.ensureApplicationDepsLayer(flatApplication);
+ }
- return this.cacheLockService.withLock(
- async () => {
- const arnCreatedWhileWaiting =
- await this.awsClient.getExistingLayerArn(layerName);
+ const dependencies = await this.getVerifiedDependencies(flatApplication);
- if (isDefined(arnCreatedWhileWaiting)) {
- return arnCreatedWhileWaiting;
- }
+ if (!isDefined(dependencies.yarnLock)) {
+ throw new ApplicationException(
+ `Workspace custom application ${flatApplication.universalIdentifier} has no stored yarn.lock`,
+ ApplicationExceptionCode.INVALID_INPUT,
+ );
+ }
- await this.createDepsLayer({ layerName, packageJson, yarnLock });
+ return this.buildDepsLayer({ layerName, dependencies });
+ }
- const newArn = await this.awsClient.getExistingLayerArn(layerName);
+ private async ensureApplicationDepsLayer(
+ flatApplication: FlatApplication,
+ ): Promise<string> {
+ const layerName = getLambdaDepsLayerName({
+ flatApplication,
+ namespace: this.options.resourceNamespace,
+ });
+ const existingArn = await this.awsClient.getExistingLayerArn(layerName);
- if (!isDefined(newArn)) {
- throw new Error(
- `Layer '${layerName}' was not created by the yarn install Lambda`,
- );
- }
+ if (isDefined(existingArn)) {
+ return existingArn;
+ }
- return newArn;
- },
- `lambda-deps-layer:${layerName}`,
- LAYER_LOCK_OPTIONS,
+ return this.buildDepsLayer({
+ layerName,
+ dependencies: await this.getVerifiedDependencies(flatApplication),
+ });
+ }
+
+ private async isWorkspaceCustomApplication(
+ flatApplication: FlatApplication,
+ ): Promise<boolean> {
+ const workspace = await this.coreEntityCacheService.get(
+ 'workspaceEntity',
+ flatApplication.workspaceId,
);
+
+ return workspace?.workspaceCustomApplicationId === flatApplication.id;
}
- async ensureSdkLayer(context: LayerAppContext): Promise<string> {
- const { flatApplication, applicationUniversalIdentifier } = context;
+ private getWorkspaceCustomSharedDepsLayerName(
+ flatApplication: FlatApplication,
+ ): string {
+ if (!isDefined(flatApplication.yarnLockChecksum)) {
+ throw new ApplicationException(
+ `Workspace custom application ${flatApplication.universalIdentifier} has no yarn.lock`,
+ ApplicationExceptionCode.INVALID_INPUT,
+ );
+ }
+
+ return getLambdaWorkspaceCustomSharedDepsLayerName({
+ yarnLockChecksum: flatApplication.yarnLockChecksum,
+ namespace: this.options.resourceNamespace,
+ });
+ }
+
+ private async canBuildWorkspaceCustomSharedDepsLayer(
+ flatApplication: FlatApplication,
+ ): Promise<boolean> {
+ const defaultPackageFields = await getDefaultApplicationPackageFields({
+ directory: WORKSPACE_CUSTOM_APPLICATION_DEPENDENCIES_DIRECTORY_NAME,
+ });
+
+ return [
+ defaultPackageFields,
+ ...Object.values(DEPENDENCY_CHECKSUMS_SEEDED_BEFORE_2_54),
+ ].some((knownChecksums) =>
+ isMatchingDependencyChecksums({
+ declaredChecksums: flatApplication,
+ filesChecksums: knownChecksums,
+ }),
+ );
+ }
+
+ async ensureSdkLayer(flatApplication: FlatApplication): Promise<string> {
const layerName = getLambdaSdkLayerName({
workspaceId: flatApplication.workspaceId,
- applicationUniversalIdentifier,
+ applicationUniversalIdentifier: flatApplication.universalIdentifier,
});
const existingArn = await this.findFreshSdkLayerArn({
@@ -135,7 +222,6 @@ export class LambdaLayerManagerService {
return this.rebuildSdkLayer({
flatApplication: refreshedFlatApplication,
- applicationUniversalIdentifier,
layerName,
});
},
@@ -172,16 +258,18 @@ export class LambdaLayerManagerService {
private async rebuildSdkLayer({
flatApplication,
- applicationUniversalIdentifier,
layerName,
- }: LayerAppContext & { layerName: string }): Promise<string> {
+ }: {
+ flatApplication: FlatApplication;
+ layerName: string;
+ }): Promise<string> {
await this.deleteAllLayerVersions(layerName);
const sdkArchiveBuffer =
await this.sdkClientArchiveService.downloadArchiveBuffer({
workspaceId: flatApplication.workspaceId,
applicationId: flatApplication.id,
- applicationUniversalIdentifier,
+ applicationUniversalIdentifier: flatApplication.universalIdentifier,
});
const zipBuffer = await reprefixLambdaZipEntries({
@@ -214,12 +302,12 @@ export class LambdaLayerManagerService {
await this.deleteAllLayerVersions(layerName);
}
- hasExpectedLayers({
+ private hasExpectedSdkLayer({
lambdaExecutor,
flatApplication,
- applicationUniversalIdentifier,
- }: LayerAppContext & {
+ }: {
lambdaExecutor: GetFunctionCommandOutput;
+ flatApplication: FlatApplication;
}): boolean {
const layers = lambdaExecutor.Configuration?.Layers;
@@ -227,13 +315,9 @@ export class LambdaLayerManagerService {
return false;
}
- const depsLayerName = getLambdaDepsLayerName({
- flatApplication,
- namespace: this.options.resourceNamespace,
- });
const sdkLayerName = getLambdaSdkLayerName({
workspaceId: flatApplication.workspaceId,
- applicationUniversalIdentifier,
+ applicationUniversalIdentifier: flatApplication.universalIdentifier,
});
const isOnCurrentSdkClient =
@@ -242,9 +326,63 @@ export class LambdaLayerManagerService {
flatApplication.sdkClientCoreChecksum;
return (
- layers.some((layer) => layer.Arn?.includes(depsLayerName)) &&
- layers.some((layer) => layer.Arn?.includes(sdkLayerName)) &&
- isOnCurrentSdkClient
+ isOnCurrentSdkClient &&
+ layers.some((layer) => layer.Arn?.includes(sdkLayerName))
+ );
+ }
+
+ async hasExpectedLayers({
+ lambdaExecutor,
+ flatApplication,
+ }: {
+ lambdaExecutor: GetFunctionCommandOutput;
+ flatApplication: FlatApplication;
+ }): Promise<boolean> {
+ if (!this.hasExpectedSdkLayer({ lambdaExecutor, flatApplication })) {
+ return false;
+ }
+
+ const depsLayerNames = await this.getDepsLayerNames(flatApplication);
+
+ return (lambdaExecutor.Configuration?.Layers ?? []).some((layer) => {
+ const layerName = isDefined(layer.Arn)
+ ? getLambdaLayerNameFromArn(layer.Arn)
+ : undefined;
+
+ return isDefined(layerName) && depsLayerNames.includes(layerName);
+ });
+ }
+
+ private async buildDepsLayer({
+ layerName,
+ dependencies,
+ }: {
+ layerName: string;
+ dependencies: DepsLayerDependencies;
+ }): Promise<string> {
+ return this.cacheLockService.withLock(
+ async () => {
+ const arnCreatedWhileWaiting =
+ await this.awsClient.getExistingLayerArn(layerName);
+
+ if (isDefined(arnCreatedWhileWaiting)) {
+ return arnCreatedWhileWaiting;
+ }
+
+ await this.createDepsLayer({ layerName, ...dependencies });
+
+ const newArn = await this.awsClient.getExistingLayerArn(layerName);
+
+ if (!isDefined(newArn)) {
+ throw new Error(
+ `Layer '${layerName}' was not created by the yarn install Lambda`,
+ );
+ }
+
+ return newArn;
+ },
+ `lambda-deps-layer:${layerName}`,
+ LAYER_LOCK_OPTIONS,
);
}
@@ -254,9 +392,7 @@ export class LambdaLayerManagerService {
yarnLock,
}: {
layerName: string;
- packageJson: string;
- yarnLock: string;
- }): Promise<void> {
+ } & DepsLayerDependencies): Promise<void> {
const existingArn = await this.awsClient.getExistingLayerArn(layerName);
if (isDefined(existingArn)) {
@@ -269,7 +405,7 @@ export class LambdaLayerManagerService {
await this.toolFunctions.runYarnInstallCreateLayer({
packageJson,
- yarnLock,
+ yarnLock: yarnLock ?? EMPTY_YARN_LOCK_CONTENT,
presignedUploadUrl,
});
@@ -312,22 +448,21 @@ export class LambdaLayerManagerService {
}
}
- private async getVerifiedDependencies({
- flatApplication,
- applicationUniversalIdentifier,
- }: LayerAppContext): Promise<{ packageJson: string; yarnLock: string }> {
+ private async getVerifiedDependencies(
+ flatApplication: FlatApplication,
+ ): Promise<DepsLayerDependencies> {
const { packageJsonContent, yarnLockContent } =
await this.applicationDependencyFilesService.getVerifiedDependencyContentsOrThrow(
{
workspaceId: flatApplication.workspaceId,
- applicationUniversalIdentifier,
+ applicationUniversalIdentifier: flatApplication.universalIdentifier,
declaredChecksums: flatApplication,
},
);
return {
packageJson: sanitizeApplicationPackageJson(packageJsonContent),
- yarnLock: yarnLockContent ?? EMPTY_YARN_LOCK_CONTENT,
+ yarnLock: yarnLockContent,
};
}
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/types/lambda-driver.type.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/types/lambda-driver.type.ts
index 6dce08ef1a0..ab979502dc1 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/types/lambda-driver.type.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/types/lambda-driver.type.ts
@@ -2,6 +2,7 @@ import { type LambdaClientConfig } from '@aws-sdk/client-lambda';
import { type LogicFunctionExecutionContext } from 'twenty-shared/logic-function';
import { type ApplicationDependencyFilesService } from 'src/engine/core-modules/application/application-package/application-dependency-files.service';
+import { type CoreEntityCacheService } from 'src/engine/core-entity-cache/services/core-entity-cache.service';
import { type CacheLockService } from 'src/engine/core-modules/cache-lock/cache-lock.service';
import { type CacheStorageService } from 'src/engine/core-modules/cache-storage/services/cache-storage.service';
import { type LogicFunctionResourceService } from 'src/engine/core-modules/logic-function/logic-function-resource/logic-function-resource.service';
@@ -45,6 +46,7 @@ export type LambdaDriverOptions = LambdaConnectionOptions & {
cacheLockService: CacheLockService;
cacheStorageService: CacheStorageService;
workspaceCacheService: WorkspaceCacheService;
+ coreEntityCacheService: CoreEntityCacheService;
};
export type LambdaConnectionOptions = LambdaClientConfig & {
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/__tests__/get-lambda-deps-layer-name.util.spec.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/__tests__/get-lambda-deps-layer-name.util.spec.ts
index f8df6b9f79c..f22ec2ecdbb 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/__tests__/get-lambda-deps-layer-name.util.spec.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/__tests__/get-lambda-deps-layer-name.util.spec.ts
@@ -1,46 +1,39 @@
import { type FlatApplication } from 'src/engine/core-modules/application/types/flat-application.type';
import { getLambdaDepsLayerName } from 'src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/get-lambda-deps-layer-name.util';
+const APPLICATION_UNIVERSAL_IDENTIFIER = '20202020-0000-0000-0000-00000000000a';
+
const buildFlatApplication = (
overrides: Partial<FlatApplication> = {},
): FlatApplication =>
({
+ universalIdentifier: APPLICATION_UNIVERSAL_IDENTIFIER,
+ packageJsonChecksum: 'def456',
yarnLockChecksum: 'abc123',
...overrides,
}) as FlatApplication;
describe('getLambdaDepsLayerName', () => {
- it('returns deps-<checksum> when yarnLockChecksum is set', () => {
- expect(
- getLambdaDepsLayerName({ flatApplication: buildFlatApplication() }),
- ).toBe('deps-abc123');
- });
-
- it('falls back to deps-default when yarnLockChecksum is undefined', () => {
+ it('names the layer of an application after its yarn.lock', () => {
expect(
getLambdaDepsLayerName({
- flatApplication: buildFlatApplication({ yarnLockChecksum: undefined }),
+ flatApplication: buildFlatApplication(),
+ namespace: 'ns123',
}),
- ).toBe('deps-default');
+ ).toBe(`depsv1-ns123-${APPLICATION_UNIVERSAL_IDENTIFIER}-abc123`);
});
- it('falls back to deps-default when yarnLockChecksum is null', () => {
- expect(
- getLambdaDepsLayerName({
- flatApplication: buildFlatApplication({
- yarnLockChecksum: null as unknown as string,
+ it('names the layer of an application without a yarn.lock after its package.json', () => {
+ for (const yarnLockChecksum of [null, undefined]) {
+ expect(
+ getLambdaDepsLayerName({
+ flatApplication: buildFlatApplication({
+ yarnLockChecksum: yarnLockChecksum as unknown as string,
+ }),
+ namespace: 'ns123',
}),
- }),
- ).toBe('deps-default');
- });
-
- it('inserts the namespace segment when provided', () => {
- expect(
- getLambdaDepsLayerName({
- flatApplication: buildFlatApplication(),
- namespace: 'ns123',
- }),
- ).toBe('deps-ns123-abc123');
+ ).toBe(`depsv1-ns123-${APPLICATION_UNIVERSAL_IDENTIFIER}-nolock-def456`);
+ }
});
it('omits the namespace segment when it is an empty string', () => {
@@ -49,6 +42,6 @@ describe('getLambdaDepsLayerName', () => {
flatApplication: buildFlatApplication(),
namespace: '',
}),
- ).toBe('deps-abc123');
+ ).toBe(`depsv1-${APPLICATION_UNIVERSAL_IDENTIFIER}-abc123`);
});
});
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/__tests__/get-lambda-workspace-custom-shared-deps-layer-name.util.spec.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/__tests__/get-lambda-workspace-custom-shared-deps-layer-name.util.spec.ts
new file mode 100644
index 00000000000..bbaa4d684a3
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/__tests__/get-lambda-workspace-custom-shared-deps-layer-name.util.spec.ts
@@ -0,0 +1,21 @@
+import { getLambdaWorkspaceCustomSharedDepsLayerName } from 'src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/get-lambda-workspace-custom-shared-deps-layer-name.util';
+
+describe('getLambdaWorkspaceCustomSharedDepsLayerName', () => {
+ it('names the layer shared by workspace custom applications after their yarn.lock', () => {
+ expect(
+ getLambdaWorkspaceCustomSharedDepsLayerName({
+ yarnLockChecksum: 'abc123',
+ namespace: 'ns123',
+ }),
+ ).toBe('depsv1-ns123-custom-abc123');
+ });
+
+ it('omits the namespace segment when it is an empty string', () => {
+ expect(
+ getLambdaWorkspaceCustomSharedDepsLayerName({
+ yarnLockChecksum: 'abc123',
+ namespace: '',
+ }),
+ ).toBe('depsv1-custom-abc123');
+ });
+});
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/get-lambda-deps-layer-name.util.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/get-lambda-deps-layer-name.util.ts
index 8a237be0277..d16be505924 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/get-lambda-deps-layer-name.util.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/get-lambda-deps-layer-name.util.ts
@@ -1,7 +1,9 @@
+import { isDefined } from 'twenty-shared/utils';
+
import { type FlatApplication } from 'src/engine/core-modules/application/types/flat-application.type';
import { buildLambdaResourceName } from 'src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/build-lambda-resource-name.util';
-const DEPS_LAYER_NAME_PREFIX = 'deps';
+export const DEPS_LAYER_NAME_PREFIX = 'depsv1';
export const getLambdaDepsLayerName = ({
flatApplication,
@@ -13,5 +15,7 @@ export const getLambdaDepsLayerName = ({
buildLambdaResourceName({
resourceNamePrefix: DEPS_LAYER_NAME_PREFIX,
namespace,
- checksum: flatApplication.yarnLockChecksum ?? 'default',
+ checksum: isDefined(flatApplication.yarnLockChecksum)
+ ? `${flatApplication.universalIdentifier}-${flatApplication.yarnLockChecksum}`
+ : `${flatApplication.universalIdentifier}-nolock-${flatApplication.packageJsonChecksum}`,
});
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/get-lambda-layer-name-from-arn.util.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/get-lambda-layer-name-from-arn.util.ts
new file mode 100644
index 00000000000..86777256102
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/get-lambda-layer-name-from-arn.util.ts
@@ -0,0 +1,6 @@
+const LAYER_NAME_ARN_SEGMENT_INDEX = 6;
+
+export const getLambdaLayerNameFromArn = (
+ layerVersionArn: string,
+): string | undefined =>
+ layerVersionArn.split(':')[LAYER_NAME_ARN_SEGMENT_INDEX];
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/get-lambda-workspace-custom-shared-deps-layer-name.util.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/get-lambda-workspace-custom-shared-deps-layer-name.util.ts
new file mode 100644
index 00000000000..98bc16dcaac
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/get-lambda-workspace-custom-shared-deps-layer-name.util.ts
@@ -0,0 +1,15 @@
+import { buildLambdaResourceName } from 'src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/build-lambda-resource-name.util';
+import { DEPS_LAYER_NAME_PREFIX } from 'src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/get-lambda-deps-layer-name.util';
+
+export const getLambdaWorkspaceCustomSharedDepsLayerName = ({
+ yarnLockChecksum,
+ namespace,
+}: {
+ yarnLockChecksum: string;
+ namespace?: string;
+}): string =>
+ buildLambdaResourceName({
+ resourceNamePrefix: DEPS_LAYER_NAME_PREFIX,
+ namespace,
+ checksum: `custom-${yarnLockChecksum}`,
+ });
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local.driver.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local.driver.ts
index 43ec77ed4c9..13a68545101 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local.driver.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local.driver.ts
@@ -107,7 +107,6 @@ export class LocalDriver implements LogicFunctionDriver {
async execute({
flatLogicFunction,
flatApplication,
- applicationUniversalIdentifier,
payload,
context,
env,
@@ -126,14 +125,8 @@ export class LocalDriver implements LogicFunctionDriver {
);
}
- await this.layerManager.ensureDepsLayer({
- flatApplication,
- applicationUniversalIdentifier,
- });
- await this.layerManager.ensureSdkLayer({
- flatApplication,
- applicationUniversalIdentifier,
- });
+ await this.layerManager.ensureDepsLayer(flatApplication);
+ await this.layerManager.ensureSdkLayer(flatApplication);
const startTime = Date.now();
const temporaryDirManager = new TemporaryDirManager();
@@ -144,7 +137,6 @@ export class LocalDriver implements LogicFunctionDriver {
await this.childProcessRunner.assembleNodeModules({
sourceTemporaryDir,
flatApplication,
- applicationUniversalIdentifier,
});
const inMemoryBuiltHandlerPath =
@@ -155,7 +147,8 @@ export class LocalDriver implements LogicFunctionDriver {
})
: await this.logicFunctionResourceService.copyBuiltCodeInMemory({
workspaceId: flatLogicFunction.workspaceId,
- applicationUniversalIdentifier,
+ applicationUniversalIdentifier:
+ flatApplication.universalIdentifier,
builtHandlerPath: flatLogicFunction.builtHandlerPath,
inMemoryDestinationPath: sourceTemporaryDir,
});
@@ -204,7 +197,6 @@ export class LocalDriver implements LogicFunctionDriver {
runnerPath,
executionDirectory: sourceTemporaryDir,
flatApplication,
- applicationUniversalIdentifier,
env: env ?? {},
payload,
context,
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local/services/local-child-process-runner.service.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local/services/local-child-process-runner.service.ts
index 89f82a3f6bd..6bbacf0b3a8 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local/services/local-child-process-runner.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local/services/local-child-process-runner.service.ts
@@ -16,16 +16,12 @@ export class LocalChildProcessRunnerService {
async assembleNodeModules({
sourceTemporaryDir,
flatApplication,
- applicationUniversalIdentifier,
}: {
sourceTemporaryDir: string;
flatApplication: FlatApplication;
- applicationUniversalIdentifier: string;
}): Promise<void> {
- const { depsLayerDirectory, sdkLayerDirectory } = this.getLayerDirectories({
- flatApplication,
- applicationUniversalIdentifier,
- });
+ const { depsLayerDirectory, sdkLayerDirectory } =
+ this.getLayerDirectories(flatApplication);
const depsNodeModules = join(depsLayerDirectory, 'node_modules');
const sdkNodeModules = join(sdkLayerDirectory, 'node_modules');
const execNodeModules = join(sourceTemporaryDir, 'node_modules');
@@ -145,7 +141,6 @@ export class LocalChildProcessRunnerService {
runnerPath: string;
executionDirectory: string;
flatApplication: FlatApplication;
- applicationUniversalIdentifier: string;
env: Record<string, string>;
payload: unknown;
context: LogicFunctionExecutionContext;
@@ -155,7 +150,6 @@ export class LocalChildProcessRunnerService {
runnerPath,
executionDirectory,
flatApplication,
- applicationUniversalIdentifier,
env,
payload,
context,
@@ -169,12 +163,7 @@ export class LocalChildProcessRunnerService {
const nodeFlags = buildLocalChildProcessNodeFlags({
readableDirectories: [
executionDirectory,
- ...Object.values(
- this.getLayerDirectories({
- flatApplication,
- applicationUniversalIdentifier,
- }),
- ),
+ ...Object.values(this.getLayerDirectories(flatApplication)),
],
writableDirectory: temporaryDirectory,
// Node 25+ denies network access under --permission unless --allow-net is passed.
@@ -268,18 +257,12 @@ export class LocalChildProcessRunnerService {
});
}
- private getLayerDirectories({
- flatApplication,
- applicationUniversalIdentifier,
- }: {
- flatApplication: FlatApplication;
- applicationUniversalIdentifier: string;
- }) {
+ private getLayerDirectories(flatApplication: FlatApplication) {
return {
depsLayerDirectory: getLocalDepsLayerPath(flatApplication),
sdkLayerDirectory: getLocalSdkLayerPath({
workspaceId: flatApplication.workspaceId,
- applicationUniversalIdentifier,
+ applicationUniversalIdentifier: flatApplication.universalIdentifier,
}),
};
}
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local/services/local-layer-manager.service.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local/services/local-layer-manager.service.ts
index c8d62147723..13572bd0eea 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local/services/local-layer-manager.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local/services/local-layer-manager.service.ts
@@ -17,11 +17,6 @@ import { type LogicFunctionResourceService } from 'src/engine/core-modules/logic
import { type SdkClientArchiveService } from 'src/engine/core-modules/sdk-client/sdk-client-archive.service';
import { type WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service';
-type LayerAppContext = {
- flatApplication: FlatApplication;
- applicationUniversalIdentifier: string;
-};
-
export class LocalLayerManagerService {
constructor(
private readonly cacheLockService: CacheLockService,
@@ -30,10 +25,7 @@ export class LocalLayerManagerService {
private readonly workspaceCacheService: WorkspaceCacheService,
) {}
- async ensureDepsLayer({
- flatApplication,
- applicationUniversalIdentifier,
- }: LayerAppContext): Promise<void> {
+ async ensureDepsLayer(flatApplication: FlatApplication): Promise<void> {
const depsLayerPath = getLocalDepsLayerPath(flatApplication);
const depsReadySentinelPath = join(
depsLayerPath,
@@ -55,7 +47,7 @@ export class LocalLayerManagerService {
await fs.rm(depsLayerPath, { recursive: true, force: true });
await this.logicFunctionResourceService.copyDependenciesInMemory({
- applicationUniversalIdentifier,
+ applicationUniversalIdentifier: flatApplication.universalIdentifier,
workspaceId: flatApplication.workspaceId,
inMemoryFolderPath: depsLayerPath,
});
@@ -71,13 +63,10 @@ export class LocalLayerManagerService {
);
}
- async ensureSdkLayer({
- flatApplication,
- applicationUniversalIdentifier,
- }: LayerAppContext): Promise<void> {
+ async ensureSdkLayer(flatApplication: FlatApplication): Promise<void> {
const sdkLayerPath = getLocalSdkLayerPath({
workspaceId: flatApplication.workspaceId,
- applicationUniversalIdentifier,
+ applicationUniversalIdentifier: flatApplication.universalIdentifier,
});
const sdkNodeModulesPath = join(sdkLayerPath, 'node_modules');
const sdkReadySentinelPath = join(sdkLayerPath, LAYER_BUILD_READY_SENTINEL);
@@ -89,7 +78,7 @@ export class LocalLayerManagerService {
return;
}
- const lockKey = `local-driver-sdk-layer:${flatApplication.workspaceId}:${applicationUniversalIdentifier}`;
+ const lockKey = `local-driver-sdk-layer:${flatApplication.workspaceId}:${flatApplication.universalIdentifier}`;
await this.cacheLockService.withLock(
async () => {
@@ -113,7 +102,7 @@ export class LocalLayerManagerService {
await this.sdkClientArchiveService.downloadAndExtractToPackage({
workspaceId: flatApplication.workspaceId,
applicationId: flatApplication.id,
- applicationUniversalIdentifier,
+ applicationUniversalIdentifier: flatApplication.universalIdentifier,
targetPackagePath: sdkPackagePath,
});
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local/services/local-prebuilt-bundle.service.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local/services/local-prebuilt-bundle.service.ts
index 483528af2d1..43df333f19e 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local/services/local-prebuilt-bundle.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local/services/local-prebuilt-bundle.service.ts
@@ -31,7 +31,7 @@ export class LocalPrebuiltBundleService {
async installPrebuiltBundle({
flatLogicFunction,
- applicationUniversalIdentifier,
+ flatApplication,
}: LogicFunctionInstallPrebuiltBundleParams): Promise<void> {
if (!isNonEmptyString(flatLogicFunction.checksum)) {
throw new LogicFunctionException(
@@ -57,7 +57,7 @@ export class LocalPrebuiltBundleService {
await this.logicFunctionResourceService.copyBuiltCodeInMemory({
workspaceId: flatLogicFunction.workspaceId,
- applicationUniversalIdentifier,
+ applicationUniversalIdentifier: flatApplication.universalIdentifier,
builtHandlerPath: flatLogicFunction.builtHandlerPath,
inMemoryDestinationPath: prebuiltDir,
});
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/interfaces/logic-function-driver.interface.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/interfaces/logic-function-driver.interface.ts
index a4da7c3e760..3c432c7b2b6 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/interfaces/logic-function-driver.interface.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/interfaces/logic-function-driver.interface.ts
@@ -23,7 +23,6 @@ export type LogicFunctionExecuteResult = {
export type LogicFunctionExecuteParams = {
flatLogicFunction: FlatLogicFunction;
flatApplication: FlatApplication;
- applicationUniversalIdentifier: string;
payload: object;
context: LogicFunctionExecutionContext;
env?: Record<string, string>;
@@ -34,7 +33,6 @@ export type LogicFunctionExecuteParams = {
export type LogicFunctionInstallPrebuiltBundleParams = {
flatLogicFunction: FlatLogicFunction;
flatApplication: FlatApplication;
- applicationUniversalIdentifier: string;
};
export type LogicFunctionTranspileParams = {
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/logic-function-driver.factory.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/logic-function-driver.factory.ts
index 8457adf8928..3e627bddfce 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/logic-function-driver.factory.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/logic-function-driver.factory.ts
@@ -6,6 +6,7 @@ import {
} from 'src/engine/core-modules/logic-function/logic-function-drivers/interfaces/logic-function-driver.interface';
import { ApplicationDependencyFilesService } from 'src/engine/core-modules/application/application-package/application-dependency-files.service';
+import { CoreEntityCacheService } from 'src/engine/core-entity-cache/services/core-entity-cache.service';
import { CacheLockService } from 'src/engine/core-modules/cache-lock/cache-lock.service';
import { InjectCacheStorage } from 'src/engine/core-modules/cache-storage/decorators/cache-storage.decorator';
import { CacheStorageService } from 'src/engine/core-modules/cache-storage/services/cache-storage.service';
@@ -32,6 +33,7 @@ export class LogicFunctionDriverFactory extends DriverFactoryBase<LogicFunctionD
private readonly sdkClientArchiveService: SdkClientArchiveService,
private readonly cacheLockService: CacheLockService,
private readonly workspaceCacheService: WorkspaceCacheService,
+ private readonly coreEntityCacheService: CoreEntityCacheService,
@InjectCacheStorage(CacheStorageNamespace.ModuleApplications)
private readonly cacheStorageService: CacheStorageService,
) {
@@ -71,6 +73,7 @@ export class LogicFunctionDriverFactory extends DriverFactoryBase<LogicFunctionD
cacheLockService: this.cacheLockService,
cacheStorageService: this.cacheStorageService,
workspaceCacheService: this.workspaceCacheService,
+ coreEntityCacheService: this.coreEntityCacheService,
...getLambdaConnectionOptions(this.twentyConfigService),
sdkClientArchiveService: this.sdkClientArchiveService,
});
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-executor/logic-function-executor.service.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-executor/logic-function-executor.service.ts
index a25abd29d64..e8cb9dc2943 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-executor/logic-function-executor.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-executor/logic-function-executor.service.ts
@@ -224,7 +224,6 @@ export class LogicFunctionExecutorService {
resultLogicFunction = await driver.execute({
flatLogicFunction,
flatApplication,
- applicationUniversalIdentifier: flatApplication.universalIdentifier,
payload,
context,
env: envVariables,
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-prebuilt-warm-up/logic-function-prebuilt-warm-up.service.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-prebuilt-warm-up/logic-function-prebuilt-warm-up.service.ts
index 568d683bcb8..6b617e3a0b2 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-prebuilt-warm-up/logic-function-prebuilt-warm-up.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-prebuilt-warm-up/logic-function-prebuilt-warm-up.service.ts
@@ -44,7 +44,6 @@ export class LogicFunctionPrebuiltWarmUpService {
await driver.installPrebuiltBundle({
flatLogicFunction,
flatApplication,
- applicationUniversalIdentifier: flatApplication.universalIdentifier,
});
} catch (error) {
if (
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function.module.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function.module.ts
index 1db71a0f494..4966411afb5 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function.module.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function.module.ts
@@ -1,5 +1,6 @@
import { type DynamicModule, Global, Module } from '@nestjs/common';
+import { CoreEntityCacheModule } from 'src/engine/core-entity-cache/core-entity-cache.module';
import { ApplicationPackageModule } from 'src/engine/core-modules/application/application-package/application-package.module';
import { CacheLockModule } from 'src/engine/core-modules/cache-lock/cache-lock.module';
import { LOGIC_FUNCTION_DRIVER_FACTORY_TOKEN } from 'src/engine/core-modules/logic-function/logic-function-drivers/constants/logic-function-driver-factory.token';
@@ -21,6 +22,7 @@ export class LogicFunctionModule {
TwentyConfigModule,
ApplicationPackageModule,
CacheLockModule,
+ CoreEntityCacheModule,
LogicFunctionResourceModule,
LogicFunctionTriggerModule,
LogicFunctionExecutorModule,
diff --git a/packages/twenty-server/test/integration/logic-function-lambda/utils/publish-dependency-layer-fixture.util.ts b/packages/twenty-server/test/integration/logic-function-lambda/utils/publish-dependency-layer-fixture.util.ts
index 48cb49de5f5..ebdd32271c0 100644
--- a/packages/twenty-server/test/integration/logic-function-lambda/utils/publish-dependency-layer-fixture.util.ts
+++ b/packages/twenty-server/test/integration/logic-function-lambda/utils/publish-dependency-layer-fixture.util.ts
@@ -5,8 +5,9 @@ import { join } from 'path';
import { DEPENDENCY_LAYER_FIXTURE } from 'test/integration/logic-function-lambda/constants/dependency-layer-fixture.constant';
import { getWorkspaceCustomFlatApplication } from 'test/integration/logic-function-lambda/utils/get-workspace-custom-flat-application.util';
+import { isDefined } from 'twenty-shared/utils';
-import { getLambdaDepsLayerName } from 'src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/get-lambda-deps-layer-name.util';
+import { getLambdaWorkspaceCustomSharedDepsLayerName } from 'src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/utils/get-lambda-workspace-custom-shared-deps-layer-name.util';
import { createZipFile } from 'src/engine/core-modules/logic-function/logic-function-drivers/utils/create-zip-file';
import { LogicFunctionRuntime } from 'src/engine/metadata-modules/logic-function/logic-function.entity';
@@ -19,7 +20,13 @@ export const publishDependencyLayerFixture = async ({
resourceNamespace: string;
workspaceId: string;
}): Promise<void> => {
- const flatApplication = await getWorkspaceCustomFlatApplication(workspaceId);
+ const { id, yarnLockChecksum } =
+ await getWorkspaceCustomFlatApplication(workspaceId);
+
+ if (!isDefined(yarnLockChecksum)) {
+ throw new Error(`Custom application ${id} has no yarn.lock checksum`);
+ }
+
const buildDirectory = await fs.mkdtemp(join(tmpdir(), 'deps-layer-'));
const packageDirectory = join(
buildDirectory,
@@ -45,8 +52,8 @@ export const publishDependencyLayerFixture = async ({
await createZipFile(join(buildDirectory, 'layer'), zipPath);
await lambdaClient.publishLayerVersion({
- LayerName: getLambdaDepsLayerName({
- flatApplication,
+ LayerName: getLambdaWorkspaceCustomSharedDepsLayerName({
+ yarnLockChecksum,
namespace: resourceNamespace,
}),
Content: { ZipFile: await fs.readFile(zipPath) },