Provide a wrapper to execute command on workspace with easier devXP (#10391)
Proposal: - Add a method in ActiveWorkspaceCommand to loop over workspace safely (add counter, add try / catch, provide datasource with fresh cache, destroy datasource => as we do always do it) Also in this PR: - make sure we clear all dataSources (and not only the one on metadata version in RAM)
This commit is contained in:
@@ -8,6 +8,8 @@ import {
|
||||
BaseCommandRunner,
|
||||
} from 'src/database/commands/base.command';
|
||||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { WorkspaceDataSource } from 'src/engine/twenty-orm/datasource/workspace.datasource';
|
||||
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
|
||||
export type ActiveWorkspacesCommandOptions = BaseCommandOptions & {
|
||||
workspaceId?: string;
|
||||
startFromWorkspaceId?: string;
|
||||
@@ -19,7 +21,10 @@ export abstract class ActiveWorkspacesCommandRunner extends BaseCommandRunner {
|
||||
private startFromWorkspaceId: string | undefined;
|
||||
private workspaceCountLimit: number | undefined;
|
||||
|
||||
constructor(protected readonly workspaceRepository: Repository<Workspace>) {
|
||||
constructor(
|
||||
protected readonly workspaceRepository: Repository<Workspace>,
|
||||
protected readonly twentyORMGlobalManager: TwentyORMGlobalManager,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
@@ -122,6 +127,54 @@ export abstract class ActiveWorkspacesCommandRunner extends BaseCommandRunner {
|
||||
);
|
||||
}
|
||||
|
||||
protected async processEachWorkspaceWithWorkspaceDataSource(
|
||||
workspaceIds: string[],
|
||||
callback: ({
|
||||
workspaceId,
|
||||
index,
|
||||
total,
|
||||
dataSource,
|
||||
}: {
|
||||
workspaceId: string;
|
||||
index: number;
|
||||
total: number;
|
||||
dataSource: WorkspaceDataSource;
|
||||
}) => Promise<void>,
|
||||
): Promise<void> {
|
||||
this.logger.log(
|
||||
chalk.green(`Running command on ${workspaceIds.length} workspaces`),
|
||||
);
|
||||
for (const [index, workspaceId] of workspaceIds.entries()) {
|
||||
this.logger.log(
|
||||
chalk.green(
|
||||
`Processing workspace ${workspaceId} (${index + 1}/${
|
||||
workspaceIds.length
|
||||
})`,
|
||||
),
|
||||
);
|
||||
|
||||
const dataSource =
|
||||
await this.twentyORMGlobalManager.getDataSourceForWorkspace(
|
||||
workspaceId,
|
||||
false,
|
||||
);
|
||||
|
||||
try {
|
||||
await callback({
|
||||
workspaceId,
|
||||
index,
|
||||
total: workspaceIds.length,
|
||||
dataSource,
|
||||
});
|
||||
} catch (error) {
|
||||
this.logger.error(`Error in workspace ${workspaceId}: ${error}`);
|
||||
}
|
||||
await this.twentyORMGlobalManager.destroyDataSourceForWorkspace(
|
||||
workspaceId,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract executeActiveWorkspacesCommand(
|
||||
passedParams: string[],
|
||||
options: BaseCommandOptions,
|
||||
|
||||
Reference in New Issue
Block a user