basic docker config, fixes several critical faults, allows status manager to get status via sse

This commit is contained in:
2023-10-07 12:19:43 -06:00
parent 2487529aaf
commit 6944cbb9f7
10 changed files with 172 additions and 45 deletions

View File

@@ -4,7 +4,7 @@ import { Loader } from "../types/mcgrizzconf.ts";
import { getConfFile, updateConfFile } from "../util/confFile.ts";
import { IS_BROWSER } from "$fresh/runtime.ts";
type MCServerEvent = 'message';
type MCServerEvent = "message";
type MCServerEventCallback = (msg: string) => void;
class ServerState {
@@ -19,21 +19,21 @@ class ServerState {
private _eulaAccepted = false;
private sockpuppet!: Sockpuppet;
private _channelId = "blanaba";
private _channelId;
public get channelId() {
return this._channelId;
}
public get eulaAccepted() {
return this._eulaAccepted;
}
private stdin!: WritableStreamDefaultWriter;
private stdin!: WritableStreamDefaultWriter;
private _serverType: Loader = 'unset';
private _serverType: Loader = "unset";
public get serverType(): Loader {
return this._serverType;
}
public set serverType(loader: Loader) {
updateConfFile({loader});
updateConfFile({ loader });
this._serverType = loader;
}
@@ -42,26 +42,35 @@ class ServerState {
return this._serverVersion;
}
public set serverVersion(version: string) {
updateConfFile({version});
updateConfFile({ version });
this._serverVersion = version;
}
constructor() {
this._channelId = crypto.randomUUID();
const conf = getConfFile();
this._serverType = conf.loader;
this._serverVersion = conf.version;
// if (this.serverType !== 'unset') this._eulaAccepted = checkEULA();
this._eulaAccepted = checkEULA();
this.sockpuppet = new Sockpuppet(
"ws://sockpuppet.cyborggrizzly.com",
() => {
this.sockpuppet.createChannel(this._channelId);
this.sockpuppet.joinChannel(this.channelId, async (msg) => {
if (msg === 'log' && !IS_BROWSER) {
const log = await Deno.readTextFile('./server/logs/latest.log');
this.channel?.send(log);
} else
this.sendStdIn(msg);
if (msg === "log" && !IS_BROWSER) {
try {
const log = await Deno.readTextFile("./server/logs/latest.log");
this.channel?.send(log);
} catch {
1;
}
} else {
console.log(msg)
this.sendStdIn(msg);
}
});
},
);
@@ -79,7 +88,6 @@ class ServerState {
await this.stdin.write(msg);
}
// "instance" should be moved to a private member once multi-instance support is implemented
public startMCServer(instance = "server") {
this.command = new Deno.Command("java", {
@@ -94,14 +102,23 @@ class ServerState {
stdout: "piped",
});
this.channel?.send('clear');
this.process = this.command.spawn();
const {readable, writable} = new TransformStream();
const { readable, writable } = new TransformStream();
readable.pipeTo(this.process.stdin);
this.stdin = writable.getWriter();
this._status = "running";
const statusEvent = new CustomEvent('serverstatuschange', {detail: this._status});
globalThis.dispatchEvent(statusEvent);
this.startStream();
// this.process.status.then(() => {
// this._status = "stopped";
// });
}
private async startStream() {
@@ -120,25 +137,54 @@ class ServerState {
}
if (done) break;
}
// await this.process.stdout.pipeThrough(new TextDecoderStream()).pipeTo(
// new WritableStream({
// write: (chunk) => {
// this.channel?.send(chunk);
// const stdoutMsg = new CustomEvent("stdoutmsg", {
// detail: chunk,
// });
// globalThis.dispatchEvent(stdoutMsg);
// },
// }),
// );
}
public gracefullyStopMCServer() {
this._status = "stopped";
this.sendStdIn("stop");
const statusEvent = new CustomEvent('serverstatuschange', {detail: this._status});
globalThis.dispatchEvent(statusEvent);
}
public forceStopMCServer() {
this._status = "stopped";
this.process.kill();
const statusEvent = new CustomEvent('serverstatuschange', {detail: this._status});
globalThis.dispatchEvent(statusEvent);
}
public async restartMCServer() {
if (this.status === "running") {
await this.sendStdIn("stop");
const statusEvent = new CustomEvent('serverstatuschange', {detail: 'restarting'});
globalThis.dispatchEvent(statusEvent);
// while (true) {
await this.process.status;
// }
}
const statusEvent = new CustomEvent('serverstatuschange', {detail: this._status});
globalThis.dispatchEvent(statusEvent);
this.startMCServer();
}