PowerJob的CleanService清理服务流程

 更新时间:2024年02月16日 13:57:16   作者:codecraft  
这篇文章主要为大家介绍了PowerJob的CleanService清理服务流程源码解读,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪<BR>

引言

本文主要研究一下PowerJob的CleanService

CleanService

@Slf4j
@Service
public class CleanService {
    private final DFsService dFsService;
    private final InstanceInfoRepository instanceInfoRepository;
    private final WorkflowInstanceInfoRepository workflowInstanceInfoRepository;
    private final WorkflowNodeInfoRepository workflowNodeInfoRepository;
    private final LockService lockService;
    private final int instanceInfoRetentionDay;
    private final int localContainerRetentionDay;
    private final int remoteContainerRetentionDay;
    private static final int TEMPORARY_RETENTION_DAY = 3;
    /**
     * 每天凌晨3点定时清理
     */
    private static final String CLEAN_TIME_EXPRESSION = "0 0 3 * * ?";
    private static final String HISTORY_DELETE_LOCK = "history_delete_lock";
    public CleanService(DFsService dFsService, InstanceInfoRepository instanceInfoRepository, WorkflowInstanceInfoRepository workflowInstanceInfoRepository,
                        WorkflowNodeInfoRepository workflowNodeInfoRepository, LockService lockService,
                        @Value("${oms.instanceinfo.retention}") int instanceInfoRetentionDay,
                        @Value("${oms.container.retention.local}") int localContainerRetentionDay,
                        @Value("${oms.container.retention.remote}") int remoteContainerRetentionDay) {
        this.dFsService = dFsService;
        this.instanceInfoRepository = instanceInfoRepository;
        this.workflowInstanceInfoRepository = workflowInstanceInfoRepository;
        this.workflowNodeInfoRepository = workflowNodeInfoRepository;
        this.lockService = lockService;
        this.instanceInfoRetentionDay = instanceInfoRetentionDay;
        this.localContainerRetentionDay = localContainerRetentionDay;
        this.remoteContainerRetentionDay = remoteContainerRetentionDay;
    }
    //......
}
CleanService提供了timingClean、cleanLocal方法

timingClean

@Async(PJThreadPool.TIMING_POOL)
    @Scheduled(cron = CLEAN_TIME_EXPRESSION)
    public void timingClean() {

        // 释放本地缓存
        WorkerClusterManagerService.cleanUp();

        // 释放磁盘空间
        cleanLocal(OmsFileUtils.genLogDirPath(), instanceInfoRetentionDay);
        cleanLocal(OmsFileUtils.genContainerJarPath(), localContainerRetentionDay);
        cleanLocal(OmsFileUtils.genTemporaryPath(), TEMPORARY_RETENTION_DAY);

        // 删除数据库历史的数据
        cleanByOneServer();
    }
timingClean先执行WorkerClusterManagerService.cleanUp()释放本地缓存,之后通过cleanLocal释放本地磁盘空间,最后执行cleanByOneServer删除历史数据

cleanLocal

@VisibleForTesting
    public void cleanLocal(String path, int day) {
        if (day < 0) {
            log.info("[CleanService] won't clean up {} because of offset day <= 0.", path);
            return;
        }
        Stopwatch stopwatch = Stopwatch.createStarted();
        File dir = new File(path);
        if (!dir.exists()) {
            return;
        }
        File[] logFiles = dir.listFiles();
        if (logFiles == null || logFiles.length == 0) {
            return;
        }
        // 计算最大偏移量
        long maxOffset = day * 24 * 60 * 60 * 1000L;
        for (File f : logFiles) {
            long offset = System.currentTimeMillis() - f.lastModified();
            if (offset >= maxOffset) {
                if (!f.delete()) {
                    log.warn("[CleanService] delete file({}) failed.", f.getName());
                }else {
                    log.info("[CleanService] delete file({}) successfully.", f.getName());
                }
            }
        }
        log.info("[CleanService] clean {} successfully, using {}.", path, stopwatch.stop());
    }
cleanLocal会删除指定目录中lastModified距离当前时间大于等于1天的文件

cleanByOneServer

