✨ 添加分布式锁组件.
This commit is contained in:
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user