2019-05-16 08:05:36 +02:00
|
|
|
/**
|
2019-05-17 08:44:59 +02:00
|
|
|
* Stops an actively-running script (represented by a WorkerScript object)
|
2019-05-16 08:05:36 +02:00
|
|
|
* and removes it from the global pool of active scripts.
|
|
|
|
*/
|
2022-02-06 01:15:42 +01:00
|
|
|
import { ScriptDeath } from "./ScriptDeath";
|
2019-05-16 08:05:36 +02:00
|
|
|
import { WorkerScript } from "./WorkerScript";
|
|
|
|
import { workerScripts } from "./WorkerScripts";
|
|
|
|
|
2021-10-07 22:56:01 +02:00
|
|
|
import { GetServer } from "../Server/AllServers";
|
2021-10-23 22:04:00 +02:00
|
|
|
import { AddRecentScript } from "./RecentScripts";
|
2022-09-27 19:35:51 +02:00
|
|
|
import { ITutorial } from "../InteractiveTutorial";
|
|
|
|
import { AlertEvents } from "../ui/React/AlertManager";
|
2022-09-05 15:55:57 +02:00
|
|
|
import { handleUnknownError } from "./NetscriptHelpers";
|
2023-04-28 00:21:06 +02:00
|
|
|
import { roundToTwo } from "../utils/helpers/roundToTwo";
|
2019-05-16 08:05:36 +02:00
|
|
|
|
2023-04-28 00:21:06 +02:00
|
|
|
export function killWorkerScript(ws: WorkerScript): boolean {
|
2022-09-27 19:35:51 +02:00
|
|
|
if (ITutorial.isRunning) {
|
|
|
|
AlertEvents.emit("Processes cannot be killed during the tutorial.");
|
|
|
|
return false;
|
|
|
|
}
|
2023-04-28 00:21:06 +02:00
|
|
|
stopAndCleanUpWorkerScript(ws);
|
2021-09-05 01:09:30 +02:00
|
|
|
|
2023-04-28 00:21:06 +02:00
|
|
|
return true;
|
2019-05-16 08:05:36 +02:00
|
|
|
}
|
|
|
|
|
2023-04-28 00:21:06 +02:00
|
|
|
export function killWorkerScriptByPid(pid: number): boolean {
|
2021-09-05 01:09:30 +02:00
|
|
|
const ws = workerScripts.get(pid);
|
|
|
|
if (ws instanceof WorkerScript) {
|
2022-07-20 07:48:54 +02:00
|
|
|
stopAndCleanUpWorkerScript(ws);
|
2021-09-05 01:09:30 +02:00
|
|
|
return true;
|
|
|
|
}
|
2019-06-19 10:51:25 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
2019-06-19 10:51:25 +02:00
|
|
|
}
|
|
|
|
|
2022-08-17 02:16:57 +02:00
|
|
|
function stopAndCleanUpWorkerScript(ws: WorkerScript): void {
|
2023-04-28 00:21:06 +02:00
|
|
|
// Only clean up once.
|
|
|
|
// Important: Only this function can set stopFlag!
|
|
|
|
if (ws.env.stopFlag) return;
|
|
|
|
|
2022-09-05 15:55:57 +02:00
|
|
|
//Clean up any ongoing netscriptDelay
|
|
|
|
if (ws.delay) clearTimeout(ws.delay);
|
|
|
|
ws.delayReject?.(new ScriptDeath(ws));
|
2022-08-29 08:41:17 +02:00
|
|
|
ws.env.runningFn = "";
|
2022-09-27 22:20:26 +02:00
|
|
|
|
2022-08-17 02:16:57 +02:00
|
|
|
if (typeof ws.atExit === "function") {
|
2021-10-15 02:13:26 +02:00
|
|
|
try {
|
2023-02-23 14:39:47 +01:00
|
|
|
const atExit = ws.atExit;
|
|
|
|
ws.atExit = undefined;
|
|
|
|
atExit();
|
2022-07-15 07:51:30 +02:00
|
|
|
} catch (e: unknown) {
|
2022-09-05 15:55:57 +02:00
|
|
|
handleUnknownError(e, ws, "Error running atExit function.\n\n");
|
2021-10-15 02:13:26 +02:00
|
|
|
}
|
2023-07-01 00:15:37 +02:00
|
|
|
if (ws.env.stopFlag) {
|
|
|
|
// If atExit() kills the script, we'll already be stopped, don't stop again.
|
|
|
|
return;
|
|
|
|
}
|
2021-10-15 02:13:26 +02:00
|
|
|
}
|
2022-08-17 02:16:57 +02:00
|
|
|
ws.env.stopFlag = true;
|
|
|
|
removeWorkerScript(ws);
|
2019-06-22 10:12:17 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 08:05:36 +02:00
|
|
|
/**
|
|
|
|
* Helper function that removes the script being killed from the global pool.
|
|
|
|
* Also handles other cleanup-time operations
|
|
|
|
*
|
2021-10-23 22:04:00 +02:00
|
|
|
* @param {WorkerScript} - Identifier for WorkerScript. Either the object itself, or
|
2019-05-16 08:05:36 +02:00
|
|
|
* its index in the global workerScripts array
|
|
|
|
*/
|
2022-07-20 07:48:54 +02:00
|
|
|
function removeWorkerScript(workerScript: WorkerScript): void {
|
2021-10-23 22:04:00 +02:00
|
|
|
const ip = workerScript.hostname;
|
2021-09-05 01:09:30 +02:00
|
|
|
|
2021-10-23 22:04:00 +02:00
|
|
|
// Get the server on which the script runs
|
|
|
|
const server = GetServer(ip);
|
|
|
|
if (server == null) {
|
|
|
|
console.error(`Could not find server on which this script is running: ${ip}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the RunningScript object from that server
|
2023-04-28 00:21:06 +02:00
|
|
|
const rs = workerScript.scriptRef;
|
|
|
|
const byPid = server.runningScriptMap.get(rs.scriptKey);
|
|
|
|
if (!byPid) {
|
|
|
|
console.error(`Couldn't find runningScriptMap for key ${rs.scriptKey}`);
|
|
|
|
} else {
|
|
|
|
byPid.delete(workerScript.pid);
|
|
|
|
if (byPid.size === 0) {
|
|
|
|
server.runningScriptMap.delete(rs.scriptKey);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-10-23 22:04:00 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
|
2023-04-28 00:21:06 +02:00
|
|
|
// Update ram used. Reround to prevent accumulation of error.
|
|
|
|
server.updateRamUsed(roundToTwo(server.ramUsed - rs.ramUsage * rs.threads));
|
2021-10-09 05:45:54 +02:00
|
|
|
|
2023-04-28 00:21:06 +02:00
|
|
|
workerScripts.delete(workerScript.pid);
|
2021-10-23 22:04:00 +02:00
|
|
|
AddRecentScript(workerScript);
|
2019-05-16 08:05:36 +02:00
|
|
|
}
|