updates fresh, adds socket error handling

This commit is contained in:
Emmaline Autumn 2023-10-21 14:30:27 -06:00
parent 65b9673f6d
commit afc4d2d5d2
2 changed files with 55 additions and 66 deletions

View File

@ -8,36 +8,19 @@
"update": "deno run -A -r https://fresh.deno.dev/update .", "update": "deno run -A -r https://fresh.deno.dev/update .",
"clean": "rm -rf ./server && rm ./mcgrizz.json && rm ./players.cache.json" "clean": "rm -rf ./server && rm ./mcgrizz.json && rm ./players.cache.json"
}, },
"lint": { "lint": { "rules": { "tags": ["fresh", "recommended"] } },
"rules": {
"tags": [
"fresh",
"recommended"
]
},
"exclude": [
"_fresh"
]
},
"fmt": {
"exclude": [
"_fresh"
]
},
"imports": { "imports": {
"$fresh/": "https://deno.land/x/fresh@1.4.3/", "$fresh/": "https://deno.land/x/fresh@1.5.2/",
"preact": "https://esm.sh/preact@10.15.1", "preact": "https://esm.sh/preact@10.18.1",
"preact/": "https://esm.sh/preact@10.15.1/", "preact/": "https://esm.sh/preact@10.18.1/",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.1", "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.2",
"@preact/signals": "https://esm.sh/*@preact/signals@1.1.3", "@preact/signals": "https://esm.sh/*@preact/signals@1.2.1",
"@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.2.3", "@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.5.0",
"$std/": "https://deno.land/std@0.193.0/", "$std/": "https://deno.land/std@0.193.0/",
"puppet": "https://deno.land/x/sockpuppet@0.6.2/mod.ts", "puppet": "https://deno.land/x/sockpuppet@0.6.2/mod.ts",
"puppet/client": "https://deno.land/x/sockpuppet@0.6.2/client/mod.ts", "puppet/client": "https://deno.land/x/sockpuppet@0.6.2/client/mod.ts",
"jsdom": "https://esm.sh/jsdom" "jsdom": "https://esm.sh/jsdom"
}, },
"compilerOptions": { "compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "preact" },
"jsx": "react-jsx", "exclude": ["**/_fresh/*"]
"jsxImportSource": "preact"
}
} }

View File

@ -12,6 +12,7 @@ type status = "running" | "stopped";
class ServerState { class ServerState {
private _status: status = "stopped"; private _status: status = "stopped";
private pid?: number;
public get status() { public get status() {
return this._status; return this._status;
} }
@ -65,16 +66,20 @@ class ServerState {
this._serverVersion = conf.version; this._serverVersion = conf.version;
this._eulaAccepted = checkEULA(); this._eulaAccepted = checkEULA();
}
private configureSockpuppet(): Promise<boolean> {
return new Promise((res) => {
this.sockpuppet = new Sockpuppet( this.sockpuppet = new Sockpuppet(
"ws://sockpuppet.cyborggrizzly.com", "ws://sockpuppet.cyborggrizzly.com",
() => { async () => {
this.sockpuppet.createChannel(this._channelId); await this.sockpuppet.createChannel(this._channelId);
res(true);
this.sockpuppet.joinChannel(this.channelId, async (msg) => { this.sockpuppet.joinChannel(this.channelId, async (msg) => {
if (msg === "log" && !IS_BROWSER) { if (msg === "log" && !IS_BROWSER) {
try { try {
const log = await Deno.readTextFile("./server/logs/latest.log"); const log = await Deno.readTextFile("./server/logs/latest.log");
this.channel?.send(log); this.sendMessageToChannel(log);
} catch { } catch {
1; 1;
} }
@ -85,6 +90,7 @@ class ServerState {
}); });
}, },
); );
});
} }
public get channel() { public get channel() {
@ -103,9 +109,12 @@ class ServerState {
public async startMCServer(instance = "server") { public async startMCServer(instance = "server") {
this.command = getServerStartCommands(this._serverType, instance); this.command = getServerStartCommands(this._serverType, instance);
this.channel?.send("clear"); await this.configureSockpuppet();
this.sendMessageToChannel("clear");
this.process = this.command.spawn(); this.process = this.command.spawn();
this.pid = this.process.pid;
const { readable, writable } = new TransformStream(); const { readable, writable } = new TransformStream();
readable.pipeTo(this.process.stdin); readable.pipeTo(this.process.stdin);
@ -119,26 +128,10 @@ class ServerState {
} }
private async startStream() { private async startStream() {
// const stream = this.process.stdout.getReader();
// const decoder = new TextDecoder();
// while (true) {
// const { done, value } = await stream.read();
// if (value) {
// const line = decoder.decode(value);
// this.channel?.send(line);
// const stdoutMsg = new CustomEvent("stdoutmsg", {
// detail: line,
// });
// globalThis.dispatchEvent(stdoutMsg);
// }
// if (done) break;
// }
await this.process.stdout.pipeThrough(new TextDecoderStream()).pipeTo( await this.process.stdout.pipeThrough(new TextDecoderStream()).pipeTo(
new WritableStream({ new WritableStream({
write: (chunk) => { write: (chunk) => {
this.channel?.send(chunk); this.sendMessageToChannel(chunk);
const stdoutMsg = new CustomEvent("stdoutmsg", { const stdoutMsg = new CustomEvent("stdoutmsg", {
detail: chunk, detail: chunk,
}); });
@ -153,9 +146,10 @@ class ServerState {
this.sendStdIn("stop"); this.sendStdIn("stop");
} }
public forceStopMCServer() { public async forceStopMCServer() {
this.status = "stopped"; this.status = "stopped";
this.process.kill(); this.process.kill();
await Deno.remove('./server/session.lock');
} }
public async restartMCServer() { public async restartMCServer() {
@ -166,10 +160,7 @@ class ServerState {
}); });
globalThis.dispatchEvent(statusEvent); globalThis.dispatchEvent(statusEvent);
// while (true) {
await this.process.status; await this.process.status;
// }
} }
this.startMCServer(); this.startMCServer();
@ -179,6 +170,21 @@ class ServerState {
this._eulaAccepted = true; this._eulaAccepted = true;
acceptEULA(); acceptEULA();
} }
private async sendMessageToChannel(msg: string, retries = 0) {
if (retries > 4) return console.log('Unable to reconnect to socket after 5 tries. Is the puppet server ok?')
try {
this.channel?.send(msg);
} catch (e) {
console.log(e);
console.log(
"Error sending message via socket, likely closed. Reconnecting",
);
await this.configureSockpuppet();
await this.sendMessageToChannel(msg, retries + 1);
}
}
} }
export const SERVER_STATE = new ServerState(); export const SERVER_STATE = new ServerState();