Skip to content

Commit

Permalink
Merge pull request #91 from yaklang/nonight/fix/fix-ps-list
Browse files Browse the repository at this point in the history
fix: 调整ps-list代码
  • Loading branch information
VillanCh authored Jan 13, 2022
2 parents 3de1155 + ef8eead commit 7633343
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,18 @@ const nonWindowsMultipleCalls = async (options = {}) => {
const ERROR_MESSAGE_PARSING_FAILED = 'ps output parsing failed';

const psFields = 'pid,ppid,uid,%cpu,%mem,comm,args';
// const psFields = 'pid,ppid,uid,comm,args';

// TODO: Use named capture groups when targeting Node.js 10
const psOutputRegex = /^[ \t]*(?<pid>\d+)[ \t]+(?<ppid>\d+)[ \t]+(?<uid>\d+)[ \t]+(?<cpu>\d+\.\d+)[ \t]+(?<memory>\d+\.\d+)[ \t]+/;
// const psOutputRegex = /^[ \t]*(?<pid>\d+)[ \t]+(?<ppid>\d+)[ \t]+(?<uid>\d+)[ \t]+/;

const nonWindowsSingleCall = async (options = {}) => {
const flags = options.all === false ? 'wwxo' : 'awwxo';

// TODO: Use the promise version of `execFile` when https://github.com/nodejs/node/issues/28244 is fixed.
const [psPid, stdout] = await new Promise((resolve, reject) => {
const child = childProcess.execFile('ps', [flags, psFields], {maxBuffer: TEN_MEGABYTES}, (error, stdout) => {
const child = childProcess.exec(`ps ${flags} ${psFields} | grep yak`, {maxBuffer: TEN_MEGABYTES}, (error, stdout) => {
if (error === null) {
resolve([child.pid, stdout]);
} else {
Expand All @@ -107,6 +109,7 @@ const nonWindowsSingleCall = async (options = {}) => {
}

const {pid, ppid, uid, cpu, memory} = match.groups;
// const {pid, ppid, uid} = match.groups;

const processInfo = {
pid: Number.parseInt(pid, 10),
Expand All @@ -118,18 +121,18 @@ const nonWindowsSingleCall = async (options = {}) => {
cmd: undefined
};

if (processInfo.pid === psPid) {
psIndex = index;
commPosition = line.indexOf('ps', match[0].length);
argsPosition = line.indexOf('ps', commPosition + 2);
}
// if (processInfo.pid === psPid) {
// psIndex = index;
// commPosition = line.indexOf('ps', match[0].length);
// argsPosition = line.indexOf('ps', commPosition + 2);
// }

return processInfo;
});

if (psIndex === undefined || commPosition === -1 || argsPosition === -1) {
throw new Error(ERROR_MESSAGE_PARSING_FAILED);
}
// if (psIndex === undefined || commPosition === -1 || argsPosition === -1) {
// throw new Error(ERROR_MESSAGE_PARSING_FAILED);
// }

const commLength = argsPosition - commPosition;
for (const [index, line] of lines.entries()) {
Expand All @@ -144,20 +147,11 @@ const nonWindowsSingleCall = async (options = {}) => {
const nonWindows = async (options = {}) => {
try {
return await nonWindowsSingleCall(options);
} catch (_) { // If the error is not a parsing error, it should manifest itself in multicall version too.
console.info("111111111111111111111111111111111")
console.info("111111111111111111111111111111111")
console.info("111111111111111111111111111111111")
console.info("111111111111111111111111111111111")
console.info("111111111111111111111111111111111")
console.info("111111111111111111111111111111111")
console.info("111111111111111111111111111111111")
console.info("111111111111111111111111111111111")
console.info("111111111111111111111111111111111")
console.info("111111111111111111111111111111111")
console.info("111111111111111111111111111111111")
console.info("111111111111111111111111111111111")
return nonWindowsMultipleCalls(options);
} catch (err) { // If the error is not a parsing error, it should manifest itself in multicall version too.
return await new Promise((_, rej) => {
rej(err)
})
// return nonWindowsMultipleCalls(options);
}
};

Expand Down
2 changes: 1 addition & 1 deletion app/main/handlers/yakLocal.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const {ipcMain, dialog} = require("electron");
const childProcess = require("child_process");
const process = require("process");
const psList = require("./libs/ps-list");
const psList = require("./libs/ps-yak-process");
const _sudoPrompt = require("sudo-prompt");
const fs = require("fs");

Expand Down
7 changes: 7 additions & 0 deletions app/renderer/src/main/src/protected/YakLocalProcess.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const YakLocalProcess: React.FC<YakLocalProcessProp> = (props) => {
const [shouldAutoStart, setShouldAutoStart] = useState(false);
const [installed, setInstalled] = useState(false);
const [psIng, setPsIng] = useState(false);
const [notified, setNotified] = useState(false);

const update = useMemoizedFn(() => {
let noProcess = true;
Expand All @@ -39,6 +40,7 @@ export const YakLocalProcess: React.FC<YakLocalProcessProp> = (props) => {
}
setPsIng(true)
ipcRenderer.invoke("ps-yak-grpc").then((i: yakProcess[]) => {
setNotified(false)
setProcess(i.map((element: yakProcess) => {
noProcess = false;
return {
Expand All @@ -48,6 +50,11 @@ export const YakLocalProcess: React.FC<YakLocalProcessProp> = (props) => {
origin: element.origin,
}
}).filter(i => true))
}).catch(e => {
if (!notified) {
failed(`PS | GREP yak failed ${e}`)
setNotified(true)
}
}).finally(() => {
if (noProcess) {
setShouldAutoStart(true)
Expand Down

0 comments on commit 7633343

Please sign in to comment.