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", "name": "@certd/pipeline",
"private": false, "private": false,
"version": "1.1.3", "version": "1.1.3",
"main": "./src/index.ts",
"module": "./src/index.ts",
"types": "./src/index.ts",
"publishConfig": {
"main": "./dist/bundle.js", "main": "./dist/bundle.js",
"module": "./dist/bundle.mjs", "module": "./dist/bundle.mjs",
"types": "./dist/d/index.d.ts", "types": "./dist/d/index.d.ts"
},
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",
"build": "rollup -c", "build": "rollup -c",

View File

@ -12,6 +12,7 @@ import { RegistryItem } from "../registry";
import { Decorator } from "../decorator"; import { Decorator } from "../decorator";
import { IEmailService } from "../service"; import { IEmailService } from "../service";
import { FileStore } from "./file-store"; import { FileStore } from "./file-store";
import { TimeoutPromise } from "../utils/util.promise";
export type ExecutorOptions = { export type ExecutorOptions = {
userId: any; userId: any;
@ -31,6 +32,7 @@ export class Executor {
lastStatusMap!: RunnableCollection; lastStatusMap!: RunnableCollection;
lastRuntime!: RunHistory; lastRuntime!: RunHistory;
options: ExecutorOptions; options: ExecutorOptions;
canceled = false;
onChanged: (history: RunHistory) => void; onChanged: (history: RunHistory) => void;
constructor(options: ExecutorOptions) { constructor(options: ExecutorOptions) {
this.options = options; this.options = options;
@ -50,7 +52,9 @@ export class Executor {
this.lastStatusMap = new RunnableCollection(lastRuntime?.pipeline); this.lastStatusMap = new RunnableCollection(lastRuntime?.pipeline);
} }
async cancel() {} cancel() {
this.canceled = true;
}
async run(runtimeId: any = 0, triggerType: string) { async run(runtimeId: any = 0, triggerType: string) {
try { try {
@ -101,11 +105,15 @@ export class Executor {
return ResultType.skip; return ResultType.skip;
} }
} }
const timer = setInterval(async () => { const intervalFlushLogId = setInterval(async () => {
await this.onChanged(this.runtime); await this.onChanged(this.runtime);
}, 10000); }, 10000);
const timeout = runnable.timeout ?? 20 * 60 * 1000;
try { try {
await run(); if (this.canceled) {
throw new Error("task canceled");
}
await TimeoutPromise(run, timeout);
this.runtime.success(runnable); this.runtime.success(runnable);
return ResultType.success; return ResultType.success;
} catch (e: any) { } catch (e: any) {
@ -113,7 +121,7 @@ export class Executor {
throw e; throw e;
} finally { } finally {
this.runtime.finally(runnable); this.runtime.finally(runnable);
clearInterval(timer); clearInterval(intervalFlushLogId);
await this.onChanged(this.runtime); await this.onChanged(this.runtime);
} }
} }

View File

@ -66,6 +66,7 @@ export type Runnable = {
strategy?: RunnableStrategy; strategy?: RunnableStrategy;
runnableType?: string; // pipeline, stage, task , step runnableType?: string; // pipeline, stage, task , step
status?: HistoryResult; status?: HistoryResult;
timeout?: number;
default?: { default?: {
[key: string]: any; [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", "name": "@certd/plugin-aliyun",
"private": false, "private": false,
"version": "1.1.3", "version": "1.1.3",
"main": "./src/index.ts",
"module": "./src/index.ts",
"types": "./src/index.ts",
"publishConfig": {
"main": "./dist/bundle.js", "main": "./dist/bundle.js",
"module": "./dist/bundle.mjs", "module": "./dist/bundle.mjs",
"types": "./dist/d/index.d.ts", "types": "./dist/d/index.d.ts"
},
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",
"build1": "vue-tsc --noEmit && vite build", "build1": "vue-tsc --noEmit && vite build",

View File

@ -2,9 +2,14 @@
"name": "@certd/plugin-all", "name": "@certd/plugin-all",
"private": false, "private": false,
"version": "1.1.3", "version": "1.1.3",
"main": "./src/index.ts",
"module": "./src/index.ts",
"types": "./src/index.ts",
"publishConfig": {
"main": "./dist/bundle.js", "main": "./dist/bundle.js",
"module": "./dist/bundle.mjs", "module": "./dist/bundle.mjs",
"types": "./dist/d/index.d.ts", "types": "./dist/d/index.d.ts"
},
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",
"build2": " vite build", "build2": " vite build",

View File

@ -2,9 +2,14 @@
"name": "@certd/plugin-cert", "name": "@certd/plugin-cert",
"private": false, "private": false,
"version": "1.1.3", "version": "1.1.3",
"main": "./src/index.ts",
"module": "./src/index.ts",
"types": "./src/index.ts",
"publishConfig": {
"main": "./dist/bundle.js", "main": "./dist/bundle.js",
"module": "./dist/bundle.mjs", "module": "./dist/bundle.mjs",
"types": "./dist/d/index.d.ts", "types": "./dist/d/index.d.ts"
},
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",
"build": "rollup -c", "build": "rollup -c",

View File

@ -2,9 +2,14 @@
"name": "@certd/plugin-host", "name": "@certd/plugin-host",
"private": false, "private": false,
"version": "1.1.3", "version": "1.1.3",
"main": "./src/index.ts",
"module": "./src/index.ts",
"types": "./src/index.ts",
"publishConfig": {
"main": "./dist/bundle.js", "main": "./dist/bundle.js",
"module": "./dist/bundle.mjs", "module": "./dist/bundle.mjs",
"types": "./dist/d/index.d.ts", "types": "./dist/d/index.d.ts"
},
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",
"build": "rollup -c", "build": "rollup -c",

View File

@ -2,9 +2,14 @@
"name": "@certd/plugin-huawei", "name": "@certd/plugin-huawei",
"private": false, "private": false,
"version": "1.1.3", "version": "1.1.3",
"main": "./src/index.ts",
"module": "./src/index.ts",
"types": "./src/index.ts",
"publishConfig": {
"main": "./dist/bundle.js", "main": "./dist/bundle.js",
"module": "./dist/bundle.mjs", "module": "./dist/bundle.mjs",
"types": "./dist/d/index.d.ts", "types": "./dist/d/index.d.ts"
},
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",
"build": "rollup -c", "build": "rollup -c",

View File

@ -2,9 +2,14 @@
"name": "@certd/plugin-tencent", "name": "@certd/plugin-tencent",
"private": false, "private": false,
"version": "1.1.3", "version": "1.1.3",
"main": "./src/index.ts",
"module": "./src/index.ts",
"types": "./src/index.ts",
"publishConfig": {
"main": "./dist/bundle.js", "main": "./dist/bundle.js",
"module": "./dist/bundle.mjs", "module": "./dist/bundle.mjs",
"types": "./dist/d/index.d.ts", "types": "./dist/d/index.d.ts"
},
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",
"build": "rollup -c", "build": "rollup -c",

View File

@ -2,9 +2,14 @@
"name": "@certd/plugin-util", "name": "@certd/plugin-util",
"private": false, "private": false,
"version": "1.1.3", "version": "1.1.3",
"main": "./src/index.ts",
"module": "./src/index.ts",
"types": "./src/index.ts",
"publishConfig": {
"main": "./dist/bundle.js", "main": "./dist/bundle.js",
"module": "./dist/bundle.mjs", "module": "./dist/bundle.mjs",
"types": "./dist/d/index.d.ts", "types": "./dist/d/index.d.ts"
},
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",
"build": "rollup -c", "build": "rollup -c",

View File

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