DolphinScheduler定期清除工作流

背景

由于工作流实例以及任务实例过多,准备只保留两天的任务记录

需要的表

t_ds_process_instance 工作流实例表
t_ds_task_instance  任务实例表
t_ds_relation_process_instance  工作流和任务实例关联表  保存着工作流和任务实例id

1.2 sql编写

创建工作流实例临时表保存两天的id

drop table if exists tmp_t_ds_process_instance_1;
create table tmp_t_ds_process_instance_1 as
SELECT id process_instance_id
FROM t_ds_process_instance
WHERE DATE(end_time) < CURDATE() - INTERVAL 2 DAY
union
select id process_instance_id
from t_ds_process_instance
where DATE(start_time) < CURDATE() - INTERVAL 2 DAY;

创建任务实例临时表保存两天的id

drop table if exists tmp_t_ds_process_instance_2;
create table tmp_t_ds_process_instance_2 as
select  
a.id as task_instance_id
from t_ds_task_instance a, tmp_t_ds_process_instance_1 b
where a.process_instance_id = b.process_instance_id;

保留两天工作流实例日志

DELETE  a from t_ds_process_instance a  where exists (
select 1 from tmp_t_ds_process_instance_1 b where a.id = b.process_instance_id
);

保留两天的任务实例日志

delete a from t_ds_task_instance a where exists (
select 1 from tmp_t_ds_process_instance_2 b where a.id = b.task_instance_id
);

删除关联的任务日志

delete a from t_ds_relation_process_instance a where exists ( select  1 from tmp_t_ds_process_instance_1 b where a.parent_process_instance_id = b.process_instance_id
);
delete a from t_ds_relation_process_instance a where exists ( select  1 from tmp_t_ds_process_instance_1 b where a.process_instance_id = b.process_instance_id
);
delete a from t_ds_relation_process_instance a where exists (select 1 from tmp_t_ds_process_instance_2 b where a.parent_task_instance_id = b.task_instance_id
);

删除临时表

drop table if exists tmp_t_ds_process_instance_1;
drop table if exists tmp_t_ds_process_instance_2;

1.3 海豚上创建任务定时运行

在这里插入图片描述

ERROR org.apache.dolphinscheduler.api.exceptions.ApiExceptionHandler:[53] - [WorkflowInstance-0][TaskInstance-0] - 导入工作流定义错误 org.springframework.dao.DuplicateKeyException: ### Error updating database. Cause: java.sql.SQLIntegrityConstraintViolationException: Duplicate entry '111' for key 'PRIMARY' ### The error may exist in org/apache/dolphinscheduler/dao/mapper/ScheduleMapper.java (best guess) ### The error may involve org.apache.dolphinscheduler.dao.mapper.ScheduleMapper.insert-Inline ### The error occurred while setting parameters ### SQL: INSERT INTO t_ds_schedules ( id, process_definition_code, start_time, end_time, timezone_id, crontab, failure_strategy, warning_type, create_time, update_time, user_id, release_state, warning_group_id, process_instance_priority, worker_group, environment_code ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) ### Cause: java.sql.SQLIntegrityConstraintViolationException: Duplicate entry '111' for key 'PRIMARY' ; Duplicate entry '111' for key 'PRIMARY'; nested exception is java.sql.SQLIntegrityConstraintViolationException: Duplicate entry '111' for key 'PRIMARY' at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:247) at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:70) -- at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:194) at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:181) at sun.reflect.GeneratedMethodAccessor834.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:427) ... 132 common frames omitted 2025-11-18 14:03:02,564 INFO org.apache.dolphinscheduler.api.aspect.AccessLogAspect:[91] - [WorkflowInstance-0][TaskInstance-0] - REQUEST TRACE_ID:4f6cea03-97db-491d-9d30-0dcc8e1d15ed, LOGIN_USER:liuyi, URI:/dolphinscheduler/projects/19712671016480/process-definition/import, METHOD:POST, HANDLER:org.apache.dolphinscheduler.api.controller.ProcessDefinitionController.importProcessDefinition, ARGS:{projectCode=19712671016480} 2025-11-18 14:03:02,629 INFO org.apache.dolphinscheduler.api.service.impl.ProcessDefinitionServiceImpl:[306] - [WorkflowInstance-0][TaskInstance-0] - The task has not changed, so skip 2025-11-18 14:03:02,633 INFO org.apache.dolphinscheduler.api.service.impl.ProcessDefinitionServiceImpl:[315] - [WorkflowInstance-0][TaskInstance-0] - Save process definition complete, processCode:19713968970912, processVersion:1.
最新发布
11-20
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值