private void cleanByOneServer() {
        // 只要第一个server抢到锁其他server就会返回,所以锁10分钟应该足够了
        boolean lock = lockService.tryLock(HISTORY_DELETE_LOCK, 10 * 60 * 1000L);
        if (!lock) {
            log.info("[CleanService] clean job is already running, just return.");
            return;
        }
        try {
            // 删除数据库运行记录
            cleanInstanceLog();
            cleanWorkflowInstanceLog();
            // 删除无用节点
            cleanWorkflowNodeInfo();
            // 删除 GridFS 过期文件
            cleanRemote(Constants.LOG_BUCKET, instanceInfoRetentionDay);
            cleanRemote(Constants.CONTAINER_BUCKET, remoteContainerRetentionDay);
        } finally {
            lockService.unlock(HISTORY_DELETE_LOCK);
        }
    }
cleanByOneServer先加锁,然后执行cleanInstanceLog、cleanWorkflowInstanceLog、cleanWorkflowNodeInfo等

小结

PowerJob的CleanService提供了timingClean、cleanLocal方法,其中timingClean先执行WorkerClusterManagerService.cleanUp()释放本地缓存,之后通过cleanLocal释放本地磁盘空间,最后执行cleanByOneServer删除历史数据;cleanLocal会删除指定目录中lastModified距离当前时间大于等于1天的文件。

以上就是PowerJob的CleanService清理服务流程的详细内容,更多关于PowerJob CleanService流程的资料请关注脚本之家其它相关文章!

相关文章

  • Java中常见队列举例详解(非线程安全)

    Java中常见队列举例详解(非线程安全)

    队列用于模拟队列这种数据结构,队列通常是指先进先出的容器,这篇文章主要介绍了Java中常见队列(非线程安全)的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2025-06-06
  • Java的super关键字与instanceof运算符使用方法

    Java的super关键字与instanceof运算符使用方法

    这篇文章主要介绍了Java的super关键字与instanceof运算符使用方法,是Java入门学习中的基础知识,需要的朋友可以参考下
    2015-09-09
  • 详解SpringBoot同时可以处理多少请求

    详解SpringBoot同时可以处理多少请求

    在日常操作中,相信很多人在SpringBoot能同时处理多少请求问题上存在疑惑,本文就来详细的介绍一下,感兴趣的可以了解一下
    2024-06-06
  • SpringBoot如何基于POI-tl和word模板导出庞大的Word文件

    SpringBoot如何基于POI-tl和word模板导出庞大的Word文件

    这篇文章主要介绍了SpringBoot如何基于POI-tl和word模板导出庞大的Word文件,poi-tl是一个基于Apache POI的Word模板引擎,也是一个免费开源的Java类库
    2022-08-08
  • Window搭建部署RocketMQ步骤详解

    Window搭建部署RocketMQ步骤详解

    这篇文章主要介绍了Window搭建部署RocketMQ步骤详解,RocketMq是一个由阿里巴巴开源的消息中间件,脱胎去阿里每部使用的MetaQ,在设计上借鉴了Kafka。,需要的朋友可以参考下
    2019-06-06
  • Java标识接口的使用方法

    Java标识接口的使用方法

    在本篇文章中小编给大家分享了关于Java标识接口的使用方法和教程内容,有需要的朋友们学习下。
    2019-01-01
  • 利用Spring Social轻松搞定微信授权登录的方法示例

    利用Spring Social轻松搞定微信授权登录的方法示例

    这篇文章主要介绍了利用Spring Social轻松搞定微信授权登录的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • 如何在Java中读取resources下的文件及资源路径

    如何在Java中读取resources下的文件及资源路径

    本文介绍了如何在Java中读取resources下的文件以及获取resource文件的路径,通过使用ClassLoader或Class的getResourceAsStream方法,可以轻松地读取resources目录下的文件,感兴趣的朋友跟随小编一起看看吧
    2023-06-06
  • java 中sleep() 和 wait() 的对比

    java 中sleep() 和 wait() 的对比

    这篇文章主要介绍了java 中sleep() 和 wait() 的对比的相关资料,需要的朋友可以参考下
    2017-04-04
  • 通Java接口上传实现SMMS图床

    通Java接口上传实现SMMS图床

    这篇文章主要介绍了通Java接口上传实现SMMS图床,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-07-07

最新评论