The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Report workspace command failures through the CLI exit status (#26263)
Félix Malfait9b0a56914d57
diff --git a/packages/twenty-server/src/database/commands/command-runners/__tests__/workspace.command-runner.spec.ts b/packages/twenty-server/src/database/commands/command-runners/__tests__/workspace.command-runner.spec.tsnew file mode 100644index 00000000000..ce8a38c5ac5--- /dev/null+++ b/packages/twenty-server/src/database/commands/command-runners/__tests__/workspace.command-runner.spec.ts@@ -0,0 +1,81 @@+import { Logger } from '@nestjs/common';+import { WorkspaceActivationStatus } from 'twenty-shared/workspace';++import { WorkspaceCommandRunner } from 'src/database/commands/command-runners/workspace.command-runner';+import {+ type WorkspaceIteratorReport,+ type WorkspaceIteratorService,+} from 'src/database/commands/command-runners/workspace-iterator.service';++class TestWorkspaceCommand extends WorkspaceCommandRunner {+ runOnWorkspace = jest.fn().mockResolvedValue(undefined);+}++describe('WorkspaceCommandRunner failure reporting', () => {+ beforeEach(() => {+ jest.spyOn(Logger.prototype, 'log').mockImplementation(() => undefined);+ jest.spyOn(Logger.prototype, 'warn').mockImplementation(() => undefined);+ jest.spyOn(Logger.prototype, 'error').mockImplementation(() => undefined);+ });++ afterEach(() => {+ jest.restoreAllMocks();+ });++ const runWithReport = (report: WorkspaceIteratorReport) => {+ const iterator = {+ listenToShutdownSignals: jest.fn(),+ iterate: jest.fn().mockResolvedValue(report),+ } satisfies Pick<+ WorkspaceIteratorService,+ 'listenToShutdownSignals' | 'iterate'+ >;++ return new TestWorkspaceCommand(iterator, [+ WorkspaceActivationStatus.ACTIVE,+ ]).run([], {});+ };++ it('succeeds when every workspace completed', async () => {+ await expect(+ runWithReport({+ success: [{ workspaceId: 'successful-workspace' }],+ fail: [],+ interrupted: false,+ }),+ ).resolves.toBeUndefined();+ });++ it.each([false, true])(+ 'rejects reported failures even when interrupted=%s',+ async (interrupted) => {+ await expect(+ runWithReport({+ success: [{ workspaceId: 'successful-workspace' }],+ fail: [+ {+ workspaceId: 'busy-workspace',+ error: new Error('Agent streams are still active'),+ },+ {+ workspaceId: 'invalid-workspace',+ error: new Error('History verification failed'),+ },+ ],+ interrupted,+ }),+ ).rejects.toThrow('Command failed for 2 workspace(s)');+ if (interrupted) {+ expect(Logger.prototype.warn).toHaveBeenCalledWith(+ expect.stringContaining('Rerun it to process the remaining ones'),+ );+ }+ },+ );++ it('preserves graceful interruption when no workspace failed', async () => {+ await expect(+ runWithReport({ success: [], fail: [], interrupted: true }),+ ).resolves.toBeUndefined();+ });+});diff --git a/packages/twenty-server/src/database/commands/command-runners/workspace.command-runner.ts b/packages/twenty-server/src/database/commands/command-runners/workspace.command-runner.tsindex b579ca3672d..17d8fc598bc 100644--- a/packages/twenty-server/src/database/commands/command-runners/workspace.command-runner.ts+++ b/packages/twenty-server/src/database/commands/command-runners/workspace.command-runner.ts@@ -1,4 +1,5 @@import chalk from 'chalk';+import { isNonEmptyArray } from 'twenty-shared/utils';import { CommandRunner, Option } from 'nest-commander';import { WorkspaceActivationStatus } from 'twenty-shared/workspace';import { type DataSource } from 'typeorm';@@ -30,7 +31,10 @@ export abstract class WorkspaceCommandRunner<protected logger: CommandLogger;constructor(- protected readonly workspaceIteratorService: WorkspaceIteratorService,+ protected readonly workspaceIteratorService: Pick<+ WorkspaceIteratorService,+ 'listenToShutdownSignals' | 'iterate'+ >,protected readonly activationStatuses: WorkspaceActivationStatus[],) {super();@@ -139,11 +143,17 @@ export abstract class WorkspaceCommandRunner<'Command interrupted before processing every workspace. Rerun it to process the remaining ones.',),);+ }- return;+ if (isNonEmptyArray(report.fail)) {+ throw new Error(+ `Command failed for ${report.fail.length} workspace(s). See the workspace errors above.`,+ );}- this.logger.log(chalk.blue('Command completed!'));+ if (!report.interrupted) {+ this.logger.log(chalk.blue('Command completed!'));+ }} catch (error) {this.logger.error(chalk.red(`Command failed`));throw error;