Feat: HiPer startup program is complete

This commit is contained in:
unitwk 2022-10-29 14:59:06 +08:00
parent 68ec56effb
commit c207d8fda7
4 changed files with 33 additions and 23 deletions

View File

@ -301,7 +301,8 @@ routerApp.on("instance/asynchronous", (ctx, data) => {
// Start HiPer Network
if (taskName === "hiper") {
const indexCode = String(parameter.indexCode);
openHiPerTask(indexCode);
const task = openHiPerTask(indexCode);
return protocol.response(ctx, task.toObject());
}
protocol.response(ctx, true);
});

View File

@ -26,8 +26,9 @@ class HiPer {
if (HiPer.hiperProcess) {
throw new Error($t("quick_install.hiperError"));
}
HiPer.hiperFilePath = path.normalize(path.join(process.cwd(), "lib", HiPer.hiperFileName));
HiPer.hiperProcess = spawn("hiper", ["-v", keyPath]);
HiPer.hiperFilePath = path.normalize(path.join(process.cwd(), "lib", "hiper", HiPer.hiperFileName));
// HiPer.hiperProcess = spawn("hiper", ["-v", keyPath]);
console.log("模拟启动成功");
}
public static stopHiPer() {
@ -58,24 +59,21 @@ export class HiPerTask extends AsyncTask {
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);
await downloadFileToLocalFile(`${this.BASE_URL}/point.yml`, this.POINT_YML);
// The node information in point.yml is overwritten to key.yml
let keyFile = fs.readFileSync(this.KEY_YML, "utf-8");
const pointFile = fs.readFileSync(this.POINT_YML, "utf-8");
const START_TEXT = ">>> AUTO SYNC AREA";
const END_TEXT = "<<< AUTO SYNC AREA";
const start = pointFile.indexOf(START_TEXT);
const end = pointFile.indexOf(END_TEXT);
if (start > -1 && end > -1) {
const nodesText = pointFile.slice(start, end);
const p1 = keyFile.indexOf(START_TEXT);
const p2 = keyFile.indexOf(END_TEXT);
if (p1 >= 0 || p2 >= 0) {
keyFile = keyFile.replace(keyFile.slice(p1, p2), "");
}
keyFile += "\n" + nodesText;
const p1 = keyFile.indexOf(START_TEXT);
const p2 = keyFile.indexOf(END_TEXT);
if (p1 >= 0 || p2 >= 0) {
keyFile = keyFile.replace(keyFile.slice(p1, p2), "");
}
keyFile += "\n\n" + pointFile;
fs.writeFileSync(this.KEY_YML, keyFile, "utf-8");
// Start Command: hiper.exe -config .\key.yml
HiPer.openHiPer(this.keyYmlPath);
@ -89,7 +87,7 @@ export class HiPerTask extends AsyncTask {
return null;
}
onError(): void {}
onError(err: Error): void {}
toObject(): IAsyncTaskJSON {
return JSON.parse(
@ -102,5 +100,7 @@ export class HiPerTask extends AsyncTask {
}
export function openHiPerTask(indexCode: string) {
TaskCenter.addTask(new HiPerTask(indexCode));
const task = new HiPerTask(indexCode);
TaskCenter.addTask(task);
return task;
}

View File

@ -28,15 +28,22 @@ export abstract class AsyncTask extends EventEmitter implements IAsyncTask {
protected _status = 0;
public start() {
return this.onStarted();
const r = this.onStarted();
this.emit("started");
return r;
}
public stop() {
const r = this.onStopped();
this.emit("stopped");
return this.onStopped();
return r;
}
public error(err: any) {
public error(err: Error) {
logger.error(`AsyncTask - ID: ${this.taskId} TYPE: ${this.type} Error:`, err);
this.onError(err);
this.emit("error", err);
this.stop();
}
@ -44,10 +51,10 @@ export abstract class AsyncTask extends EventEmitter implements IAsyncTask {
return this._status;
}
abstract onStarted(): Promise<boolean | void>;
abstract onStopped(): Promise<boolean | void>;
abstract onError(): void;
abstract toObject(): IAsyncTaskJSON;
public abstract onStarted(): Promise<boolean | void>;
public abstract onStopped(): Promise<boolean | void>;
public abstract onError(err: Error): void;
public abstract toObject(): IAsyncTaskJSON;
}
export class TaskCenter {

View File

@ -4,8 +4,10 @@ import path from "path";
import fs from "fs-extra";
import axios from "axios";
import { pipeline, Readable } from "stream";
import logger from "./log";
export function downloadFileToLocalFile(url: string, localFilePath: string): Promise<boolean> {
logger.info(`Request file: ${url} --> ${path.normalize(localFilePath)}`);
return new Promise(async (resolve, reject) => {
const writeStream = fs.createWriteStream(path.normalize(localFilePath));
const response = await axios<Readable>({