PowerJob的CleanService清理服務流程
引言
本文主要研究一下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); // 刪除數(shù)據(jù)庫歷史的數(shù)據(jù) cleanByOneServer(); }
timingClean先執(zhí)行WorkerClusterManagerService.cleanUp()釋放本地緩存,之后通過cleanLocal釋放本地磁盤空間,最后執(zhí)行cleanByOneServer刪除歷史數(shù)據(jù)
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會刪除指定目錄中l(wèi)astModified距離當前時間大于等于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 { // 刪除數(shù)據(jù)庫運行記錄 cleanInstanceLog(); cleanWorkflowInstanceLog(); // 刪除無用節(jié)點 cleanWorkflowNodeInfo(); // 刪除 GridFS 過期文件 cleanRemote(Constants.LOG_BUCKET, instanceInfoRetentionDay); cleanRemote(Constants.CONTAINER_BUCKET, remoteContainerRetentionDay); } finally { lockService.unlock(HISTORY_DELETE_LOCK); } }
cleanByOneServer先加鎖,然后執(zhí)行cleanInstanceLog、cleanWorkflowInstanceLog、cleanWorkflowNodeInfo等
小結
PowerJob的CleanService提供了timingClean、cleanLocal方法,其中timingClean先執(zhí)行WorkerClusterManagerService.cleanUp()釋放本地緩存,之后通過cleanLocal釋放本地磁盤空間,最后執(zhí)行cleanByOneServer刪除歷史數(shù)據(jù);cleanLocal會刪除指定目錄中l(wèi)astModified距離當前時間大于等于1天的文件。
以上就是PowerJob的CleanService清理服務流程的詳細內(nèi)容,更多關于PowerJob CleanService流程的資料請關注腳本之家其它相關文章!
相關文章
Java使用itextpdf實現(xiàn)PDF轉(zhuǎn)文本以及轉(zhuǎn)圖片
PDF轉(zhuǎn)文本的插件常用的有pdfbox ,itextpdf 和 spire.pdf,本文主要介紹如何使用itextpdf實現(xiàn)PDF轉(zhuǎn)文本以及轉(zhuǎn)圖片,需要的可以參考一下2025-01-01關于elasticsearch的match_phrase_prefix查詢詳解
這篇文章主要介紹了關于elasticsearch的match_phrase_prefix查詢問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03使用ServletInputStream在攔截器或過濾器中應用后重寫
這篇文章主要介紹了使用ServletInputStream在攔截器或過濾器中應用后重寫,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10Java8時間轉(zhuǎn)換(LocalDateTime)代碼實例
這篇文章主要介紹了java8時間轉(zhuǎn)換(LocalDateTime)代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11Mybatis-Plus使用ID_WORKER生成主鍵id重復的解決方法
本文主要介紹了Mybatis-Plus使用ID_WORKER生成主鍵id重復的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07java基本教程之常用的實現(xiàn)多線程的兩種方式 java多線程教程
下面開始學習“常用的實現(xiàn)多線程的2種方式”:Thread 和 Runnable。之所以說是常用的,是因為通過還可以通過java.util.concurrent包中的線程池來實現(xiàn)多線程2014-01-01