序本文主要研究一下 PowerJob 的 CleanService
CleanService@Slf4j@Servicepublic 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@VisibleForTestingpublic 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());}
复制代码
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 天的文件。
技术前沿拓展
前端开发,你的认知不能仅局限于技术内,需要发散思维了解技术圈的前沿知识。细心的人会发现,开发内部工具的过程中,大量的页面、场景、组件等在不断重复,这种重复造轮子的工作,浪费工程师的大量时间。
介绍一款程序员都应该知道的软件JNPF快速开发平台,很多人都尝试用过它,它是功能的集大成者,任何信息化系统都可以基于它开发出来。
这是一个基于 Java Boot/.Net Core 构建的简单、跨平台快速开发框架。前后端封装了上千个常用类,方便扩展;集成了代码生成器,支持前后端业务代码生成,实现快速开发,提升工作效率;框架集成了表单、报表、图表、大屏等各种常用的 Demo 方便直接使用;后端框架支持 Vue2、Vue3。如果你有闲暇时间,可以做个知识拓展。
看完本文如果觉得有用,记得点个赞支持,收藏起来说不定哪天就用上啦~
评论