perf: timeout

pull/21/merge
xiaojunnuo 2023-07-03 11:16:46 +08:00
parent 91be6826b9
commit 3eeb1f77aa
12 changed files with 99 additions and 30 deletions

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",

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);
}
}

View File

@ -66,6 +66,7 @@ export type Runnable = {
strategy?: RunnableStrategy;
runnableType?: string; // pipeline, stage, task , step
status?: HistoryResult;
timeout?: number;
default?: {
[key: string]: any;
};

View File

@ -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);
});
}

View File

@ -2,9 +2,14 @@
"name": "@certd/plugin-aliyun",
"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",
"build1": "vue-tsc --noEmit && vite build",

View File

@ -2,9 +2,14 @@
"name": "@certd/plugin-all",
"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",
"build2": " vite build",

View File

@ -2,9 +2,14 @@
"name": "@certd/plugin-cert",
"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",

View File

@ -2,9 +2,14 @@
"name": "@certd/plugin-host",
"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",

View File

@ -2,9 +2,14 @@
"name": "@certd/plugin-huawei",
"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",

View File

@ -2,9 +2,14 @@
"name": "@certd/plugin-tencent",
"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",

View File

@ -2,9 +2,14 @@
"name": "@certd/plugin-util",
"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",

View File

@ -221,14 +221,21 @@ export class PipelineService extends BaseService<PipelineEntity> {
fileRootDir: this.certdConfig.fileRootDir,
});
try {
runningTasks.set(id, executor);
runningTasks.set(historyId, executor);
await executor.init();
await executor.run(historyId, triggerType);
} catch (e) {
logger.error('执行失败:', e);
throw e;
} finally {
runningTasks.delete(id);
runningTasks.delete(historyId);
}
}
async cancel(historyId: number) {
const executor = runningTasks.get(historyId);
if (executor) {
executor.cancel();
}
}