mirror of
https://github.com/PowerJob/PowerJob.git
synced 2025-07-17 00:00:04 +08:00
fix: multi server clean the same object
This commit is contained in:
commit
24cff5b6ec
@ -7,6 +7,7 @@ import com.github.kfcfans.powerjob.server.persistence.core.repository.InstanceIn
|
|||||||
import com.github.kfcfans.powerjob.server.persistence.core.repository.WorkflowInstanceInfoRepository;
|
import com.github.kfcfans.powerjob.server.persistence.core.repository.WorkflowInstanceInfoRepository;
|
||||||
import com.github.kfcfans.powerjob.server.persistence.mongodb.GridFsManager;
|
import com.github.kfcfans.powerjob.server.persistence.mongodb.GridFsManager;
|
||||||
import com.github.kfcfans.powerjob.server.service.ha.WorkerManagerService;
|
import com.github.kfcfans.powerjob.server.service.ha.WorkerManagerService;
|
||||||
|
import com.github.kfcfans.powerjob.server.service.lock.LockService;
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
import com.google.common.base.Stopwatch;
|
import com.google.common.base.Stopwatch;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@ -36,6 +37,8 @@ public class CleanService {
|
|||||||
private InstanceInfoRepository instanceInfoRepository;
|
private InstanceInfoRepository instanceInfoRepository;
|
||||||
@Resource
|
@Resource
|
||||||
private WorkflowInstanceInfoRepository workflowInstanceInfoRepository;
|
private WorkflowInstanceInfoRepository workflowInstanceInfoRepository;
|
||||||
|
@Resource
|
||||||
|
private LockService lockService;
|
||||||
|
|
||||||
@Value("${oms.instanceinfo.retention}")
|
@Value("${oms.instanceinfo.retention}")
|
||||||
private int instanceInfoRetentionDay;
|
private int instanceInfoRetentionDay;
|
||||||
@ -51,6 +54,8 @@ public class CleanService {
|
|||||||
// 每天凌晨3点定时清理
|
// 每天凌晨3点定时清理
|
||||||
private static final String CLEAN_TIME_EXPRESSION = "0 0 3 * * ?";
|
private static final String CLEAN_TIME_EXPRESSION = "0 0 3 * * ?";
|
||||||
|
|
||||||
|
private static final String HISTORY_DELETE_LOCK = "history_delete";
|
||||||
|
|
||||||
|
|
||||||
@Async("omsTimingPool")
|
@Async("omsTimingPool")
|
||||||
@Scheduled(cron = CLEAN_TIME_EXPRESSION)
|
@Scheduled(cron = CLEAN_TIME_EXPRESSION)
|
||||||
@ -59,18 +64,35 @@ public class CleanService {
|
|||||||
// 释放本地缓存
|
// 释放本地缓存
|
||||||
WorkerManagerService.cleanUp();
|
WorkerManagerService.cleanUp();
|
||||||
|
|
||||||
// 删除数据库运行记录
|
|
||||||
cleanInstanceLog();
|
|
||||||
cleanWorkflowInstanceLog();
|
|
||||||
|
|
||||||
// 释放磁盘空间
|
// 释放磁盘空间
|
||||||
cleanLocal(OmsFileUtils.genLogDirPath(), instanceInfoRetentionDay);
|
cleanLocal(OmsFileUtils.genLogDirPath(), instanceInfoRetentionDay);
|
||||||
cleanLocal(OmsFileUtils.genContainerJarPath(), localContainerRetentionDay);
|
cleanLocal(OmsFileUtils.genContainerJarPath(), localContainerRetentionDay);
|
||||||
cleanLocal(OmsFileUtils.genTemporaryPath(), TEMPORARY_RETENTION_DAY);
|
cleanLocal(OmsFileUtils.genTemporaryPath(), TEMPORARY_RETENTION_DAY);
|
||||||
|
|
||||||
// 删除 GridFS 过期文件
|
// 删除数据库历史的数据
|
||||||
cleanRemote(GridFsManager.LOG_BUCKET, instanceInfoRetentionDay);
|
cleanOneServer();
|
||||||
cleanRemote(GridFsManager.CONTAINER_BUCKET, remoteContainerRetentionDay);
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 只能一台server清理的操作统一到这里执行
|
||||||
|
*/
|
||||||
|
private void cleanOneServer() {
|
||||||
|
// 只要第一个server抢到锁其他server就会返回,所以锁10分钟应该足够了
|
||||||
|
boolean lock = lockService.lock(HISTORY_DELETE_LOCK, 10 * 60 * 1000);
|
||||||
|
if (!lock) {
|
||||||
|
log.info("task is already running, just return.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
// 删除数据库运行记录
|
||||||
|
cleanInstanceLog();
|
||||||
|
cleanWorkflowInstanceLog();
|
||||||
|
// 删除 GridFS 过期文件
|
||||||
|
cleanRemote(GridFsManager.LOG_BUCKET, instanceInfoRetentionDay);
|
||||||
|
cleanRemote(GridFsManager.CONTAINER_BUCKET, remoteContainerRetentionDay);
|
||||||
|
}finally {
|
||||||
|
lockService.unlock(HISTORY_DELETE_LOCK);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
|
Loading…
x
Reference in New Issue
Block a user