diff --git a/engine/engine.ts b/engine/engine.ts index 08348a58..22b808cb 100644 --- a/engine/engine.ts +++ b/engine/engine.ts @@ -57,7 +57,7 @@ export class Engine { } for (let runnerIndex in this.environment.runners) { - (await this.getRunner(this.environment.runners[runnerIndex])).destroy(this.playbook); + await (await this.getRunner(this.environment.runners[runnerIndex])).destroy(this.playbook); } } diff --git a/engine/runner.ts b/engine/runner.ts index 2d54fa8b..2e49f325 100644 --- a/engine/runner.ts +++ b/engine/runner.ts @@ -97,14 +97,17 @@ export abstract class Runner { } } - destroy(playbook: Playbook): void { + async destroy(playbook: Playbook): Promise { } - protected createFolder(path: string, deleteFolerIfExist: boolean) { + protected createFolder(path: string, deleteFolderIfExist: boolean) { if(fs.existsSync(path)) { - if(deleteFolerIfExist) { - rimraf.sync(path); - fs.mkdirSync(path, { recursive: true }); + if(deleteFolderIfExist) { + try { + rimraf.sync(path); + } catch(e) { + console.log("Error deleting foler " + path, e); + } } else return path; } fs.mkdirSync(path, { recursive: true }); diff --git a/engine/wikiRunner.ts b/engine/wikiRunner.ts index dc51c044..44c40bf6 100644 --- a/engine/wikiRunner.ts +++ b/engine/wikiRunner.ts @@ -13,7 +13,7 @@ export abstract class WikiRunner extends Runner { this.outputPathTutorial = this.createFolder(path.join(outputDirectory, playbook.name), true); } - destroy(playbook: Playbook): void { + async destroy(playbook: Playbook): Promise { } diff --git a/runners/console/index.ts b/runners/console/index.ts index dba5be65..d72f9b6b 100644 --- a/runners/console/index.ts +++ b/runners/console/index.ts @@ -36,8 +36,8 @@ export class Console extends Runner { this.env = process.env; } - destroy(playbook: Playbook): void { - this.cleanUp(); + async destroy(playbook: Playbook): Promise { + await this.cleanUp(); } runInstallDevonfwIde(runCommand: RunCommand): RunResult { @@ -361,7 +361,7 @@ export class Console extends Runner { assert.directoryExits(path.join(this.getWorkingDirectory(), "devonfw", "software", tool)); } } catch(error) { - this.cleanUp(); + await this.cleanUp(); throw error; } } @@ -379,7 +379,7 @@ export class Console extends Runner { .fileExits(path.join(this.getWorkingDirectory(), "devonfw", "software", "cobigen-cli", "cobigen.jar")) .fileExits(path.join(this.getWorkingDirectory(), "devonfw", "software", "cobigen-cli", "cobigen")); } catch(error) { - this.cleanUp(); + await this.cleanUp(); throw error; } } @@ -393,7 +393,7 @@ export class Console extends Runner { .directoryExits(path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0], "core", "target")) .directoryExits(path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0], "server", "target")); } catch(error) { - this.cleanUp(); + await this.cleanUp(); throw error; } } @@ -405,7 +405,7 @@ export class Console extends Runner { .noException(result) .fileExits(path.join(this.getWorkingDirectory(), "devonfw", "workspaces", "main", runCommand.command.parameters[0])); } catch(error) { - this.cleanUp(); + await this.cleanUp(); throw error; } } @@ -423,7 +423,7 @@ export class Console extends Runner { .directoryExits(path.join(workspaceDir, runCommand.command.parameters[0], "server", "src", "main", "java")) .fileExits(path.join(workspaceDir, runCommand.command.parameters[0], "core", "src", "main", "java", "com", "example", "application", runCommand.command.parameters[0], "SpringBootApp.java")); } catch(error) { - this.cleanUp(); + await this.cleanUp(); throw error; } } @@ -435,7 +435,7 @@ export class Console extends Runner { .noException(result) .fileExits(path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0])); } catch(error) { - this.cleanUp(); + await this.cleanUp(); throw error; } } @@ -456,7 +456,7 @@ export class Console extends Runner { .fileExits(filepath) .fileContains(filepath, content); } catch(error) { - this.cleanUp(); + await this.cleanUp(); throw error; } } @@ -475,18 +475,18 @@ export class Console extends Runner { await this.sleep(runCommand.command.parameters[1].startupTime); if(!runCommand.command.parameters[1].port) { - this.killAsyncProcesses(); + await this.killAsyncProcesses(); throw new Error("Missing arguments for command dockerCompose. You have to specify a port and a path for the server. For further information read the function documentation."); } else { let isReachable = await assert.serverIsReachable(runCommand.command.parameters[1].port, runCommand.command.parameters[1].path); if(!isReachable) { - this.killAsyncProcesses(); + await this.killAsyncProcesses(); throw new Error("The server has not become reachable in " + startupTimeInSeconds + " seconds: " + "http://localhost:" + runCommand.command.parameters[1].port + "/" + runCommand.command.parameters[1].path); } } } } catch(error) { - this.cleanUp(); + await this.cleanUp(); throw error; } } @@ -505,18 +505,18 @@ export class Console extends Runner { await this.sleep(runCommand.command.parameters[1].startupTime); if(!runCommand.command.parameters[1].port || !runCommand.command.parameters[1].path) { - this.killAsyncProcesses(); + await this.killAsyncProcesses(); throw new Error("Missing arguments for command runServerJava. You have to specify a port and a path for the server. For further information read the function documentation."); } else { let isReachable = await assert.serverIsReachable(runCommand.command.parameters[1].port, runCommand.command.parameters[1].path); if(!isReachable) { - this.killAsyncProcesses(); + await this.killAsyncProcesses(); throw new Error("The server has not become reachable in " + startupTimeInSeconds + " seconds: " + "http://localhost:" + runCommand.command.parameters[1].port + "/" + runCommand.command.parameters[1].path) } } } } catch(error) { - this.cleanUp(); + await this.cleanUp(); throw error; } } @@ -534,7 +534,7 @@ export class Console extends Runner { .directoryNotEmpty(path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0], repoName)) .repositoryIsClean(directorypath); } catch(error) { - this.cleanUp(); + await this.cleanUp(); throw error; } } @@ -552,7 +552,7 @@ export class Console extends Runner { .directoryNotEmpty(path.join(projectDir, "node_modules")); } } catch(error) { - this.cleanUp(); + await this.cleanUp(); throw error; } } @@ -570,7 +570,7 @@ export class Console extends Runner { .directoryNotEmpty(directory) .fileExits(path.join(directory, runCommand.command.parameters[1])); } catch(error) { - this.cleanUp(); + await this.cleanUp(); throw error; } } @@ -589,18 +589,18 @@ export class Console extends Runner { await this.sleep(runCommand.command.parameters[1].startupTime); if(!runCommand.command.parameters[1].port) { - this.killAsyncProcesses(); + await this.killAsyncProcesses(); throw new Error("Missing arguments for command runClientNg. You have to specify a port for the server. For further information read the function documentation."); } else { let isReachable = await assert.serverIsReachable(runCommand.command.parameters[1].port, runCommand.command.parameters[1].path); if(!isReachable) { - this.killAsyncProcesses(); + await this.killAsyncProcesses(); throw new Error("The server has not become reachable in " + startupTimeInSeconds + " seconds: " + "http://localhost:" + runCommand.command.parameters[1].port + "/" + runCommand.command.parameters[1].path) } } } } catch(error) { - this.cleanUp(); + await this.cleanUp(); throw error; } } @@ -625,7 +625,7 @@ export class Console extends Runner { .directoryNotEmpty(path.join(projectPath, outputpath)); } catch(error) { - this.cleanUp(); + await this.cleanUp(); throw error; } } @@ -638,7 +638,7 @@ export class Console extends Runner { .noException(result) .directoryExits(folderPath); } catch(error) { - this.cleanUp(); + await this.cleanUp(); throw error; } } @@ -653,7 +653,7 @@ export class Console extends Runner { .directoryNotEmpty(templatesDir); } catch(error) { - this.cleanUp(); + await this.cleanUp(); throw error; } } @@ -667,7 +667,7 @@ export class Console extends Runner { .directoryExits(projectDir) .directoryNotEmpty(projectDir); } catch(error) { - this.cleanUp(); + await this.cleanUp(); throw error; } } @@ -690,46 +690,51 @@ export class Console extends Runner { return new Promise(resolve => setTimeout(resolve, seconds * 1000)); } - private killAsyncProcesses() { - if(this.asyncProcesses.length > 0) { - psList().then(processes => { - // Get all processes and check if they are child orprocesses of the processes that should be terminated. If so, kill them first. - let killProcessesRecursively = function(processes, processIdToKill) { - let childProcesses = processes.filter(process => { - return process.ppid == processIdToKill; - }); - - if(childProcesses.length > 0) { - childProcesses.forEach(childProcess => { - killProcessesRecursively(processes, childProcess.pid) - }); - } + private async killAsyncProcesses(): Promise { + let killProcessesRecursively = function(processes: psList.ProcessDescriptor[], processIdToKill: number) { + let childProcesses = processes.filter(process => { + return process.ppid == processIdToKill; + }); - process.kill(processIdToKill); + if(childProcesses.length > 0) { + for(let childProcess of childProcesses) { + killProcessesRecursively(processes, childProcess.pid) } + } + + try { + process.kill(processIdToKill); + } catch(e) { + console.error("Error killing id " + processIdToKill, e); + } + } - this.asyncProcesses.forEach(asyncProcess => { - killProcessesRecursively(processes, asyncProcess.pid); - }); - }).then(() => { - //Check if there are still running processes on the given ports - this.asyncProcesses.forEach(asyncProcess => { - findProcess("port", asyncProcess.port).then((processes) => { - if(processes.length > 0) { - processes.forEach(proc => { - if(proc.name == asyncProcess.name || proc.name == asyncProcess.name + ".exe") { - process.kill(proc.pid); - } - }); + if(this.asyncProcesses.length > 0) { + let processes: psList.ProcessDescriptor[] = Array.from((await psList()).values()); + for(let asyncProcess of this.asyncProcesses) { + killProcessesRecursively(processes, asyncProcess.pid); + } + + //Check if there are still running processes on the given ports + for(let asyncProcess of this.asyncProcesses) { + let processes: any[] = await findProcess("port", asyncProcess.port); + if(processes.length > 0) { + for(let proc of processes) { + if(proc.name == asyncProcess.name || proc.name == asyncProcess.name + ".exe") { + try { + process.kill(proc.pid); + } catch(e) { + console.error("Error killing id " + proc.pid, e); + } } - }) - }); - }) + } + } + } } } - private cleanUp(): void { - this.killAsyncProcesses(); + private async cleanUp(): Promise { + await this.killAsyncProcesses(); ConsoleUtils.restoreDevonDirectory(); } } diff --git a/runners/katacoda/index.ts b/runners/katacoda/index.ts index d4c4a797..69261bdd 100644 --- a/runners/katacoda/index.ts +++ b/runners/katacoda/index.ts @@ -51,7 +51,7 @@ export class Katacoda extends Runner { this.assetManager = new KatacodaAssetManager(path.join(this.outputPathTutorial, "assets")); } - destroy(playbook: Playbook): void { + async destroy(playbook: Playbook): Promise { let tutorialDirectoryName = path.basename(playbook.path); this.renderTemplate("intro.md", path.join(this.outputPathTutorial, "intro.md"), { description: playbook.description, tutorialPath: tutorialDirectoryName }); fs.writeFileSync(this.outputPathTutorial + 'finish.md', ""); diff --git a/runners/vscode/index.ts b/runners/vscode/index.ts index 4859a8f8..9c738fd3 100644 --- a/runners/vscode/index.ts +++ b/runners/vscode/index.ts @@ -33,7 +33,7 @@ export class VsCode extends Runner { }); } - destroy(playbook: Playbook): void { + async destroy(playbook: Playbook): Promise { this.cleanUp(); } diff --git a/runners/wikiConsole/index.ts b/runners/wikiConsole/index.ts index 63d25108..9df53d0c 100644 --- a/runners/wikiConsole/index.ts +++ b/runners/wikiConsole/index.ts @@ -10,7 +10,7 @@ export class WikiConsole extends WikiRunner { super.init(playbook); } - destroy(playbook: Playbook): void { + async destroy(playbook: Playbook): Promise { super.destroy(playbook); } diff --git a/runners/wikiEclipse/index.ts b/runners/wikiEclipse/index.ts index d874b053..34eca9d8 100644 --- a/runners/wikiEclipse/index.ts +++ b/runners/wikiEclipse/index.ts @@ -7,7 +7,7 @@ export class WikiEclipse extends WikiRunner { super.init(playbook); } - destroy(playbook: Playbook): void { + async destroy(playbook: Playbook): Promise { super.destroy(playbook); } } \ No newline at end of file diff --git a/runners/wikiEditor/index.ts b/runners/wikiEditor/index.ts index 08fe4e16..4b3838a6 100644 --- a/runners/wikiEditor/index.ts +++ b/runners/wikiEditor/index.ts @@ -7,7 +7,7 @@ export class WikiEditor extends WikiRunner { super.init(playbook); } - destroy(playbook: Playbook): void { + async destroy(playbook: Playbook): Promise { super.destroy(playbook); } } \ No newline at end of file diff --git a/runners/wikiVsCode/index.ts b/runners/wikiVsCode/index.ts index 550adae9..3eef6efa 100644 --- a/runners/wikiVsCode/index.ts +++ b/runners/wikiVsCode/index.ts @@ -7,7 +7,7 @@ export class WikiVsCode extends WikiRunner { super.init(playbook); } - destroy(playbook: Playbook): void { + async destroy(playbook: Playbook): Promise { super.destroy(playbook); } } \ No newline at end of file