diff --git a/lib/AppLoader.d.ts b/lib/AppLoader.d.ts index 22d1038..94d2300 100644 --- a/lib/AppLoader.d.ts +++ b/lib/AppLoader.d.ts @@ -62,5 +62,6 @@ export declare class AppLoader, context: Cxt): Promise> | undefined; startTimers(): void; execStartRoutines(): Promise; + execStopRoutines(): Promise; execRoutine(routine: >(context: Cxt) => Promise): Promise; } diff --git a/lib/AppLoader.js b/lib/AppLoader.js index 066c4f7..0b0c6be 100644 --- a/lib/AppLoader.js +++ b/lib/AppLoader.js @@ -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 { diff --git a/src/AppLoader.ts b/src/AppLoader.ts index 9c90096..c880822 100644 --- a/src/AppLoader.ts +++ b/src/AppLoader.ts @@ -637,6 +637,41 @@ export class AppLoader[] = 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); + 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; + 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: >(context: Cxt) => Promise) { const context = await this.makeContext();