/
processes.js
53 lines (48 loc) · 1.75 KB
/
processes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// @flow
import psList from 'ps-list';
import { isObject } from 'lodash';
import { Logger } from './logging';
export type Process = {
pid: number,
name: string,
cmd: string,
ppid?: number,
cpu: number,
memore: number,
};
export const getProcessById = async (processId: number): Promise<Process> => {
// retrieves all running processes
const processes: Array<Process> = await psList();
// filters running processes against previous PID
const matches: Array<Process> = processes.filter(({ pid }) => processId === pid);
return matches.length > 0 ? matches[0] : Promise.reject();
};
export const getProcessName = async (processId: number) => (
(await getProcessById(processId)).name
);
export const getProcessesByName = async (processName: string): Promise<Array<Process>> => {
// retrieves all running processes
const processes: Array<Process> = await psList();
// filters running processes against previous PID
return processes.filter(({ name }) => processName === name);
};
export const getProcess = async (processId: number, processName: string): Promise<?Process> => {
try {
// retrieves all running processes
const runningProcesses: Array<Process> = await psList();
// filters running processes against given pid
const matchingProcesses: Array<Process> = runningProcesses.filter(({ pid }) => (
pid === processId
));
// no processes exist with a matching PID
if (!matchingProcesses.length) return null;
// Return first matching process if names match
const previousProcess: Process = matchingProcesses[0];
if (isObject(previousProcess) && previousProcess.name === processName) {
return previousProcess;
}
} catch (error) {
Logger.error('getProcess error', { error });
return null;
}
};