Skip to content

Commit 2bc1e0c

Browse files
Merge pull request devonfw-tutorials#155 from GuentherJulian/fix/cleanUp
fix cleanup in console runner
2 parents 58830b5 + 342657c commit 2bc1e0c

File tree

10 files changed

+79
-71
lines changed

10 files changed

+79
-71
lines changed

engine/engine.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class Engine {
5757
}
5858

5959
for (let runnerIndex in this.environment.runners) {
60-
(await this.getRunner(this.environment.runners[runnerIndex])).destroy(this.playbook);
60+
await (await this.getRunner(this.environment.runners[runnerIndex])).destroy(this.playbook);
6161
}
6262
}
6363

engine/runner.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,17 @@ export abstract class Runner {
9797
}
9898
}
9999

100-
destroy(playbook: Playbook): void {
100+
async destroy(playbook: Playbook): Promise<void> {
101101
}
102102

103-
protected createFolder(path: string, deleteFolerIfExist: boolean) {
103+
protected createFolder(path: string, deleteFolderIfExist: boolean) {
104104
if(fs.existsSync(path)) {
105-
if(deleteFolerIfExist) {
106-
rimraf.sync(path);
107-
fs.mkdirSync(path, { recursive: true });
105+
if(deleteFolderIfExist) {
106+
try {
107+
rimraf.sync(path);
108+
} catch(e) {
109+
console.log("Error deleting foler " + path, e);
110+
}
108111
} else return path;
109112
}
110113
fs.mkdirSync(path, { recursive: true });

engine/wikiRunner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export abstract class WikiRunner extends Runner {
1313
this.outputPathTutorial = this.createFolder(path.join(outputDirectory, playbook.name), true);
1414
}
1515

16-
destroy(playbook: Playbook): void {
16+
async destroy(playbook: Playbook): Promise<void> {
1717

1818
}
1919

runners/console/index.ts

Lines changed: 63 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ export class Console extends Runner {
3636
this.env = process.env;
3737
}
3838

39-
destroy(playbook: Playbook): void {
40-
this.cleanUp();
39+
async destroy(playbook: Playbook): Promise<void> {
40+
await this.cleanUp();
4141
}
4242

4343
runInstallDevonfwIde(runCommand: RunCommand): RunResult {
@@ -361,7 +361,7 @@ export class Console extends Runner {
361361
assert.directoryExits(path.join(this.getWorkingDirectory(), "devonfw", "software", tool));
362362
}
363363
} catch(error) {
364-
this.cleanUp();
364+
await this.cleanUp();
365365
throw error;
366366
}
367367
}
@@ -379,7 +379,7 @@ export class Console extends Runner {
379379
.fileExits(path.join(this.getWorkingDirectory(), "devonfw", "software", "cobigen-cli", "cobigen.jar"))
380380
.fileExits(path.join(this.getWorkingDirectory(), "devonfw", "software", "cobigen-cli", "cobigen"));
381381
} catch(error) {
382-
this.cleanUp();
382+
await this.cleanUp();
383383
throw error;
384384
}
385385
}
@@ -393,7 +393,7 @@ export class Console extends Runner {
393393
.directoryExits(path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0], "core", "target"))
394394
.directoryExits(path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0], "server", "target"));
395395
} catch(error) {
396-
this.cleanUp();
396+
await this.cleanUp();
397397
throw error;
398398
}
399399
}
@@ -405,7 +405,7 @@ export class Console extends Runner {
405405
.noException(result)
406406
.fileExits(path.join(this.getWorkingDirectory(), "devonfw", "workspaces", "main", runCommand.command.parameters[0]));
407407
} catch(error) {
408-
this.cleanUp();
408+
await this.cleanUp();
409409
throw error;
410410
}
411411
}
@@ -423,7 +423,7 @@ export class Console extends Runner {
423423
.directoryExits(path.join(workspaceDir, runCommand.command.parameters[0], "server", "src", "main", "java"))
424424
.fileExits(path.join(workspaceDir, runCommand.command.parameters[0], "core", "src", "main", "java", "com", "example", "application", runCommand.command.parameters[0], "SpringBootApp.java"));
425425
} catch(error) {
426-
this.cleanUp();
426+
await this.cleanUp();
427427
throw error;
428428
}
429429
}
@@ -435,7 +435,7 @@ export class Console extends Runner {
435435
.noException(result)
436436
.fileExits(path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0]));
437437
} catch(error) {
438-
this.cleanUp();
438+
await this.cleanUp();
439439
throw error;
440440
}
441441
}
@@ -456,7 +456,7 @@ export class Console extends Runner {
456456
.fileExits(filepath)
457457
.fileContains(filepath, content);
458458
} catch(error) {
459-
this.cleanUp();
459+
await this.cleanUp();
460460
throw error;
461461
}
462462
}
@@ -475,18 +475,18 @@ export class Console extends Runner {
475475
await this.sleep(runCommand.command.parameters[1].startupTime);
476476

477477
if(!runCommand.command.parameters[1].port) {
478-
this.killAsyncProcesses();
478+
await this.killAsyncProcesses();
479479
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.");
480480
} else {
481481
let isReachable = await assert.serverIsReachable(runCommand.command.parameters[1].port, runCommand.command.parameters[1].path);
482482
if(!isReachable) {
483-
this.killAsyncProcesses();
483+
await this.killAsyncProcesses();
484484
throw new Error("The server has not become reachable in " + startupTimeInSeconds + " seconds: " + "http://localhost:" + runCommand.command.parameters[1].port + "/" + runCommand.command.parameters[1].path);
485485
}
486486
}
487487
}
488488
} catch(error) {
489-
this.cleanUp();
489+
await this.cleanUp();
490490
throw error;
491491
}
492492
}
@@ -505,18 +505,18 @@ export class Console extends Runner {
505505
await this.sleep(runCommand.command.parameters[1].startupTime);
506506

507507
if(!runCommand.command.parameters[1].port || !runCommand.command.parameters[1].path) {
508-
this.killAsyncProcesses();
508+
await this.killAsyncProcesses();
509509
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.");
510510
} else {
511511
let isReachable = await assert.serverIsReachable(runCommand.command.parameters[1].port, runCommand.command.parameters[1].path);
512512
if(!isReachable) {
513-
this.killAsyncProcesses();
513+
await this.killAsyncProcesses();
514514
throw new Error("The server has not become reachable in " + startupTimeInSeconds + " seconds: " + "http://localhost:" + runCommand.command.parameters[1].port + "/" + runCommand.command.parameters[1].path)
515515
}
516516
}
517517
}
518518
} catch(error) {
519-
this.cleanUp();
519+
await this.cleanUp();
520520
throw error;
521521
}
522522
}
@@ -534,7 +534,7 @@ export class Console extends Runner {
534534
.directoryNotEmpty(path.join(this.getVariable(this.workspaceDirectory), runCommand.command.parameters[0], repoName))
535535
.repositoryIsClean(directorypath);
536536
} catch(error) {
537-
this.cleanUp();
537+
await this.cleanUp();
538538
throw error;
539539
}
540540
}
@@ -552,7 +552,7 @@ export class Console extends Runner {
552552
.directoryNotEmpty(path.join(projectDir, "node_modules"));
553553
}
554554
} catch(error) {
555-
this.cleanUp();
555+
await this.cleanUp();
556556
throw error;
557557
}
558558
}
@@ -570,7 +570,7 @@ export class Console extends Runner {
570570
.directoryNotEmpty(directory)
571571
.fileExits(path.join(directory, runCommand.command.parameters[1]));
572572
} catch(error) {
573-
this.cleanUp();
573+
await this.cleanUp();
574574
throw error;
575575
}
576576
}
@@ -589,18 +589,18 @@ export class Console extends Runner {
589589
await this.sleep(runCommand.command.parameters[1].startupTime);
590590

591591
if(!runCommand.command.parameters[1].port) {
592-
this.killAsyncProcesses();
592+
await this.killAsyncProcesses();
593593
throw new Error("Missing arguments for command runClientNg. You have to specify a port for the server. For further information read the function documentation.");
594594
} else {
595595
let isReachable = await assert.serverIsReachable(runCommand.command.parameters[1].port, runCommand.command.parameters[1].path);
596596
if(!isReachable) {
597-
this.killAsyncProcesses();
597+
await this.killAsyncProcesses();
598598
throw new Error("The server has not become reachable in " + startupTimeInSeconds + " seconds: " + "http://localhost:" + runCommand.command.parameters[1].port + "/" + runCommand.command.parameters[1].path)
599599
}
600600
}
601601
}
602602
} catch(error) {
603-
this.cleanUp();
603+
await this.cleanUp();
604604
throw error;
605605
}
606606
}
@@ -625,7 +625,7 @@ export class Console extends Runner {
625625
.directoryNotEmpty(path.join(projectPath, outputpath));
626626

627627
} catch(error) {
628-
this.cleanUp();
628+
await this.cleanUp();
629629
throw error;
630630
}
631631
}
@@ -638,7 +638,7 @@ export class Console extends Runner {
638638
.noException(result)
639639
.directoryExits(folderPath);
640640
} catch(error) {
641-
this.cleanUp();
641+
await this.cleanUp();
642642
throw error;
643643
}
644644
}
@@ -653,7 +653,7 @@ export class Console extends Runner {
653653
.directoryNotEmpty(templatesDir);
654654

655655
} catch(error) {
656-
this.cleanUp();
656+
await this.cleanUp();
657657
throw error;
658658
}
659659
}
@@ -667,7 +667,7 @@ export class Console extends Runner {
667667
.directoryExits(projectDir)
668668
.directoryNotEmpty(projectDir);
669669
} catch(error) {
670-
this.cleanUp();
670+
await this.cleanUp();
671671
throw error;
672672
}
673673
}
@@ -690,46 +690,51 @@ export class Console extends Runner {
690690
return new Promise(resolve => setTimeout(resolve, seconds * 1000));
691691
}
692692

