feat: 新增stopRoutine执行函数

This commit is contained in:
QCQCQC@Debian 2025-12-10 10:14:35 +08:00
parent 3e182c6769
commit bf6416abb0
3 changed files with 69 additions and 0 deletions

1
lib/AppLoader.d.ts vendored
View File

@ -62,5 +62,6 @@ export declare class AppLoader<ED extends EntityDict & BaseEntityDict, Cxt exten
protected execFreeTimer(timer: FreeTimer<ED, Cxt>, context: Cxt): Promise<OperationResult<ED>> | undefined;
startTimers(): void;
execStartRoutines(): Promise<void>;
execStopRoutines(): Promise<void>;
execRoutine(routine: <Cxt extends AsyncContext<ED>>(context: Cxt) => Promise<void>): Promise<void>;
}

View File

@ -543,6 +543,39 @@ class AppLoader extends types_1.AppLoader {
}
}
}
async execStopRoutines() {
const routines = this.requireSth('lib/routines/stop') || [];
for (const routine of routines) {
if (routine.hasOwnProperty('entity')) {
const start = Date.now();
try {
const result = await this.execWatcher(routine);
console.log(`例程【${routine.name}】执行成功,耗时${Date.now() - start}毫秒,结果是`, result);
}
catch (err) {
console.error(`例程【${routine.name}】执行失败,耗时${Date.now() - start}毫秒,错误是`, err);
throw err;
}
}
else {
const { name, routine: routineFn } = routine;
const context = await this.makeContext();
const start = Date.now();
try {
const result = await routineFn(context, {
socket: this.nsSocket,
});
console.log(`例程【${name}】执行成功,耗时${Date.now() - start}毫秒,结果是【${result}`);
await context.commit();
}
catch (err) {
console.error(`例程【${name}】执行失败,耗时${Date.now() - start}毫秒,错误是`, err);
await context.rollback();
throw err;
}
}
}
}
async execRoutine(routine) {
const context = await this.makeContext();
try {

View File

@ -637,6 +637,41 @@ export class AppLoader<ED extends EntityDict & BaseEntityDict, Cxt extends Backe
}
}
async execStopRoutines() {
const routines: Routine<ED, keyof ED, Cxt>[] = this.requireSth('lib/routines/stop') || [];
for (const routine of routines) {
if (routine.hasOwnProperty('entity')) {
const start = Date.now();
try {
const result = await this.execWatcher(routine as Watcher<ED, keyof ED, Cxt>);
console.log(`例程【${routine.name}】执行成功,耗时${Date.now() - start}毫秒,结果是`, result);
}
catch (err) {
console.error(`例程【${routine.name}】执行失败,耗时${Date.now() - start}毫秒,错误是`, err);
throw err;
}
}
else {
const { name, routine: routineFn } = routine as FreeRoutine<ED, Cxt>;
const context = await this.makeContext();
const start = Date.now();
try {
const result = await routineFn(context, {
socket: this.nsSocket!,
});
console.log(`例程【${name}】执行成功,耗时${Date.now() - start}毫秒,结果是【${result}`);
await context.commit();
}
catch (err) {
console.error(`例程【${name}】执行失败,耗时${Date.now() - start}毫秒,错误是`, err);
await context.rollback();
throw err;
}
}
}
}
async execRoutine(routine: <Cxt extends AsyncContext<ED>>(context: Cxt) => Promise<void>) {
const context = await this.makeContext();