The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Install pre-installed apps in a dedicated job after the workspace upgrade cursor is written (#23517)
martmullc4a79c50c39a
diff --git a/packages/twenty-server/src/engine/core-modules/application/pre-installed-apps/__tests__/pre-installed-apps.service.spec.ts b/packages/twenty-server/src/engine/core-modules/application/pre-installed-apps/__tests__/pre-installed-apps.service.spec.tsindex 9f554af3245..19cf443531c 100644--- a/packages/twenty-server/src/engine/core-modules/application/pre-installed-apps/__tests__/pre-installed-apps.service.spec.ts+++ b/packages/twenty-server/src/engine/core-modules/application/pre-installed-apps/__tests__/pre-installed-apps.service.spec.ts@@ -9,6 +9,8 @@ import {ApplicationException,ApplicationExceptionCode,} from 'src/engine/core-modules/application/application.exception';+import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';+import { getQueueToken } from 'src/engine/core-modules/message-queue/utils/get-queue-token.util';describe('PreInstalledAppsService', () => {let service: PreInstalledAppsService;@@ -18,11 +20,13 @@ describe('PreInstalledAppsService', () => {findOne: jest.Mock;};let workspaceIteratorService: { iterate: jest.Mock };+ let messageQueueService: { add: jest.Mock };beforeEach(async () => {applicationInstallService = { installApplication: jest.fn() };applicationRegistrationRepository = { find: jest.fn(), findOne: jest.fn() };workspaceIteratorService = { iterate: jest.fn() };+ messageQueueService = { add: jest.fn() };const module: TestingModule = await Test.createTestingModule({providers: [@@ -39,6 +43,10 @@ describe('PreInstalledAppsService', () => {provide: WorkspaceIteratorService,useValue: workspaceIteratorService,},+ {+ provide: getQueueToken(MessageQueue.workspaceQueue),+ useValue: messageQueueService,+ },],}).compile();diff --git a/packages/twenty-server/src/engine/core-modules/application/pre-installed-apps/jobs/install-pre-installed-apps.job-constants.ts b/packages/twenty-server/src/engine/core-modules/application/pre-installed-apps/jobs/install-pre-installed-apps.job-constants.tsnew file mode 100644index 00000000000..87e2b401111--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/application/pre-installed-apps/jobs/install-pre-installed-apps.job-constants.ts@@ -0,0 +1,5 @@+export const INSTALL_PRE_INSTALLED_APPS_JOB_NAME = 'InstallPreInstalledAppsJob';++export type InstallPreInstalledAppsJobData = {+ workspaceId: string;+};diff --git a/packages/twenty-server/src/engine/core-modules/application/pre-installed-apps/jobs/install-pre-installed-apps.job.ts b/packages/twenty-server/src/engine/core-modules/application/pre-installed-apps/jobs/install-pre-installed-apps.job.tsnew file mode 100644index 00000000000..1e3ff209c7f--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/application/pre-installed-apps/jobs/install-pre-installed-apps.job.ts@@ -0,0 +1,28 @@+import { Logger } from '@nestjs/common';++import {+ INSTALL_PRE_INSTALLED_APPS_JOB_NAME,+ type InstallPreInstalledAppsJobData,+} from 'src/engine/core-modules/application/pre-installed-apps/jobs/install-pre-installed-apps.job-constants';+import { PreInstalledAppsService } from 'src/engine/core-modules/application/pre-installed-apps/pre-installed-apps.service';+import { Process } from 'src/engine/core-modules/message-queue/decorators/process.decorator';+import { Processor } from 'src/engine/core-modules/message-queue/decorators/processor.decorator';+import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';++@Processor(MessageQueue.workspaceQueue)+export class InstallPreInstalledAppsJob {+ private readonly logger = new Logger(InstallPreInstalledAppsJob.name);++ constructor(+ private readonly preInstalledAppsService: PreInstalledAppsService,+ ) {}++ @Process(INSTALL_PRE_INSTALLED_APPS_JOB_NAME)+ async handle({ workspaceId }: InstallPreInstalledAppsJobData): Promise<void> {+ this.logger.log(+ `Installing pre-installed apps on workspace ${workspaceId}`,+ );++ await this.preInstalledAppsService.installOnWorkspace(workspaceId);+ }+}diff --git a/packages/twenty-server/src/engine/core-modules/application/pre-installed-apps/pre-installed-apps.service.ts b/packages/twenty-server/src/engine/core-modules/application/pre-installed-apps/pre-installed-apps.service.tsindex fdd7b5d14e2..9242a1d4097 100644--- a/packages/twenty-server/src/engine/core-modules/application/pre-installed-apps/pre-installed-apps.service.ts+++ b/packages/twenty-server/src/engine/core-modules/application/pre-installed-apps/pre-installed-apps.service.ts@@ -10,6 +10,13 @@ import {ApplicationException,ApplicationExceptionCode,} from 'src/engine/core-modules/application/application.exception';+import {+ INSTALL_PRE_INSTALLED_APPS_JOB_NAME,+ type InstallPreInstalledAppsJobData,+} from 'src/engine/core-modules/application/pre-installed-apps/jobs/install-pre-installed-apps.job-constants';+import { InjectMessageQueue } from 'src/engine/core-modules/message-queue/decorators/message-queue.decorator';+import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';+import { MessageQueueService } from 'src/engine/core-modules/message-queue/services/message-queue.service';@Injectable()export class PreInstalledAppsService {@@ -20,8 +27,18 @@ export class PreInstalledAppsService {@InjectRepository(ApplicationRegistrationEntity)private readonly applicationRegistrationRepository: Repository<ApplicationRegistrationEntity>,private readonly workspaceIteratorService: WorkspaceIteratorService,+ @InjectMessageQueue(MessageQueue.workspaceQueue)+ private readonly messageQueueService: MessageQueueService,) {}+ async enqueueInstallOnWorkspace(workspaceId: string): Promise<void> {+ await this.messageQueueService.add<InstallPreInstalledAppsJobData>(+ INSTALL_PRE_INSTALLED_APPS_JOB_NAME,+ { workspaceId },+ { id: `${INSTALL_PRE_INSTALLED_APPS_JOB_NAME}-${workspaceId}` },+ );+ }+// Per-app failures are logged but never block the other installs —// `ApplicationInstallService` holds a per-app cache lock so parallel// installs are safe.diff --git a/packages/twenty-server/src/engine/core-modules/message-queue/jobs.module.ts b/packages/twenty-server/src/engine/core-modules/message-queue/jobs.module.tsindex 9e657a94e27..2d7e5c1085d 100644--- a/packages/twenty-server/src/engine/core-modules/message-queue/jobs.module.ts+++ b/packages/twenty-server/src/engine/core-modules/message-queue/jobs.module.ts@@ -16,6 +16,8 @@ import { ApplicationInstallModule } from 'src/engine/core-modules/application/apimport { ApplicationRegistrationModule } from 'src/engine/core-modules/application/application-registration/application-registration.module';import { ApplicationUpgradeModule } from 'src/engine/core-modules/application/application-upgrade/application-upgrade.module';import { UpgradeApplicationsJob } from 'src/engine/core-modules/application/jobs/upgrade-applications.job';+import { InstallPreInstalledAppsJob } from 'src/engine/core-modules/application/pre-installed-apps/jobs/install-pre-installed-apps.job';+import { PreInstalledAppsModule } from 'src/engine/core-modules/application/pre-installed-apps/pre-installed-apps.module';import { InstallOnboardingAppsJob } from 'src/engine/core-modules/onboarding/jobs/install-onboarding-apps.job';import { OnboardingModule } from 'src/engine/core-modules/onboarding/onboarding.module';import { EmailSenderJob } from 'src/engine/core-modules/email/email-sender.job';@@ -95,6 +97,7 @@ import { WorkflowModule } from 'src/modules/workflow/workflow.module';ApplicationInstallModule,ApplicationRegistrationModule,ApplicationUpgradeModule,+ PreInstalledAppsModule,OnboardingModule,BillingReminderModule,],@@ -113,6 +116,7 @@ import { WorkflowModule } from 'src/modules/workflow/workflow.module';GenerateSdkClientJob,UpgradeApplicationsJob,InstallOnboardingAppsJob,+ InstallPreInstalledAppsJob,],})export class JobsModule {diff --git a/packages/twenty-server/src/engine/core-modules/workspace/services/workspace.service.ts b/packages/twenty-server/src/engine/core-modules/workspace/services/workspace.service.tsindex fcfc7864ed1..05d175db044 100644--- a/packages/twenty-server/src/engine/core-modules/workspace/services/workspace.service.ts+++ b/packages/twenty-server/src/engine/core-modules/workspace/services/workspace.service.ts@@ -411,6 +411,8 @@ export class WorkspaceService {await this.activateAndInitializeUpgradeState({workspaceId: workspace.id,});++ await this.enqueuePreInstalledAppsInstallation(workspace.id);} catch (error) {await this.workspaceRepository.update(workspace.id, {activationStatus: WorkspaceActivationStatus.PENDING_CREATION,@@ -952,12 +954,16 @@ export class WorkspaceService {);this.exceptionHandlerService.captureExceptions([error as Error]);}+ }+ private async enqueuePreInstalledAppsInstallation(+ workspaceId: string,+ ): Promise<void> {try {- await this.preInstalledAppsService.installOnWorkspace(workspaceId);+ await this.preInstalledAppsService.enqueueInstallOnWorkspace(workspaceId);} catch (error) {this.logger.error(- `Non-critical: failed to install pre-installed apps for workspace ${workspaceId}`,+ `Non-critical: failed to enqueue pre-installed apps installation for workspace ${workspaceId}`,error,);this.exceptionHandlerService.captureExceptions([error as Error]);