From 89b8a7b85e6da219c99fe76ae14403cdb3fb0881 Mon Sep 17 00:00:00 2001 From: unitwk Date: Sun, 23 Oct 2022 11:48:52 +0800 Subject: [PATCH] Feat: add HiPer download task --- src/service/async_task_service/hiper_start.ts | 93 +++++++++++++++++++ src/service/async_task_service/index.ts | 2 + .../async_task_service/quick_install.ts | 2 + src/service/download.ts | 23 +++++ 4 files changed, 120 insertions(+) create mode 100644 src/service/async_task_service/hiper_start.ts create mode 100644 src/service/download.ts diff --git a/src/service/async_task_service/hiper_start.ts b/src/service/async_task_service/hiper_start.ts new file mode 100644 index 0000000..796436b --- /dev/null +++ b/src/service/async_task_service/hiper_start.ts @@ -0,0 +1,93 @@ +import { ChildProcess } from "child_process"; +// Copyright (C) 2022 MCSManager + +import { v4 } from "uuid"; +import fs from "fs-extra"; +import Instance from "../../entity/instance/instance"; +import InstanceSubsystem from "../system_instance"; +import InstanceConfig from "../../entity/instance/Instance_config"; +import { $t, i18next } from "../../i18n"; +import path from "path"; + +import { getFileManager } from "../file_router_service"; +import EventEmitter from "events"; +import { IAsyncTask, IAsyncTaskJSON, TaskCenter } from "./index"; +import logger from "../log"; +import { downloadFileToLocalFile } from "../download"; + +// singleton pattern + +export class HiPer { + public static subProcess: ChildProcess; + + public static openHiPer(keyPath: string) {} + + public static stopHiPer() {} +} + +export class HiPerTask extends EventEmitter implements IAsyncTask { + public taskId: string; + public instance: Instance; + + public readonly KEY_YML = path.normalize(path.join(process.cwd(), "lib", "hiper", "key.yml")); + public readonly POINT_YML = path.normalize(path.join(process.cwd(), "lib", "hiper", "point.yml")); + private readonly BASE_URL = "https://cert.mcer.cn"; + + private keyYmlPath: string; + private pointYmlPath: string; + private _status = 0; // 0=stop 1=running -1=error 2=downloading + + constructor(public readonly instanceUuid: string, public readonly indexCode: string) { + super(); + this.taskId = `HiPerTask-${instanceUuid}-${v4()}`; + } + + async start() { + this._status = 1; + this.emit("started"); + try { + // Download hiper key.yml + await downloadFileToLocalFile(`${this.BASE_URL}/${this.indexCode}.yml`, this.KEY_YML); + + // Download hiper point.yml + await downloadFileToLocalFile(`${this.BASE_URL}/point.yml`, this.KEY_YML); + + // TODO: The node information in point.yml is overwritten to key.yml + // fs.writeFile() + + // Start Command: hiper.exe -config .\key.yml + HiPer.openHiPer(this.keyYmlPath); + } catch (error) { + logger.error("HiPer Task Error:", error); + this.emit("failure"); + } finally { + this.stop(); + } + } + + async stop() { + this._status = 0; + this.emit("stopped"); + } + + failure() { + this._status = -1; + this.emit("failure"); + } + + status(): number { + return this._status; + } + + toObject(): IAsyncTaskJSON { + return JSON.parse( + JSON.stringify({ + taskId: this.taskId, + status: this.status(), + instanceUuid: this.instance.instanceUuid, + instanceStatus: this.instance.status(), + instanceConfig: this.instance.config + }) + ); + } +} diff --git a/src/service/async_task_service/index.ts b/src/service/async_task_service/index.ts index 6815e47..082264d 100644 --- a/src/service/async_task_service/index.ts +++ b/src/service/async_task_service/index.ts @@ -1,3 +1,5 @@ +// Copyright (C) 2022 MCSManager + import EventEmitter from "events"; import logger from "../log"; diff --git a/src/service/async_task_service/quick_install.ts b/src/service/async_task_service/quick_install.ts index 0e78e17..4f5556d 100644 --- a/src/service/async_task_service/quick_install.ts +++ b/src/service/async_task_service/quick_install.ts @@ -1,3 +1,5 @@ +// Copyright (C) 2022 MCSManager + import { v4 } from "uuid"; import axios from "axios"; import { pipeline, Readable } from "stream"; diff --git a/src/service/download.ts b/src/service/download.ts new file mode 100644 index 0000000..f08150b --- /dev/null +++ b/src/service/download.ts @@ -0,0 +1,23 @@ +// Copyright (C) 2022 MCSManager + +import path from "path"; +import fs from "fs-extra"; +import axios from "axios"; +import { pipeline, Readable } from "stream"; + +export function downloadFileToLocalFile(url: string, localFilePath: string): Promise { + return new Promise(async (resolve, reject) => { + const writeStream = fs.createWriteStream(path.normalize(localFilePath)); + const response = await axios({ + url, + responseType: "stream" + }); + pipeline(response.data, writeStream, (err) => { + if (err) { + reject(err); + } else { + resolve(true); + } + }); + }); +}