Refactor: daemon instance dispatcher

This commit is contained in:
unitwk 2023-11-15 17:28:54 +08:00
parent ecbe25b235
commit 3297bb7066
5 changed files with 36 additions and 27 deletions

View File

@ -1,9 +1,5 @@
import path from "path";
import fs from "fs-extra";
import Instance from "../instance/instance";
import InstanceCommand from "./base/command";
import RefreshPlayer from "./task/players";
import MinecraftGetPlayersCommand from "../minecraft/mc_getplayer";
import NullCommand from "./nullfunc";
import GeneralStartCommand from "./general/general_start";
import GeneralStopCommand from "./general/general_stop";
@ -12,14 +8,12 @@ import GeneralSendCommand from "./general/general_command";
import GeneralRestartCommand from "./general/general_restart";
import DockerStartCommand from "./docker/docker_start";
import TimeCheck from "./task/time";
import MinecraftBedrockGetPlayersCommand from "../minecraft/mc_getplayer_bedrock";
import GeneralUpdateCommand from "./general/general_update";
import PtyStartCommand from "./pty/pty_start";
import PtyStopCommand from "./pty/pty_stop";
import { PTY_PATH } from "../../const";
import OpenFrpTask from "./task/openfrp";
// instance function scheduler
// Instance function dispatcher
// Dispatch and assign different functions according to different types
export default class FunctionDispatcher extends InstanceCommand {
constructor() {
@ -41,6 +35,7 @@ export default class FunctionDispatcher extends InstanceCommand {
instance.setPreset("kill", new GeneralKillCommand());
instance.setPreset("restart", new GeneralRestartCommand());
instance.setPreset("update", new GeneralUpdateCommand());
instance.setPreset("getPlayer", new NullCommand());
// Preset the basic operation mode according to the instance startup type
if (!instance.config.processType || instance.config.processType === "general") {
@ -60,21 +55,22 @@ export default class FunctionDispatcher extends InstanceCommand {
}
// Whether to enable Docker PTY mode
if (instance.config.processType === "docker") {
instance.setPreset("resize", new NullCommand());
instance.setPreset("start", new DockerStartCommand());
instance.setPreset("resize", new NullCommand());
}
// Set different preset functions and functions according to different types
if (instance.config.type.includes(Instance.TYPE_UNIVERSAL)) {
instance.setPreset("getPlayer", new NullCommand());
}
if (instance.config.type.includes(Instance.TYPE_MINECRAFT_JAVA)) {
instance.setPreset("getPlayer", new MinecraftGetPlayersCommand());
instance.lifeCycleTaskManager.registerLifeCycleTask(new RefreshPlayer());
}
if (instance.config.type.includes(Instance.TYPE_MINECRAFT_BEDROCK)) {
instance.setPreset("getPlayer", new MinecraftBedrockGetPlayersCommand());
instance.lifeCycleTaskManager.registerLifeCycleTask(new RefreshPlayer());
}
// No suitable implementation solution found, not supported for the time being.
// if (instance.config.type.includes(Instance.TYPE_UNIVERSAL)) {
// instance.setPreset("getPlayer", new NullCommand());
// }
// if (instance.config.type.includes(Instance.TYPE_MINECRAFT_JAVA)) {
// instance.setPreset("getPlayer", new MinecraftGetPlayersCommand());
// instance.lifeCycleTaskManager.registerLifeCycleTask(new RefreshPlayer());
// }
// if (instance.config.type.includes(Instance.TYPE_MINECRAFT_BEDROCK)) {
// instance.setPreset("getPlayer", new MinecraftBedrockGetPlayersCommand());
// instance.lifeCycleTaskManager.registerLifeCycleTask(new RefreshPlayer());
// }
}
}

View File

@ -4,10 +4,10 @@ import InstanceCommand from "../commands/base/command";
export default class MinecraftUpdateCommand extends InstanceCommand {
constructor() {
super("UpdateCommand");
super("MinecraftUpdateCommand");
}
async exec(instance: Instance) {
console.log($t("TXT_CODE_mc_update.updateInstance"));
// Not supported yet
}
}

View File

@ -9,7 +9,7 @@ import type { DefaultEventsMap } from "@socket.io/component-emitter";
import type { InstanceDetail } from "@/types";
import { Terminal } from "xterm";
import { FitAddon } from "xterm-addon-fit";
import { useScreen } from "./useScreen";
import { INSTANCE_STATUS_CODE } from "@/types/const";
export const TERM_COLOR = {
TERM_RESET: "\x1B[0m",
@ -140,8 +140,10 @@ export function useTerminal() {
},
allowProposedApi: true,
rendererType: "canvas",
// The backend needs to be consistent.
// See "/daemon/src/entity/instance/Instance_config.ts"
rows: 40,
cols: 160
cols: 140
});
const fitAddon = new FitAddon();
// term.loadAddon(fitAddon);
@ -191,8 +193,10 @@ export function useTerminal() {
socket?.close();
});
const isRunning = computed(() => state?.value?.status === 3);
const isStopped = computed(() => state?.value?.status === 0);
const isStopped = computed(() =>
[INSTANCE_STATUS_CODE.STOPPED, INSTANCE_STATUS_CODE.UNKNOWN].includes(state?.value?.status ?? 0)
);
const isRunning = computed(() => !isStopped.value);
return {
events,

View File

@ -21,6 +21,14 @@ export const INSTANCE_STATUS = {
"3": t("TXT_CODE_f912fadc")
};
export enum INSTANCE_STATUS_CODE {
UNKNOWN = -1,
STOPPED = 0,
STOPPING = 1,
STARTING = 2,
RUNNING = 3
}
export const defaultDockerFile = `FROM ubuntu:latest\nRUN mkdir -p /workspace\nWORKDIR /workspace\n`;
export const openjdk8 = `FROM openjdk:8-jre

View File

@ -30,6 +30,7 @@ import type { InstanceMoreDetail } from "../hooks/useInstance";
import { useInstanceMoreDetail } from "../hooks/useInstance";
import { throttle } from "lodash";
import { useScreen } from "@/hooks/useScreen";
import { parseTimestamp } from "../tools/time";
defineProps<{
card: LayoutCard;
@ -425,11 +426,11 @@ onMounted(async () => {
</div>
<div>
{{ t("TXT_CODE_d31a684c") }}
{{ item.config.lastDatetime }}
{{ parseTimestamp(item.config.lastDatetime) }}
</div>
<div>
{{ t("TXT_CODE_ae747cc0") }}
{{ item.config.endTime }}
{{ parseTimestamp(item.config.endTime) }}
</div>
</a-typography-paragraph>
</template>