add deno-process-mod.ts
This commit is contained in:
115
libraries/deno-process-mod.ts
Normal file
115
libraries/deno-process-mod.ts
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
import {assertEquals} from "jsr:@std/assert";
|
||||||
|
import {assert} from "jsr:@std/assert/assert";
|
||||||
|
|
||||||
|
export class Process {
|
||||||
|
user: string;
|
||||||
|
pid: number;
|
||||||
|
cpu: number;
|
||||||
|
mem: number;
|
||||||
|
vsz: number;
|
||||||
|
rss: number;
|
||||||
|
tty: string;
|
||||||
|
stat: string;
|
||||||
|
start: string;
|
||||||
|
time: string;
|
||||||
|
command: string;
|
||||||
|
constructor(
|
||||||
|
user: string,
|
||||||
|
pid: number,
|
||||||
|
cpu: number,
|
||||||
|
mem: number,
|
||||||
|
vsz: number,
|
||||||
|
rss: number,
|
||||||
|
tty: string,
|
||||||
|
stat: string,
|
||||||
|
start: string,
|
||||||
|
time: string,
|
||||||
|
command: string,
|
||||||
|
) {
|
||||||
|
this.user = user;
|
||||||
|
this.pid = pid;
|
||||||
|
this.cpu = cpu;
|
||||||
|
this.mem = mem;
|
||||||
|
this.vsz = vsz;
|
||||||
|
this.rss = rss;
|
||||||
|
this.tty = tty;
|
||||||
|
this.stat = stat;
|
||||||
|
this.start = start;
|
||||||
|
this.time = time;
|
||||||
|
this.command = command;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function parseProcessLine(line: string): Process | null {
|
||||||
|
const processMatchRegex =
|
||||||
|
/^\s*([\w+\-_]+)\s+(\d+)\s+([\d.]+)\s+([\d.]+)\s+(\d+)\s+(\d+)\s+([\w\/\\?]+)\s+([\w+<>]+)\s+([\w:]+)\s+([\d:]+)\s+(.*)$/;
|
||||||
|
// "app 3622 0.2 24.0 2932504 453004 ? Sl Jan25 23:04 /usr/lib/jvm/jdk-25/bin/java -Dfastjson.parser.safeMode=true......";
|
||||||
|
// USER PID CPU MEM VSZ RSS TTY STAT START TIME COMMAND
|
||||||
|
const matcher = line.match(processMatchRegex);
|
||||||
|
if (!matcher) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const user = matcher[1];
|
||||||
|
const pid = parseInt(matcher[2]);
|
||||||
|
const cpu = Number(matcher[3]);
|
||||||
|
const mem = Number(matcher[4]);
|
||||||
|
const vsz = parseInt(matcher[5]);
|
||||||
|
const rss = parseInt(matcher[6]);
|
||||||
|
const tty = matcher[7];
|
||||||
|
const stat = matcher[8];
|
||||||
|
const start = matcher[9];
|
||||||
|
const time = matcher[10];
|
||||||
|
const command = matcher[11];
|
||||||
|
return new Process(
|
||||||
|
user,
|
||||||
|
pid,
|
||||||
|
cpu,
|
||||||
|
mem,
|
||||||
|
vsz,
|
||||||
|
rss,
|
||||||
|
tty,
|
||||||
|
stat,
|
||||||
|
start,
|
||||||
|
time,
|
||||||
|
command,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Deno.test("parseProcessLine", () => {
|
||||||
|
const p1 = parseProcessLine(
|
||||||
|
"app 3622 0.2 24.0 2932504 453004 ? Sl Jan25 23:04 /usr/lib/jvm/jdk-25/bin/java -Dfastjson.parser.safeMode=true......",
|
||||||
|
);
|
||||||
|
if (p1 !== null) {
|
||||||
|
assertEquals("app", p1.user);
|
||||||
|
assertEquals(3622, p1.pid);
|
||||||
|
} else {
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
const p2 = parseProcessLine(
|
||||||
|
"root 10880 0.0 0.0 151104 1820 pts/2 R+ 23:17 0:00 ps aux",
|
||||||
|
);
|
||||||
|
if (p2 !== null) {
|
||||||
|
assertEquals("root", p2.user);
|
||||||
|
assertEquals(10880, p2.pid);
|
||||||
|
} else {
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
const p3 = parseProcessLine(
|
||||||
|
"root 18 0.0 0.0 0 0 ? S< 2020 0:00 [kblockd]",
|
||||||
|
);
|
||||||
|
if (p3 !== null) {
|
||||||
|
assertEquals("root", p3.user);
|
||||||
|
assertEquals(18, p3.pid);
|
||||||
|
} else {
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
const p4 = parseProcessLine(
|
||||||
|
"filebro+ 10377 0.0 0.6 1901492 12432 ? Sl 2024 25:15 /home/filebrowser/filebrowser",
|
||||||
|
);
|
||||||
|
if (p4 !== null) {
|
||||||
|
assertEquals("filebro+", p4.user);
|
||||||
|
assertEquals(10377, p4.pid);
|
||||||
|
} else {
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -11,8 +11,7 @@ import {
|
|||||||
readFileToString,
|
readFileToString,
|
||||||
sleep,
|
sleep,
|
||||||
} from "https://global.hatter.ink/script/get/@24/deno-commons-mod.ts";
|
} from "https://global.hatter.ink/script/get/@24/deno-commons-mod.ts";
|
||||||
import {assertEquals} from "jsr:@std/assert";
|
import {parseProcessLine, Process,} from "https://global.hatter.ink/script/get/@0/deno-process-mod.ts";
|
||||||
import {assert} from "jsr:@std/assert/assert";
|
|
||||||
import {fromFileUrl} from "https://deno.land/std/path/mod.ts";
|
import {fromFileUrl} from "https://deno.land/std/path/mod.ts";
|
||||||
|
|
||||||
interface HealthCheck {
|
interface HealthCheck {
|
||||||
@@ -28,45 +27,6 @@ interface ServerControlConfig {
|
|||||||
healthCheck?: HealthCheck;
|
healthCheck?: HealthCheck;
|
||||||
}
|
}
|
||||||
|
|
||||||
class Process {
|
|
||||||
user: string;
|
|
||||||
pid: number;
|
|
||||||
cpu: number;
|
|
||||||
mem: number;
|
|
||||||
vsz: number;
|
|
||||||
rss: number;
|
|
||||||
tty: string;
|
|
||||||
stat: string;
|
|
||||||
start: string;
|
|
||||||
time: string;
|
|
||||||
command: string;
|
|
||||||
constructor(
|
|
||||||
user: string,
|
|
||||||
pid: number,
|
|
||||||
cpu: number,
|
|
||||||
mem: number,
|
|
||||||
vsz: number,
|
|
||||||
rss: number,
|
|
||||||
tty: string,
|
|
||||||
stat: string,
|
|
||||||
start: string,
|
|
||||||
time: string,
|
|
||||||
command: string,
|
|
||||||
) {
|
|
||||||
this.user = user;
|
|
||||||
this.pid = pid;
|
|
||||||
this.cpu = cpu;
|
|
||||||
this.mem = mem;
|
|
||||||
this.vsz = vsz;
|
|
||||||
this.rss = rss;
|
|
||||||
this.tty = tty;
|
|
||||||
this.stat = stat;
|
|
||||||
this.start = start;
|
|
||||||
this.time = time;
|
|
||||||
this.command = command;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function listJavaProcesses(
|
async function listJavaProcesses(
|
||||||
serverControlConfig: ServerControlConfig,
|
serverControlConfig: ServerControlConfig,
|
||||||
): Promise<Process[]> {
|
): Promise<Process[]> {
|
||||||
@@ -83,41 +43,6 @@ async function listJavaProcesses(
|
|||||||
}).filter((line) => line !== null);
|
}).filter((line) => line !== null);
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseProcessLine(line: string): Process | null {
|
|
||||||
const processMatchRegex =
|
|
||||||
/^\s*([\w+\-_]+)\s+(\d+)\s+([\d.]+)\s+([\d.]+)\s+(\d+)\s+(\d+)\s+([\w\/\\?]+)\s+([\w+<>]+)\s+([\w:]+)\s+([\d:]+)\s+(.*)$/;
|
|
||||||
// "app 3622 0.2 24.0 2932504 453004 ? Sl Jan25 23:04 /usr/lib/jvm/jdk-25/bin/java -Dfastjson.parser.safeMode=true......";
|
|
||||||
// USER PID CPU MEM VSZ RSS TTY STAT START TIME COMMAND
|
|
||||||
const matcher = line.match(processMatchRegex);
|
|
||||||
if (!matcher) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
const user = matcher[1];
|
|
||||||
const pid = parseInt(matcher[2]);
|
|
||||||
const cpu = Number(matcher[3]);
|
|
||||||
const mem = Number(matcher[4]);
|
|
||||||
const vsz = parseInt(matcher[5]);
|
|
||||||
const rss = parseInt(matcher[6]);
|
|
||||||
const tty = matcher[7];
|
|
||||||
const stat = matcher[8];
|
|
||||||
const start = matcher[9];
|
|
||||||
const time = matcher[10];
|
|
||||||
const command = matcher[11];
|
|
||||||
return new Process(
|
|
||||||
user,
|
|
||||||
pid,
|
|
||||||
cpu,
|
|
||||||
mem,
|
|
||||||
vsz,
|
|
||||||
rss,
|
|
||||||
tty,
|
|
||||||
stat,
|
|
||||||
start,
|
|
||||||
time,
|
|
||||||
command,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function checkServerStarted(
|
async function checkServerStarted(
|
||||||
serverControlConfig: ServerControlConfig,
|
serverControlConfig: ServerControlConfig,
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
@@ -230,11 +155,11 @@ async function handleRestart(serverControlConfig: ServerControlConfig) {
|
|||||||
async function main() {
|
async function main() {
|
||||||
const args = Deno.args;
|
const args = Deno.args;
|
||||||
if (args.length === 0) {
|
if (args.length === 0) {
|
||||||
log.error("No args.");
|
log.error(`No args.
|
||||||
console.log("");
|
|
||||||
console.log("server-control.js status");
|
server-control.js status
|
||||||
console.log("server-control.js kill|stop");
|
server-control.js kill|stop
|
||||||
console.log("server-control.js re|restart");
|
server-control.js re|restart`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const serverControlConfig = await loadServerControlConfig();
|
const serverControlConfig = await loadServerControlConfig();
|
||||||
@@ -262,42 +187,3 @@ async function main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
main().catch((e) => log.error(e));
|
main().catch((e) => log.error(e));
|
||||||
|
|
||||||
Deno.test("parseProcessLine", () => {
|
|
||||||
const p1 = parseProcessLine(
|
|
||||||
"app 3622 0.2 24.0 2932504 453004 ? Sl Jan25 23:04 /usr/lib/jvm/jdk-25/bin/java -Dfastjson.parser.safeMode=true......",
|
|
||||||
);
|
|
||||||
if (p1 !== null) {
|
|
||||||
assertEquals("app", p1.user);
|
|
||||||
assertEquals(3622, p1.pid);
|
|
||||||
} else {
|
|
||||||
assert(false);
|
|
||||||
}
|
|
||||||
const p2 = parseProcessLine(
|
|
||||||
"root 10880 0.0 0.0 151104 1820 pts/2 R+ 23:17 0:00 ps aux",
|
|
||||||
);
|
|
||||||
if (p2 !== null) {
|
|
||||||
assertEquals("root", p2.user);
|
|
||||||
assertEquals(10880, p2.pid);
|
|
||||||
} else {
|
|
||||||
assert(false);
|
|
||||||
}
|
|
||||||
const p3 = parseProcessLine(
|
|
||||||
"root 18 0.0 0.0 0 0 ? S< 2020 0:00 [kblockd]",
|
|
||||||
);
|
|
||||||
if (p3 !== null) {
|
|
||||||
assertEquals("root", p3.user);
|
|
||||||
assertEquals(18, p3.pid);
|
|
||||||
} else {
|
|
||||||
assert(false);
|
|
||||||
}
|
|
||||||
const p4 = parseProcessLine(
|
|
||||||
"filebro+ 10377 0.0 0.6 1901492 12432 ? Sl 2024 25:15 /home/filebrowser/filebrowser",
|
|
||||||
);
|
|
||||||
if (p4 !== null) {
|
|
||||||
assertEquals("filebro+", p4.user);
|
|
||||||
assertEquals(10377, p4.pid);
|
|
||||||
} else {
|
|
||||||
assert(false);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|||||||
Reference in New Issue
Block a user