Improve workspace delete handler

This commit is contained in:
Hakan Shehu
2025-01-14 18:53:02 +01:00
parent dbffc94527
commit 85d38e943c
2 changed files with 32 additions and 7 deletions

View File

@@ -77,13 +77,16 @@ class DatabaseService {
return this.workspaceDatabases;
}
public async deleteWorkspaceDatabase(userId: string): Promise<void> {
public async removeWorkspaceDatabase(userId: string): Promise<void> {
this.debug(`Deleting workspace database for user: ${userId}`);
await this.waitForInit();
if (this.workspaceDatabases.has(userId)) {
this.workspaceDatabases.delete(userId);
const workspaceDatabase = this.workspaceDatabases.get(userId);
if (workspaceDatabase) {
workspaceDatabase.destroy();
}
this.workspaceDatabases.delete(userId);
}
private async waitForInit(): Promise<void> {

View File

@@ -340,12 +340,34 @@ class AccountService {
this.debug(`Deleted workspace ${userId}`);
}
await databaseService.deleteWorkspaceDatabase(userId);
await databaseService.removeWorkspaceDatabase(userId);
const workspaceDir = getWorkspaceDirectoryPath(userId);
if (fs.existsSync(workspaceDir)) {
fs.rmSync(workspaceDir, { recursive: true });
let deleted = false;
for (let attempt = 1; attempt <= 5; attempt++) {
try {
if (fs.existsSync(workspaceDir)) {
fs.rmSync(workspaceDir, { recursive: true, force: true });
deleted = true;
break;
}
} catch (error) {
this.debug(
`Failed to delete workspace directory ${workspaceDir} on attempt ${attempt}`,
error
);
if (attempt === 5) {
break;
}
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
if (!deleted) {
this.debug(`Failed to delete workspace directory ${workspaceDir}`);
} else {
this.debug(`Deleted workspace directory ${workspaceDir}`);
}
this.debug(`Deleted workspace directory ${workspaceDir}`);
eventBus.publish({
type: 'workspace_deleted',