Skip to content

The open alternative to Salesforce, designed for AI.

Restore the install-apps onboarding step when enqueuing its job fails (#23759)

Raphaël Bosi531361c9a73b
diff --git a/packages/twenty-server/src/engine/core-modules/onboarding/onboarding.exception.ts b/packages/twenty-server/src/engine/core-modules/onboarding/onboarding.exception.ts
new file mode 100644
index 00000000000..23d6f98584f
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/onboarding/onboarding.exception.ts
@@ -0,0 +1,33 @@
+import { type MessageDescriptor } from '@lingui/core';
+import { msg } from '@lingui/core/macro';
+import { assertUnreachable } from 'twenty-shared/utils';
+
+import { CustomException } from 'src/utils/custom-exception';
+
+export enum OnboardingExceptionCode {
+ INSTALL_APPS_JOB_ENQUEUE_FAILED = 'INSTALL_APPS_JOB_ENQUEUE_FAILED',
+}
+
+const getOnboardingExceptionUserFriendlyMessage = (
+ code: OnboardingExceptionCode,
+) => {
+ switch (code) {
+ case OnboardingExceptionCode.INSTALL_APPS_JOB_ENQUEUE_FAILED:
+ return msg`Something went wrong while starting the app installation. Please try again.`;
+ default:
+ assertUnreachable(code);
+ }
+};
+
+export class OnboardingException extends CustomException<OnboardingExceptionCode> {
+ constructor(
+ message: string,
+ code: OnboardingExceptionCode,
+ { userFriendlyMessage }: { userFriendlyMessage?: MessageDescriptor } = {},
+ ) {
+ super(message, code, {
+ userFriendlyMessage:
+ userFriendlyMessage ?? getOnboardingExceptionUserFriendlyMessage(code),
+ });
+ }
+}
diff --git a/packages/twenty-server/src/engine/core-modules/onboarding/onboarding.service.spec.ts b/packages/twenty-server/src/engine/core-modules/onboarding/onboarding.service.spec.ts
index 48397f8a082..c2ecf80e2e3 100644
--- a/packages/twenty-server/src/engine/core-modules/onboarding/onboarding.service.spec.ts
+++ b/packages/twenty-server/src/engine/core-modules/onboarding/onboarding.service.spec.ts
@@ -12,6 +12,7 @@ import { getQueueToken } from 'src/engine/core-modules/message-queue/utils/get-q
import { ONBOARDING_INSTALLABLE_APP_UNIVERSAL_IDENTIFIERS } from 'src/engine/core-modules/onboarding/constants/onboarding-installable-app-universal-identifiers';
import { OnboardingStatus } from 'src/engine/core-modules/onboarding/enums/onboarding-status.enum';
import { INSTALL_ONBOARDING_APPS_JOB_NAME } from 'src/engine/core-modules/onboarding/jobs/install-onboarding-apps.job-constants';
+import { OnboardingException } from 'src/engine/core-modules/onboarding/onboarding.exception';
import {
OnboardingService,
OnboardingStepKeys,
@@ -378,6 +379,89 @@ describe('OnboardingService', () => {
expect(userVarsService.delete).toHaveBeenCalled();
expect(messageQueueService.add).not.toHaveBeenCalled();
});
+
+ it('should restore the pending step and throw when enqueuing fails', async () => {
+ jest.spyOn(userVarsService, 'delete').mockResolvedValue(1);
+ jest
+ .spyOn(messageQueueService, 'add')
+ .mockRejectedValue(new Error('queue failure'));
+
+ await expect(
+ service.triggerInstallAppsOnboardingStep({
+ userId,
+ workspaceId,
+ universalIdentifiers: [callRecorderId],
+ }),
+ ).rejects.toThrow(OnboardingException);
+
+ expect(userVarsService.set).toHaveBeenCalledWith(
+ {
+ userId,
+ workspaceId,
+ key: OnboardingStepKeys.ONBOARDING_INSTALL_APPS_PENDING,
+ value: true,
+ },
+ undefined,
+ );
+ });
+
+ it('should let the user retry the step after a failed enqueue', async () => {
+ let isInstallAppsStepPending = true;
+
+ jest.spyOn(userVarsService, 'delete').mockImplementation(async () => {
+ const affectedRows = isInstallAppsStepPending ? 1 : 0;
+
+ isInstallAppsStepPending = false;
+
+ return affectedRows;
+ });
+ jest.spyOn(userVarsService, 'set').mockImplementation(async () => {
+ isInstallAppsStepPending = true;
+ });
+ jest
+ .spyOn(messageQueueService, 'add')
+ .mockRejectedValueOnce(new Error('queue failure'))
+ .mockResolvedValueOnce(undefined);
+
+ await expect(
+ service.triggerInstallAppsOnboardingStep({
+ userId,
+ workspaceId,
+ universalIdentifiers: [callRecorderId],
+ }),
+ ).rejects.toThrow(OnboardingException);
+
+ await service.triggerInstallAppsOnboardingStep({
+ userId,
+ workspaceId,
+ universalIdentifiers: [callRecorderId],
+ });
+
+ expect(messageQueueService.add).toHaveBeenCalledTimes(2);
+ expect(messageQueueService.add).toHaveBeenLastCalledWith(
+ INSTALL_ONBOARDING_APPS_JOB_NAME,
+ { workspaceId, universalIdentifiers: [callRecorderId] },
+ { id: `${INSTALL_ONBOARDING_APPS_JOB_NAME}-${workspaceId}` },
+ );
+ });
+
+ it('should still throw when restoring the pending step fails', async () => {
+ jest.spyOn(userVarsService, 'delete').mockResolvedValue(1);
+ jest
+ .spyOn(messageQueueService, 'add')
+ .mockRejectedValue(new Error('queue failure'));
+ jest
+ .spyOn(userVarsService, 'set')
+ .mockRejectedValue(new Error('database failure'));
+
+ await expect(
+ service.triggerInstallAppsOnboardingStep({
+ userId,
+ workspaceId,
+ universalIdentifiers: [callRecorderId],
+ }),
+ ).rejects.toThrow(OnboardingException);
+ });
});
describe('creditInstallAppsReward', () => {
diff --git a/packages/twenty-server/src/engine/core-modules/onboarding/onboarding.service.ts b/packages/twenty-server/src/engine/core-modules/onboarding/onboarding.service.ts
index 1d20dbacaec..dcc0f31008e 100644
--- a/packages/twenty-server/src/engine/core-modules/onboarding/onboarding.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/onboarding/onboarding.service.ts
@@ -17,6 +17,10 @@ import {
INSTALL_ONBOARDING_APPS_JOB_NAME,
type InstallOnboardingAppsJobData,
} from 'src/engine/core-modules/onboarding/jobs/install-onboarding-apps.job-constants';
+import {
+ OnboardingException,
+ OnboardingExceptionCode,
+} from 'src/engine/core-modules/onboarding/onboarding.exception';
import { readBookCallStepMinEmployeeCount } from 'src/engine/core-modules/onboarding/utils/read-book-call-step-min-employee-count.util';
import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
import { UserVarsService } from 'src/engine/core-modules/user/user-vars/services/user-vars.service';
@@ -336,11 +340,45 @@ export class OnboardingService {
return;
}
- await this.messageQueueService.add<InstallOnboardingAppsJobData>(
- INSTALL_ONBOARDING_APPS_JOB_NAME,
- { workspaceId, universalIdentifiers: installableUniversalIdentifiers },
- { id: `${INSTALL_ONBOARDING_APPS_JOB_NAME}-${workspaceId}` },
- );
+ try {
+ await this.messageQueueService.add<InstallOnboardingAppsJobData>(
+ INSTALL_ONBOARDING_APPS_JOB_NAME,
+ { workspaceId, universalIdentifiers: installableUniversalIdentifiers },
+ { id: `${INSTALL_ONBOARDING_APPS_JOB_NAME}-${workspaceId}` },
+ );
+ } catch (error) {
+ const enqueueFailureMessage = `Failed to enqueue the install onboarding apps job for workspace ${workspaceId}`;
+
+ this.logger.error(enqueueFailureMessage, error);
+
+ await this.releaseInstallAppsOnboardingStepClaim({ userId, workspaceId });
+
+ throw new OnboardingException(
+ enqueueFailureMessage,
+ OnboardingExceptionCode.INSTALL_APPS_JOB_ENQUEUE_FAILED,
+ );
+ }
+ }
+
+ private async releaseInstallAppsOnboardingStepClaim({
+ userId,
+ workspaceId,
+ }: {
+ userId: string;
+ workspaceId: string;
+ }) {
+ try {
+ await this.setOnboardingInstallAppsPending({
+ userId,
+ workspaceId,
+ value: true,
+ });
+ } catch (error) {
+ this.logger.error(
+ `Failed to restore the pending install-apps onboarding step for workspace ${workspaceId}`,
+ error,
+ );
+ }
}
private async claimInstallAppsOnboardingStep({