chore: 修复pipelineid为空被注册任务

pull/148/head
xiaojunnuo 2024-08-05 15:08:24 +08:00
parent 3f0a10007c
commit 2182dce07c
1 changed files with 15 additions and 4 deletions

View File

@ -73,6 +73,7 @@ export class PipelineService extends BaseService<PipelineEntity> {
const info = await this.info(pipelineId); const info = await this.info(pipelineId);
if (info && !info.disabled) { if (info && !info.disabled) {
const pipeline = JSON.parse(info.content); const pipeline = JSON.parse(info.content);
// 手动触发不要await
this.registerTriggers(pipeline); this.registerTriggers(pipeline);
} }
} }
@ -173,7 +174,7 @@ export class PipelineService extends BaseService<PipelineEntity> {
if (immediateTriggerOnce) { if (immediateTriggerOnce) {
await this.trigger(pipeline.id); await this.trigger(pipeline.id);
await sleep(1000); await sleep(200);
} }
} }
@ -224,6 +225,11 @@ export class PipelineService extends BaseService<PipelineEntity> {
} }
registerCron(pipelineId, trigger) { registerCron(pipelineId, trigger) {
if (pipelineId == null) {
logger.warn('pipelineId为空无法注册定时任务');
return;
}
let cron = trigger.props?.cron; let cron = trigger.props?.cron;
if (cron == null) { if (cron == null) {
return; return;
@ -232,15 +238,20 @@ export class PipelineService extends BaseService<PipelineEntity> {
if (cron.startsWith('*')) { if (cron.startsWith('*')) {
cron = '0' + cron.substring(1, cron.length); cron = '0' + cron.substring(1, cron.length);
} }
const name = this.buildCronKey(pipelineId, trigger.id); const triggerId = trigger.id;
const name = this.buildCronKey(pipelineId, triggerId);
this.cron.remove(name); this.cron.remove(name);
this.cron.register({ this.cron.register({
name, name,
cron, cron,
job: async () => { job: async () => {
logger.info('定时任务触发:', pipelineId, trigger.id); logger.info('定时任务触发:', pipelineId, triggerId);
if (pipelineId == null) {
logger.warn('pipelineId为空,无法执行');
return;
}
try { try {
await this.run(pipelineId, trigger.id); await this.run(pipelineId, triggerId);
} catch (e) { } catch (e) {
logger.error('定时job执行失败', e); logger.error('定时job执行失败', e);
} }