✨ 添加分布式锁组件.
This commit is contained in:
@@ -17,9 +17,9 @@ import org.springframework.stereotype.Component;
|
||||
public class AppExecLogConfig {
|
||||
|
||||
/**
|
||||
* 是否拼接执行状态
|
||||
* 是否拼接 ansi 执行状态日志
|
||||
*/
|
||||
private Boolean appendStatus;
|
||||
private Boolean appendAnsi;
|
||||
|
||||
/**
|
||||
* 自动清理执行文件
|
||||
@@ -32,9 +32,9 @@ public class AppExecLogConfig {
|
||||
private Integer keepPeriod;
|
||||
|
||||
public AppExecLogConfig() {
|
||||
this.appendStatus = true;
|
||||
this.appendAnsi = true;
|
||||
this.autoClear = true;
|
||||
this.keepPeriod = 90;
|
||||
this.keepPeriod = 60;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package com.orion.ops.module.asset.task;
|
||||
|
||||
import com.orion.ops.framework.redis.core.utils.RedisLocks;
|
||||
import com.orion.ops.module.asset.service.CommandSnippetGroupService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -25,9 +24,6 @@ public class CommandSnippetGroupAutoClearTask {
|
||||
*/
|
||||
private static final String LOCK_KEY = "clear:csg:lock";
|
||||
|
||||
@Resource
|
||||
private RedissonClient redissonClient;
|
||||
|
||||
@Resource
|
||||
private CommandSnippetGroupService commandSnippetGroupService;
|
||||
|
||||
@@ -37,22 +33,9 @@ public class CommandSnippetGroupAutoClearTask {
|
||||
@Scheduled(cron = "0 10 2 * * ?")
|
||||
public void clear() {
|
||||
log.info("CommandSnippetGroupAutoClearTask.clear start");
|
||||
// 获取锁
|
||||
RLock lock = redissonClient.getLock(LOCK_KEY);
|
||||
// 未获取到直接返回
|
||||
if (!lock.tryLock()) {
|
||||
log.info("CommandSnippetGroupAutoClearTask.clear locked end");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// 清理
|
||||
commandSnippetGroupService.clearUnusedGroup();
|
||||
log.info("CommandSnippetGroupAutoClearTask.clear finish");
|
||||
} catch (Exception e) {
|
||||
log.error("CommandSnippetGroupAutoClearTask.clear error", e);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
// 获取锁并清理
|
||||
RedisLocks.tryLock(LOCK_KEY, commandSnippetGroupService::clearUnusedGroup);
|
||||
log.info("CommandSnippetGroupAutoClearTask.clear finish");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,12 +4,11 @@ import com.orion.lang.utils.Strings;
|
||||
import com.orion.lang.utils.io.Files1;
|
||||
import com.orion.lang.utils.time.Dates;
|
||||
import com.orion.ops.framework.common.file.FileClient;
|
||||
import com.orion.ops.framework.redis.core.utils.RedisLocks;
|
||||
import com.orion.ops.module.asset.dao.ExecHostLogDAO;
|
||||
import com.orion.ops.module.asset.define.config.AppExecLogConfig;
|
||||
import com.orion.ops.module.asset.entity.domain.ExecHostLogDO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -38,9 +37,6 @@ public class ExecLogFileAutoClearTask {
|
||||
@Resource
|
||||
private AppExecLogConfig appExecLogConfig;
|
||||
|
||||
@Resource
|
||||
private RedissonClient redissonClient;
|
||||
|
||||
@Resource
|
||||
private FileClient logsFileClient;
|
||||
|
||||
@@ -50,26 +46,12 @@ public class ExecLogFileAutoClearTask {
|
||||
/**
|
||||
* 清理
|
||||
*/
|
||||
// @Scheduled(cron = "0 0 3 * * ?")
|
||||
@Scheduled(fixedRate = 20000)
|
||||
@Scheduled(cron = "0 0 3 * * ?")
|
||||
public void clear() {
|
||||
log.info("ExecLogFileAutoClearTask.clear start");
|
||||
// 获取锁
|
||||
RLock lock = redissonClient.getLock(LOCK_KEY);
|
||||
// 未获取到直接返回
|
||||
if (!lock.tryLock()) {
|
||||
log.info("ExecLogFileAutoClearTask.clear locked end");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// 清理
|
||||
this.doClearFile();
|
||||
log.info("ExecLogFileAutoClearTask.clear finish");
|
||||
} catch (Exception e) {
|
||||
log.error("ExecLogFileAutoClearTask.clear error", e);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
// 获取锁并且执行
|
||||
RedisLocks.tryLock(LOCK_KEY, this::doClearFile);
|
||||
log.info("ExecLogFileAutoClearTask.clear finish");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package com.orion.ops.module.asset.task;
|
||||
|
||||
import com.orion.ops.framework.redis.core.utils.RedisLocks;
|
||||
import com.orion.ops.module.asset.service.PathBookmarkGroupService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -25,9 +24,6 @@ public class PathBookmarkGroupAutoClearTask {
|
||||
*/
|
||||
private static final String LOCK_KEY = "clear:pbg:lock";
|
||||
|
||||
@Resource
|
||||
private RedissonClient redissonClient;
|
||||
|
||||
@Resource
|
||||
private PathBookmarkGroupService pathBookmarkGroupService;
|
||||
|
||||
@@ -37,22 +33,9 @@ public class PathBookmarkGroupAutoClearTask {
|
||||
@Scheduled(cron = "0 20 2 * * ?")
|
||||
public void clear() {
|
||||
log.info("PathBookmarkGroupAutoClearTask.clear start");
|
||||
// 获取锁
|
||||
RLock lock = redissonClient.getLock(LOCK_KEY);
|
||||
// 未获取到直接返回
|
||||
if (!lock.tryLock()) {
|
||||
log.info("PathBookmarkGroupAutoClearTask.clear locked end");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// 清理
|
||||
pathBookmarkGroupService.clearUnusedGroup();
|
||||
log.info("PathBookmarkGroupAutoClearTask.clear finish");
|
||||
} catch (Exception e) {
|
||||
log.error("PathBookmarkGroupAutoClearTask.clear error", e);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
// 获取锁并清理
|
||||
RedisLocks.tryLock(LOCK_KEY, pathBookmarkGroupService::clearUnusedGroup);
|
||||
log.info("PathBookmarkGroupAutoClearTask.clear finish");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -48,9 +48,9 @@
|
||||
"defaultValue": "bk_${fileName}_${timestamp}"
|
||||
},
|
||||
{
|
||||
"name": "app.exec-log.append-status",
|
||||
"name": "app.exec-log.append-ansi",
|
||||
"type": "java.lang.Boolean",
|
||||
"description": "是否拼接执行状态.",
|
||||
"description": "是否拼接 ansi 执行状态日志.",
|
||||
"defaultValue": "true"
|
||||
},
|
||||
{
|
||||
@@ -63,7 +63,7 @@
|
||||
"name": "app.exec-log.keep-period",
|
||||
"type": "java.lang.Integer",
|
||||
"description": "保留周期 (天)",
|
||||
"defaultValue": "90"
|
||||
"defaultValue": "60"
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user