Feat: zip json to instance config

This commit is contained in:
unitwk 2022-10-22 17:00:43 +08:00
parent 7d7323a66c
commit 4cab2d04c3
3 changed files with 31 additions and 15 deletions

View File

@ -252,7 +252,7 @@ export default class Instance extends EventEmitter {
// Release resources (mainly release process-related resources)
releaseResources() {
this.process.destroy();
if (this.process) this.process.destroy();
this.process = null;
}

View File

@ -329,13 +329,26 @@ routerApp.on("instance/stop_asynchronous", (ctx, data) => {
// Query async task status
routerApp.on("instance/query_asynchronous", (ctx, data) => {
const taskId = String(data.parameter.taskId);
const task = TaskCenter.getTask(taskId);
protocol.response(ctx, {
taskId,
status: task.status(),
detail: task.toObject()
});
const taskId = data.parameter.taskId as string | undefined;
if (!taskId) {
const result = [];
for (const task of TaskCenter.tasks) {
result.push({
taskId: task.taskId,
status: task.status(),
detail: task.toObject()
});
}
protocol.response(ctx, result);
} else {
const task = TaskCenter.getTask(String(taskId));
if (task)
protocol.response(ctx, {
taskId: task.taskId,
status: task.status(),
detail: task.toObject()
});
}
});
routerApp.on("instance/process_config/list", (ctx, data) => {

View File

@ -10,13 +10,16 @@ import path from "path";
import { getFileManager } from "../file_router_service";
import EventEmitter from "events";
import { IAsyncTask, IAsyncTaskJSON, TaskCenter } from "./index";
import logger from "../log";
export class QuickInstallTask extends EventEmitter implements IAsyncTask {
private _status = 0; // 0=stop 1=running -1=error 2=downloading
public taskId: string;
private instance: Instance;
private readonly TMP_ZIP_NAME = "mcsm_install_package.zip";
private zipPath = "";
public instance: Instance;
public readonly TMP_ZIP_NAME = "mcsm_install_package.zip";
public readonly ZIP_CONFIG_JSON = "mcsmanager-config.json";
public zipPath = "";
private _status = 0; // 0=stop 1=running -1=error 2=downloading
private downloadStream: fs.WriteStream = null;
constructor(public instanceName: string, public targetLink: string) {
@ -56,11 +59,11 @@ export class QuickInstallTask extends EventEmitter implements IAsyncTask {
let result = await this.download();
result = await fileManager.promiseUnzip(this.TMP_ZIP_NAME, ".", "UTF-8");
if (!result) throw new Error($t("quick_install.unzipError"));
// TODO mcsm-config.json reader
console.log("OK!!!!");
const config = JSON.parse(await fileManager.readFile(this.ZIP_CONFIG_JSON));
this.instance.parameters(config);
this.stop();
} catch (error) {
console.log("Task error:", error);
logger.error("QuickInstall Task Error:", error);
this.emit("failure");
} finally {
fs.remove(fileManager.toAbsolutePath(this.TMP_ZIP_NAME), () => {});