693-
private killAsyncProcesses() {
694-
if(this.asyncProcesses.length > 0) {
695-
psList().then(processes => {
696-
// Get all processes and check if they are child orprocesses of the processes that should be terminated. If so, kill them first.
697-
let killProcessesRecursively = function(processes, processIdToKill) {
698-
let childProcesses = processes.filter(process => {
699-
return process.ppid == processIdToKill;
700-
});
701-
702-
if(childProcesses.length > 0) {
703-
childProcesses.forEach(childProcess => {
704-
killProcessesRecursively(processes, childProcess.pid)
705-
});
706-
}
693+
private async killAsyncProcesses(): Promise<void> {
694+
let killProcessesRecursively = function(processes: psList.ProcessDescriptor[], processIdToKill: number) {
695+
let childProcesses = processes.filter(process => {
696+
return process.ppid == processIdToKill;
697+
});
707698

708-
process.kill(processIdToKill);
699+
if(childProcesses.length > 0) {
700+
for(let childProcess of childProcesses) {
701+
killProcessesRecursively(processes, childProcess.pid)
709702
}
703+
}
704+
705+
try {
706+
process.kill(processIdToKill);
707+
} catch(e) {
708+
console.error("Error killing id " + processIdToKill, e);
709+
}
710+
}
710711

711-
this.asyncProcesses.forEach(asyncProcess => {
712-
killProcessesRecursively(processes, asyncProcess.pid);
713-
});
714-
}).then(() => {
715-
//Check if there are still running processes on the given ports
716-
this.asyncProcesses.forEach(asyncProcess => {
717-
findProcess("port", asyncProcess.port).then((processes) => {
718-
if(processes.length > 0) {
719-
processes.forEach(proc => {
720-
if(proc.name == asyncProcess.name || proc.name == asyncProcess.name + ".exe") {
721-
process.kill(proc.pid);
722-
}
723-
});
712+
if(this.asyncProcesses.length > 0) {
713+
let processes: psList.ProcessDescriptor[] = Array.from((await psList()).values());
714+
for(let asyncProcess of this.asyncProcesses) {
715+
killProcessesRecursively(processes, asyncProcess.pid);
716+
}
717+
718+
//Check if there are still running processes on the given ports
719+
for(let asyncProcess of this.asyncProcesses) {
720+
let processes: any[] = await findProcess("port", asyncProcess.port);
721+
if(processes.length > 0) {
722+
for(let proc of processes) {
723+
if(proc.name == asyncProcess.name || proc.name == asyncProcess.name + ".exe") {
724+
try {
725+
process.kill(proc.pid);
726+
} catch(e) {
727+
console.error("Error killing id " + proc.pid, e);
728+
}
724729
}
725-
})
726-
});
727-
})
730+
}
731+
}
732+
}
728733
}
729734
}
730735

731-
private cleanUp(): void {
732-
this.killAsyncProcesses();
736+
private async cleanUp(): Promise<void> {
737+
await this.killAsyncProcesses();
733738
ConsoleUtils.restoreDevonDirectory();
734739
}
735740
}

runners/katacoda/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class Katacoda extends Runner {
5151
this.assetManager = new KatacodaAssetManager(path.join(this.outputPathTutorial, "assets"));
5252
}
5353

54-
destroy(playbook: Playbook): void {
54+
async destroy(playbook: Playbook): Promise<void> {
5555
let tutorialDirectoryName = path.basename(playbook.path);
5656
this.renderTemplate("intro.md", path.join(this.outputPathTutorial, "intro.md"), { description: playbook.description, tutorialPath: tutorialDirectoryName });
5757
fs.writeFileSync(this.outputPathTutorial + 'finish.md', "");

runners/vscode/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class VsCode extends Runner {
3333
});
3434
}
3535

36-
destroy(playbook: Playbook): void {
36+
async destroy(playbook: Playbook): Promise<void> {
3737
this.cleanUp();
3838
}
3939

runners/wikiConsole/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class WikiConsole extends WikiRunner {
1010
super.init(playbook);
1111
}
1212

13-
destroy(playbook: Playbook): void {
13+
async destroy(playbook: Playbook): Promise<void> {
1414
super.destroy(playbook);
1515
}
1616

runners/wikiEclipse/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class WikiEclipse extends WikiRunner {
77
super.init(playbook);
88
}
99

10-
destroy(playbook: Playbook): void {
10+
async destroy(playbook: Playbook): Promise<void> {
1111
super.destroy(playbook);
1212
}
1313
}

runners/wikiEditor/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class WikiEditor extends WikiRunner {
77
super.init(playbook);
88
}
99

10-
destroy(playbook: Playbook): void {
10+
async destroy(playbook: Playbook): Promise<void> {
1111
super.destroy(playbook);
1212
}
1313
}

runners/wikiVsCode/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class WikiVsCode extends WikiRunner {
77
super.init(playbook);
88
}
99

10-
destroy(playbook: Playbook): void {
10+
async destroy(playbook: Playbook): Promise<void> {
1111
super.destroy(playbook);
1212
}
1313
}

0 commit comments

Comments
 (0)