mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-28 03:47:03 +02:00
NETSCRIPT: Greatly speed up script launching, and remove the limitation unique args per script (#440)
* Remove the limitation unique args per script * Internal changes to how runningScripts are stored on the server, to make common usage faster.
This commit is contained in:
@@ -1,25 +1,50 @@
|
||||
import { Terminal } from "../../Terminal";
|
||||
import { BaseServer } from "../../Server/BaseServer";
|
||||
import { killWorkerScript } from "../../Netscript/killWorkerScript";
|
||||
import { findRunningScripts } from "../../Script/ScriptHelpers";
|
||||
import { killWorkerScriptByPid } from "../../Netscript/killWorkerScript";
|
||||
import { hasScriptExtension } from "../../Paths/ScriptFilePath";
|
||||
|
||||
import type { BaseServer } from "../../Server/BaseServer";
|
||||
|
||||
export function kill(args: (string | number | boolean)[], server: BaseServer): void {
|
||||
if (args.length < 1) {
|
||||
return Terminal.error("Incorrect usage of kill command. Usage: kill [pid] or kill [scriptname] [arg1] [arg2]...");
|
||||
}
|
||||
if (typeof args[0] === "number") {
|
||||
const pid = args[0];
|
||||
if (killWorkerScript(pid)) return Terminal.print(`Killing script with PID ${pid}`);
|
||||
}
|
||||
// Shift args doesn't need to be sliced to check runningScript args
|
||||
const fileName = String(args.shift());
|
||||
const path = Terminal.getFilepath(fileName);
|
||||
if (!path) return Terminal.error(`Could not parse filename: ${fileName}`);
|
||||
if (!hasScriptExtension(path)) return Terminal.error(`${path} does not have a script file extension`);
|
||||
try {
|
||||
if (args.length < 1 || typeof args[0] === "boolean") {
|
||||
Terminal.error("Incorrect usage of kill command. Usage: kill [pid] or kill [scriptname] [arg1] [arg2]...");
|
||||
return;
|
||||
}
|
||||
|
||||
const runningScript = server.getRunningScript(path, args);
|
||||
if (runningScript == null) return Terminal.error("No such script is running. Nothing to kill");
|
||||
// Kill by PID
|
||||
if (typeof args[0] === "number") {
|
||||
const pid = args[0];
|
||||
const res = killWorkerScriptByPid(pid);
|
||||
if (res) {
|
||||
Terminal.print(`Killing script with PID ${pid}`);
|
||||
} else {
|
||||
Terminal.error(`Failed to kill script with PID ${pid}. No such script is running`);
|
||||
}
|
||||
|
||||
killWorkerScript(runningScript.pid);
|
||||
Terminal.print(`Killing ${path}`);
|
||||
return;
|
||||
}
|
||||
|
||||
const path = Terminal.getFilepath(args[0]);
|
||||
if (!path) return Terminal.error(`Invalid filename: ${args[0]}`);
|
||||
if (!hasScriptExtension(path)) return Terminal.error(`Invalid file extension. Kill can only be used on scripts.`);
|
||||
const runningScripts = findRunningScripts(path, args.slice(1), server);
|
||||
if (runningScripts === null) {
|
||||
Terminal.error("No such script is running. Nothing to kill");
|
||||
return;
|
||||
}
|
||||
let killed = 0;
|
||||
for (const pid of runningScripts.keys()) {
|
||||
killed++;
|
||||
if (killed < 5) {
|
||||
Terminal.print(`Killing ${path} with pid ${pid}`);
|
||||
}
|
||||
killWorkerScriptByPid(pid);
|
||||
}
|
||||
if (killed >= 5) {
|
||||
Terminal.print(`... killed ${killed} instances total`);
|
||||
}
|
||||
} catch (e) {
|
||||
Terminal.error(e + "");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user