Skip to content

Commit

Permalink
fix: stop the process successful
Browse files Browse the repository at this point in the history
  • Loading branch information
juanrgm committed Sep 23, 2022
1 parent 17211fe commit bc7f2cc
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 46 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-flowers-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"phpstan-vscode": patch
---

Stop the process successful
60 changes: 43 additions & 17 deletions src/commands/analyse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
parsePHPStanAnalyseResult,
PHPStanAnalyseResult,
} from "../utils/phpstan";
import { waitForClose } from "../utils/process";
import { killProcess, waitForClose } from "../utils/process";
import showOutput from "./showOutput";
import stopAnalyse from "./stopAnalyse";
import { spawn } from "child_process";
Expand Down Expand Up @@ -68,7 +68,7 @@ async function refreshDiagnostics(ext: Ext, result: PHPStanAnalyseResult) {
async function rutine(ext: Ext, args?: string[]) {
setStatusBarProgress(ext);

const childProcess = (ext.store.analyse.process = spawn(
const childProcess = spawn(
ext.settings.phpPath,
[
"-f",
Expand All @@ -87,29 +87,55 @@ async function rutine(ext: Ext, args?: string[]) {
{
cwd: ext.cwd,
}
));

childProcess.stdout.on("data", (buffer: Buffer) => ext.log(buffer));

childProcess.stderr.on("data", (buffer: Buffer) => {
const progress = /(\d{1,3})%\s*$/.exec(buffer.toString())?.[1];
if (progress) setStatusBarProgress(ext, Number(progress));
ext.log(buffer);
);

let stdout = "";
let skipCloseError = false;
const { channel } = ext.store.analyse;

channel.once("stop", async () => {
skipCloseError = true;
try {
const killed = await killProcess(childProcess);
ext.log({
tag: "call",
message: `killProcess (${killed ? "true" : "false"})`,
});
} catch (error) {
ext.log(error as Error);
}
});

const [, stdout] = await waitForClose(childProcess);

ext.store.analyse.process = undefined;

const phpstanResult = parsePHPStanAnalyseResult(stdout);
try {
childProcess.stdout.on("data", (buffer: Buffer) => {
stdout += buffer.toString();
ext.log(buffer);
});
childProcess.stderr.on("data", (buffer: Buffer) => {
const progress = /(\d{1,3})%\s*$/.exec(buffer.toString())?.[1];
if (progress) setStatusBarProgress(ext, Number(progress));
ext.log(buffer);
});
try {
await waitForClose(childProcess);
} catch (error) {
if (skipCloseError) return;
throw error;
}
} finally {
channel.removeAllListeners();
}

refreshDiagnostics(ext, phpstanResult);
if (stdout) {
const phpstanResult = parsePHPStanAnalyseResult(stdout);
refreshDiagnostics(ext, phpstanResult);
}

ext.clearStatusBar();
}

export default async function analyse(ext: Ext, ms?: number, args?: string[]) {
await stopAnalyse(ext);
stopAnalyse(ext);
ext.store.analyse.timeout.run(async () => {
await rutine(ext, args);
}, ms ?? ext.settings.analysedDelay);
Expand Down
9 changes: 2 additions & 7 deletions src/commands/stopAnalyse.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { Ext } from "../extension";
import { killProcess } from "../utils/process";

export default async function stopAnalyse(ext: Ext) {
if (ext.store.analyse.process) {
await killProcess(ext.store.analyse.process);
ext.clearStatusBar();
ext.store.analyse.process = undefined;
}
export default function stopAnalyse(ext: Ext) {
ext.store.analyse.channel.emit("stop");
}
10 changes: 7 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
getWorkspacePath,
onChangeExtensionSettings,
} from "./utils/vscode";
import { ChildProcessWithoutNullStreams } from "child_process";
import { EventEmitter } from "events";
import {
DiagnosticCollection,
Disposable,
Expand Down Expand Up @@ -44,7 +44,7 @@ export type ExtStore = {
timeout: DelayedTimeout;
};
analyse: {
process?: ChildProcessWithoutNullStreams;
channel: EventEmitter;
timeout: DelayedTimeout;
};
fileWatcher: {
Expand Down Expand Up @@ -92,6 +92,7 @@ export class Ext<
},
analyse: {
timeout: createDelayedTimeout(),
channel: new EventEmitter(),
},
fileWatcher: {
enabled: true,
Expand Down Expand Up @@ -227,7 +228,10 @@ export class Ext<
tag: `event:${eventName}`,
message: path,
});
return await this.options.commands.analyse(this);
await this.call(
async () => await this.options.commands.analyse(this),
"analyse"
);
}
}
});
Expand Down
30 changes: 11 additions & 19 deletions src/utils/process.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
import { ChildProcessWithoutNullStreams, exec } from "child_process";
import { platform } from "os";
import { ChildProcess } from "child_process";

export async function killProcess(
process: ChildProcessWithoutNullStreams
): Promise<void> {
const os = platform();
if (os === "win32") {
return new Promise((resolve) => {
exec(`taskkill /pid ${process.pid} /T /F`, () => resolve());
});
} else {
process.kill();
export async function killProcess(p: ChildProcess) {
p.stdout?.removeAllListeners();
p.stderr?.removeAllListeners();
try {
return p.kill("SIGKILL");
} catch (error) {
return false;
}
}

export async function waitForClose(
childProcess: ChildProcessWithoutNullStreams
) {
return new Promise<[number | null, string]>((resolve, reject) => {
let result = "";
childProcess.stdout.on("data", (data) => (result += data));
export async function waitForClose(childProcess: ChildProcess) {
return new Promise<number | null>((resolve, reject) => {
childProcess.on("error", reject);
childProcess.on("close", (exitCode) => resolve([exitCode, result]));
childProcess.on("close", (exitCode) => resolve(exitCode));
});
}

0 comments on commit bc7f2cc

Please sign in to comment.