forked from mirror/MCSM-Daemon
Feat: add HiPer download task
This commit is contained in:
parent
4cab2d04c3
commit
89b8a7b85e
93
src/service/async_task_service/hiper_start.ts
Normal file
93
src/service/async_task_service/hiper_start.ts
Normal file
@ -0,0 +1,93 @@
|
||||
import { ChildProcess } from "child_process";
|
||||
// Copyright (C) 2022 MCSManager <mcsmanager-dev@outlook.com>
|
||||
|
||||
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
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
// Copyright (C) 2022 MCSManager <mcsmanager-dev@outlook.com>
|
||||
|
||||
import EventEmitter from "events";
|
||||
import logger from "../log";
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
// Copyright (C) 2022 MCSManager <mcsmanager-dev@outlook.com>
|
||||
|
||||
import { v4 } from "uuid";
|
||||
import axios from "axios";
|
||||
import { pipeline, Readable } from "stream";
|
||||
|
23
src/service/download.ts
Normal file
23
src/service/download.ts
Normal file
@ -0,0 +1,23 @@
|
||||
// Copyright (C) 2022 MCSManager <mcsmanager-dev@outlook.com>
|
||||
|
||||
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<boolean> {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const writeStream = fs.createWriteStream(path.normalize(localFilePath));
|
||||
const response = await axios<Readable>({
|
||||
url,
|
||||
responseType: "stream"
|
||||
});
|
||||
pipeline(response.data, writeStream, (err) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue
Block a user