perf: timeout

This commit is contained in:
xiaojunnuo
2023-07-03 11:16:46 +08:00
parent 91be6826b9
commit 3eeb1f77aa
12 changed files with 99 additions and 30 deletions
+8 -3
View File
@@ -2,9 +2,14 @@
"name": "@certd/pipeline",
"private": false,
"version": "1.1.3",
"main": "./dist/bundle.js",
"module": "./dist/bundle.mjs",
"types": "./dist/d/index.d.ts",
"main": "./src/index.ts",
"module": "./src/index.ts",
"types": "./src/index.ts",
"publishConfig": {
"main": "./dist/bundle.js",
"module": "./dist/bundle.mjs",
"types": "./dist/d/index.d.ts"
},
"scripts": {
"dev": "vite",
"build": "rollup -c",
+12 -4
View File
@@ -12,6 +12,7 @@ import { RegistryItem } from "../registry";
import { Decorator } from "../decorator";
import { IEmailService } from "../service";
import { FileStore } from "./file-store";
import { TimeoutPromise } from "../utils/util.promise";
export type ExecutorOptions = {
userId: any;
@@ -31,6 +32,7 @@ export class Executor {
lastStatusMap!: RunnableCollection;
lastRuntime!: RunHistory;
options: ExecutorOptions;
canceled = false;
onChanged: (history: RunHistory) => void;
constructor(options: ExecutorOptions) {
this.options = options;
@@ -50,7 +52,9 @@ export class Executor {
this.lastStatusMap = new RunnableCollection(lastRuntime?.pipeline);
}
async cancel() {}
cancel() {
this.canceled = true;
}
async run(runtimeId: any = 0, triggerType: string) {
try {
@@ -101,11 +105,15 @@ export class Executor {
return ResultType.skip;
}
}
const timer = setInterval(async () => {
const intervalFlushLogId = setInterval(async () => {
await this.onChanged(this.runtime);
}, 10000);
const timeout = runnable.timeout ?? 20 * 60 * 1000;
try {
await run();
if (this.canceled) {
throw new Error("task canceled");
}
await TimeoutPromise(run, timeout);
this.runtime.success(runnable);
return ResultType.success;
} catch (e: any) {
@@ -113,7 +121,7 @@ export class Executor {
throw e;
} finally {
this.runtime.finally(runnable);
clearInterval(timer);
clearInterval(intervalFlushLogId);
await this.onChanged(this.runtime);
}
}
@@ -66,6 +66,7 @@ export type Runnable = {
strategy?: RunnableStrategy;
runnableType?: string; // pipeline, stage, task , step
status?: HistoryResult;
timeout?: number;
default?: {
[key: string]: any;
};
@@ -0,0 +1,13 @@
export function TimeoutPromise(callback: () => Promise<void>, ms = 30 * 1000) {
let timeout: any;
return Promise.race([
callback(),
new Promise((resolve, reject) => {
timeout = setTimeout(() => {
reject(new Error(`Task timeout in ${ms} ms`));
}, ms);
}),
]).finally(() => {
clearTimeout(timeout);
});
}