Skip to content

Commit cc046e3

Browse files
committed
used new npm package to get processes
1 parent 19c9cf1 commit cc046e3

1 file changed

Lines changed: 22 additions & 11 deletions

File tree

runners/console/index.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { Playbook } from "../../engine/playbook";
66
import { ConsolePlatform, AsyncProcess } from "./consoleInterfaces";
77
import * as path from 'path';
88
import * as fs from "fs-extra";
9-
import * as psList from "ps-list";
109
import { ConsoleUtils } from "./consoleUtils";
1110

11+
const { listProcess } = require("process-list");
1212
const findProcess = require("find-process");
1313

1414
const os = require("os");
@@ -413,7 +413,7 @@ export class Console extends Runner {
413413
return result;
414414
}
415415

416-
runNextKatacodaStep(runCommand: RunCommand): RunResult {
416+
runDisplayContent(runCommand: RunCommand): RunResult {
417417
//Only needed for katacoda and wiki runner
418418
return null;
419419
}
@@ -918,15 +918,15 @@ export class Console extends Runner {
918918
}
919919

920920
private async killAsyncProcesses(): Promise<void> {
921-
let killProcessesRecursively = function(processes: psList.ProcessDescriptor[], processIdToKill: number) {
921+
let killProcessesRecursively = function(processes: Object[], processIdToKill: number) {
922922
let childProcesses = processes.filter(process => {
923-
return process.ppid == processIdToKill;
923+
return process["ppid"] == processIdToKill;
924924
});
925925

926926

927927
if(childProcesses.length > 0) {
928928
for(let childProcess of childProcesses) {
929-
killProcessesRecursively(processes, childProcess.pid)
929+
killProcessesRecursively(processes, childProcess["pid"])
930930
}
931931
}
932932

@@ -936,17 +936,27 @@ export class Console extends Runner {
936936
console.error("Error killing id " + processIdToKill, e);
937937
}
938938
}
939-
939+
let processes = await listProcess("name", "pid", "ppid", "path");
940940
if(this.asyncProcesses.length > 0) {
941-
let processes: psList.ProcessDescriptor[] = Array.from((await psList()).values());
942941
for(let asyncProcess of this.asyncProcesses) {
943942
killProcessesRecursively(processes, asyncProcess.pid);
944943
}
944+
}
945+
for(let proc of processes){
946+
if(path.normalize(proc.path).includes(path.normalize(this.getWorkingDirectory()))){
947+
try {
948+
process.kill(proc.pid);
949+
} catch(e) {
950+
console.error("Error killing process "+proc.name+" with id: " + proc.pid , e);
951+
}
952+
}
953+
}
945954
//Check if there are still running processes on the given ports
955+
// Maybe not needed anymore can be deleted and the function documentation should be updated
946956
for(let asyncProcess of this.asyncProcesses.reverse()) {
947-
let processes: any[] = await findProcess("port", asyncProcess.port);
948-
if(processes.length > 0) {
949-
for(let proc of processes) {
957+
let portProcesses: any[] = await findProcess("port", asyncProcess.port);
958+
if(portProcesses.length > 0) {
959+
for(let proc of portProcesses) {
950960
try {
951961
process.kill(proc.pid);
952962
} catch(e) {
@@ -955,7 +965,8 @@ export class Console extends Runner {
955965
}
956966
}
957967
}
958-
}
968+
969+
959970
}
960971

961972
private async cleanUp(): Promise<void> {

0 commit comments

Comments
 (0)