添加分布式锁组件.

This commit is contained in:
lijiahang
2024-04-25 19:00:12 +08:00
parent f5b07ee906
commit 142c0fff1d
13 changed files with 176 additions and 101 deletions

View File

@@ -1,9 +1,8 @@
package com.orion.ops.module.infra.task;
import com.orion.ops.framework.redis.core.utils.RedisLocks;
import com.orion.ops.module.infra.service.TagService;
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,34 +24,18 @@ public class TagAutoClearTask {
*/
private static final String LOCK_KEY = "clear:tag:lock";
@Resource
private RedissonClient redissonClient;
@Resource
private TagService tagService;
/**
* 定时清理未引用的 tag
* 清理
*/
@Scheduled(cron = "0 0 2 * * ?")
public void clear() {
log.info("TagAutoClearTask.clear start");
// 获取锁
RLock lock = redissonClient.getLock(LOCK_KEY);
// 未获取到直接返回
if (!lock.tryLock()) {
log.info("TagAutoClearTask.clear locked end");
return;
}
try {
// 清理
tagService.clearUnusedTag();
log.info("TagAutoClearTask.clear finish");
} catch (Exception e) {
log.error("TagAutoClearTask.clear error", e);
} finally {
lock.unlock();
}
// 获取锁并清理
RedisLocks.tryLock(LOCK_KEY, tagService::clearUnusedTag);
log.info("TagAutoClearTask.clear finish");
}
}