ts-api-core
Version:
Nodejs api framework core
89 lines • 4.08 kB
JavaScript
;
/* eslint-disable no-plusplus */
/* eslint-disable no-await-in-loop */
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BackupServer = void 0;
const logger_1 = require("../base/logger");
const base_object_1 = require("../context/base.object");
const request_context_1 = require("../context/request.context");
const utils_1 = require("../utils/utils");
/** 后台服务基类 */
class BackupServer extends base_object_1.BaseObject {
/**
* 后台服务
* @param context 上下文对象
* @param name 任务名称
* @param delayTime 开始延时: ms
* @param intervalTime 间隔时长: ms
* @param executeTask 任务执行回调
* @param doStopTask 任务停止回调
*/
constructor(context, name, delayTime = 0, intervalTime = 1000, executeTask, doStopTask) {
super(context);
this.name = name;
this.delayTime = delayTime;
this.intervalTime = intervalTime;
this.executeTask = executeTask;
/** 任务已经执行次数 */
this.runCount = 0;
this.onStop = doStopTask !== null && doStopTask !== void 0 ? doStopTask : function () { return false; };
}
/** 获取默认的服务上下文对象 */
static getServerContext(options) {
var _a, _b;
class ServerContext extends request_context_1.RequestContext {
constructor(onLog) {
super();
this.onLog = onLog;
}
log(level, data) {
this.onLog(level, data);
}
}
const onLog = (_a = options === null || options === void 0 ? void 0 : options.onLog) !== null && _a !== void 0 ? _a : ((_b = options === null || options === void 0 ? void 0 : options.logger) !== null && _b !== void 0 ? _b : new logger_1.DefaultSaveLogger()).log;
return new ServerContext(onLog);
}
/** 运行服务, 注意:任务不停止,不会返回,所以不要使用 await 等着返回 */
start() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
if (this.delayTime > 0) {
yield utils_1.Utils.sleep(this.delayTime);
}
this.logW(`开始${this.name} (${(_a = utils_1.Utils.getIPAddress()) !== null && _a !== void 0 ? _a : '0.0.0.0'}, 间隔时长: ${this.intervalTime} ms )`);
let lastRun = 0;
this.runCount = 0;
while (this.onStop() === false) {
yield utils_1.Utils.sleep(1000);
if (utils_1.Utils.currentTimeMillis() - lastRun >= this.intervalTime) {
lastRun = utils_1.Utils.currentTimeMillis();
this.runCount++;
try {
yield this.runTask(this.runCount);
}
catch (e) {
this.logE(`执行第${this.runCount}次${this.name}出错:${e.message}`);
}
}
}
});
}
runTask(runRef) {
return __awaiter(this, void 0, void 0, function* () {
if (this.executeTask) {
yield this.executeTask(this);
}
});
}
}
exports.BackupServer = BackupServer;
//# sourceMappingURL=backup.server.js.map