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 // Start HiPer Network
if (taskName === "hiper") { if (taskName === "hiper") {
const indexCode = String(parameter.indexCode); const indexCode = String(parameter.indexCode);
openHiPerTask(indexCode); const task = openHiPerTask(indexCode);
return protocol.response(ctx, task.toObject());
} }
protocol.response(ctx, true); protocol.response(ctx, true);
}); });

View File

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

View File

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