mirror of
https://github.com/certd/certd.git
synced 2026-04-30 17:37:24 +08:00
perf: 优化流水线执行时的状态保存性能
This commit is contained in:
@@ -23,6 +23,7 @@ export type ExecutorOptions = {
|
|||||||
pipeline: Pipeline;
|
pipeline: Pipeline;
|
||||||
storage: IStorage;
|
storage: IStorage;
|
||||||
onChanged: (history: RunHistory) => Promise<void>;
|
onChanged: (history: RunHistory) => Promise<void>;
|
||||||
|
onFinished: (history: RunHistory) => Promise<void>;
|
||||||
accessService: IAccessService;
|
accessService: IAccessService;
|
||||||
emailService: IEmailService;
|
emailService: IEmailService;
|
||||||
notificationService: INotificationService;
|
notificationService: INotificationService;
|
||||||
@@ -47,16 +48,19 @@ export class Executor {
|
|||||||
lastRuntime!: RunHistory;
|
lastRuntime!: RunHistory;
|
||||||
options: ExecutorOptions;
|
options: ExecutorOptions;
|
||||||
abort: AbortController = new AbortController();
|
abort: AbortController = new AbortController();
|
||||||
|
|
||||||
_inited = false;
|
_inited = false;
|
||||||
|
|
||||||
onChanged: (history: RunHistory) => Promise<void>;
|
onChanged: (history: RunHistory) => Promise<void>;
|
||||||
|
onFinished: (history: RunHistory) => Promise<void>;
|
||||||
constructor(options: ExecutorOptions) {
|
constructor(options: ExecutorOptions) {
|
||||||
this.options = options;
|
this.options = options;
|
||||||
this.pipeline = cloneDeep(options.pipeline);
|
this.pipeline = cloneDeep(options.pipeline);
|
||||||
this.onChanged = async (history: RunHistory) => {
|
this.onChanged = async (history: RunHistory) => {
|
||||||
await options.onChanged(history);
|
await options.onChanged(history);
|
||||||
};
|
};
|
||||||
|
this.onFinished = async (history: RunHistory) => {
|
||||||
|
await options.onFinished(history);
|
||||||
|
};
|
||||||
this.pipeline.userId = options.user.id;
|
this.pipeline.userId = options.user.id;
|
||||||
this.contextFactory = new ContextFactory(options.storage);
|
this.contextFactory = new ContextFactory(options.storage);
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
@@ -77,7 +81,7 @@ export class Executor {
|
|||||||
async cancel() {
|
async cancel() {
|
||||||
this.abort.abort();
|
this.abort.abort();
|
||||||
this.runtime?.cancel(this.pipeline);
|
this.runtime?.cancel(this.pipeline);
|
||||||
await this.onChanged(this.runtime);
|
await this.onFinished(this.runtime);
|
||||||
}
|
}
|
||||||
|
|
||||||
async run(runtimeId: any = 0, triggerType: string) {
|
async run(runtimeId: any = 0, triggerType: string) {
|
||||||
@@ -111,7 +115,7 @@ export class Executor {
|
|||||||
this.logger.error("pipeline 执行失败", e);
|
this.logger.error("pipeline 执行失败", e);
|
||||||
} finally {
|
} finally {
|
||||||
clearInterval(intervalFlushLogId);
|
clearInterval(intervalFlushLogId);
|
||||||
await this.onChanged(this.runtime);
|
await this.onFinished(this.runtime);
|
||||||
//保存之前移除logs
|
//保存之前移除logs
|
||||||
const lastRuntime: any = {
|
const lastRuntime: any = {
|
||||||
...this.runtime,
|
...this.runtime,
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ const development = {
|
|||||||
type: 'better-sqlite3',
|
type: 'better-sqlite3',
|
||||||
database: './data/db.sqlite',
|
database: './data/db.sqlite',
|
||||||
synchronize: false, // 如果第一次使用,不存在表,有同步的需求可以写 true
|
synchronize: false, // 如果第一次使用,不存在表,有同步的需求可以写 true
|
||||||
logging: true,
|
logging: false,
|
||||||
highlightSql: false,
|
highlightSql: false,
|
||||||
|
|
||||||
// 配置实体模型 或者 entities: '/entity',
|
// 配置实体模型 或者 entities: '/entity',
|
||||||
|
|||||||
@@ -675,7 +675,7 @@ export class PipelineService extends BaseService<PipelineEntity> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const onChanged = async (history: RunHistory) => {
|
const doSaveHistory = async (history: RunHistory) => {
|
||||||
//保存执行历史
|
//保存执行历史
|
||||||
try {
|
try {
|
||||||
logger.info("保存执行历史:", history.id);
|
logger.info("保存执行历史:", history.id);
|
||||||
@@ -690,6 +690,46 @@ export class PipelineService extends BaseService<PipelineEntity> {
|
|||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
class HistorySaver {
|
||||||
|
latest: RunHistory = null;
|
||||||
|
interval: any = null;
|
||||||
|
started: boolean = false;
|
||||||
|
async save(){
|
||||||
|
const latest = this.latest;
|
||||||
|
this.latest = null;
|
||||||
|
if (latest == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await doSaveHistory(latest);
|
||||||
|
}
|
||||||
|
|
||||||
|
async start(){
|
||||||
|
this.started = true
|
||||||
|
await this.save();
|
||||||
|
this.interval = setInterval(()=>{
|
||||||
|
this.save();
|
||||||
|
}, 1000 * 5);
|
||||||
|
}
|
||||||
|
async push(history: RunHistory){
|
||||||
|
this.latest = history;
|
||||||
|
if(!this.started){
|
||||||
|
await this.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async done(){
|
||||||
|
clearInterval(this.interval);
|
||||||
|
await this.save();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const historySaver = new HistorySaver();
|
||||||
|
const onChanged = async (history: RunHistory)=>{
|
||||||
|
await historySaver.push(history);
|
||||||
|
}
|
||||||
|
const onFinished = async (history: RunHistory)=>{
|
||||||
|
await onChanged(history);
|
||||||
|
await historySaver.done();
|
||||||
|
}
|
||||||
|
|
||||||
const userId = entity.userId;
|
const userId = entity.userId;
|
||||||
const projectId = entity.projectId;
|
const projectId = entity.projectId;
|
||||||
@@ -723,6 +763,7 @@ export class PipelineService extends BaseService<PipelineEntity> {
|
|||||||
user,
|
user,
|
||||||
pipeline,
|
pipeline,
|
||||||
onChanged,
|
onChanged,
|
||||||
|
onFinished,
|
||||||
accessService: accessGetter,
|
accessService: accessGetter,
|
||||||
cnameProxyService,
|
cnameProxyService,
|
||||||
pluginConfigService: this.pluginConfigGetter,
|
pluginConfigService: this.pluginConfigGetter,
|
||||||
|
|||||||
Reference in New Issue
Block a user