PowerJob的CleanService清理服務(wù)流程
引言
本文主要研究一下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點(diǎn)定時(shí)清理
*/
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ù)庫(kù)歷史的數(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;
}
// 計(jì)算最大偏移量
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會(huì)刪除指定目錄中l(wèi)astModified距離當(dāng)前時(shí)間大于等于1天的文件
cleanByOneServer
private void cleanByOneServer() {
// 只要第一個(gè)server搶到鎖其他server就會(huì)返回,所以鎖10分鐘應(yīng)該足夠了
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ù)庫(kù)運(yùn)行記錄
cleanInstanceLog();
cleanWorkflowInstanceLog();
// 刪除無用節(jié)點(diǎn)
cleanWorkflowNodeInfo();
// 刪除 GridFS 過期文件
cleanRemote(Constants.LOG_BUCKET, instanceInfoRetentionDay);
cleanRemote(Constants.CONTAINER_BUCKET, remoteContainerRetentionDay);
} finally {
lockService.unlock(HISTORY_DELETE_LOCK);
}
}cleanByOneServer先加鎖,然后執(zhí)行cleanInstanceLog、cleanWorkflowInstanceLog、cleanWorkflowNodeInfo等
小結(jié)
PowerJob的CleanService提供了timingClean、cleanLocal方法,其中timingClean先執(zhí)行WorkerClusterManagerService.cleanUp()釋放本地緩存,之后通過cleanLocal釋放本地磁盤空間,最后執(zhí)行cleanByOneServer刪除歷史數(shù)據(jù);cleanLocal會(huì)刪除指定目錄中l(wèi)astModified距離當(dāng)前時(shí)間大于等于1天的文件。
以上就是PowerJob的CleanService清理服務(wù)流程的詳細(xì)內(nèi)容,更多關(guān)于PowerJob CleanService流程的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
關(guān)于Integer.parseInt()方法的使用
這篇文章主要介紹了關(guān)于Integer.parseInt()方法的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
Java使用itextpdf實(shí)現(xiàn)PDF轉(zhuǎn)文本以及轉(zhuǎn)圖片
PDF轉(zhuǎn)文本的插件常用的有pdfbox ,itextpdf 和 spire.pdf,本文主要介紹如何使用itextpdf實(shí)現(xiàn)PDF轉(zhuǎn)文本以及轉(zhuǎn)圖片,需要的可以參考一下2025-01-01
使用ServletInputStream在攔截器或過濾器中應(yīng)用后重寫
Java8時(shí)間轉(zhuǎn)換(LocalDateTime)代碼實(shí)例
Mybatis-Plus使用ID_WORKER生成主鍵id重復(fù)的解決方法
java基本教程之常用的實(shí)現(xiàn)多線程的兩種方式 java多線程教程

