定时删除未使用的分组.

This commit is contained in:
lijiahang
2024-04-25 11:50:09 +08:00
parent d6a021b4d9
commit d52c1f4d88
13 changed files with 273 additions and 7 deletions

View File

@@ -47,4 +47,9 @@ public interface CommandSnippetGroupService {
*/
Integer deleteCommandSnippetGroup(CommandSnippetGroupDeleteRequest request);
/**
* 清理未使用的分组
*/
void clearUnusedGroup();
}

View File

@@ -47,4 +47,9 @@ public interface PathBookmarkGroupService {
*/
Integer deletePathBookmarkGroup(PathBookmarkGroupDeleteRequest request);
/**
* 清理未使用的分组
*/
void clearUnusedGroup();
}

View File

@@ -7,6 +7,8 @@ import com.orion.ops.framework.common.constant.ErrorMessage;
import com.orion.ops.framework.common.utils.Valid;
import com.orion.ops.framework.security.core.utils.SecurityUtils;
import com.orion.ops.module.asset.convert.CommandSnippetGroupConvert;
import com.orion.ops.module.asset.dao.CommandSnippetDAO;
import com.orion.ops.module.asset.entity.domain.CommandSnippetDO;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetGroupCreateRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetGroupDeleteRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetGroupUpdateRequest;
@@ -26,6 +28,7 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
@@ -45,6 +48,9 @@ public class CommandSnippetGroupServiceImpl implements CommandSnippetGroupServic
@Resource
private DataGroupUserApi dataGroupUserApi;
@Resource
private CommandSnippetDAO commandSnippetDAO;
@Resource
private CommandSnippetService commandSnippetService;
@@ -96,4 +102,37 @@ public class CommandSnippetGroupServiceImpl implements CommandSnippetGroupServic
return effect;
}
@Override
public void clearUnusedGroup() {
// 查询全部 groupId
Map<Long, List<Long>> userGroupMap = commandSnippetDAO.of()
.createWrapper()
.select(CommandSnippetDO::getUserId, CommandSnippetDO::getGroupId)
.isNotNull(CommandSnippetDO::getGroupId)
.groupBy(CommandSnippetDO::getGroupId)
.then()
.stream()
.collect(Collectors.groupingBy(CommandSnippetDO::getUserId,
Collectors.mapping(CommandSnippetDO::getGroupId, Collectors.toList())));
userGroupMap.forEach((k, v) -> {
// 查询用户分组
List<DataGroupDTO> groups = dataGroupUserApi.getDataGroupList(DataGroupTypeEnum.COMMAND_SNIPPET, k);
if (groups.isEmpty()) {
return;
}
// 不存在的则移除
List<Long> deleteGroupList = groups.stream()
.map(DataGroupDTO::getId)
.filter(s -> !v.contains(s))
.collect(Collectors.toList());
if (deleteGroupList.isEmpty()) {
return;
}
log.info("CommandSnippetGroupService.clearUnusedGroup userId: {}, deleteGroupList: {}", k, deleteGroupList);
// 删除分组
Integer effect = dataGroupUserApi.deleteDataGroupByIdList(DataGroupTypeEnum.COMMAND_SNIPPET, k, deleteGroupList);
log.info("CommandSnippetGroupService.clearUnusedGroup userId: {}, effect: {}", k, effect);
});
}
}

View File

@@ -7,6 +7,8 @@ import com.orion.ops.framework.common.constant.ErrorMessage;
import com.orion.ops.framework.common.utils.Valid;
import com.orion.ops.framework.security.core.utils.SecurityUtils;
import com.orion.ops.module.asset.convert.PathBookmarkGroupConvert;
import com.orion.ops.module.asset.dao.PathBookmarkDAO;
import com.orion.ops.module.asset.entity.domain.PathBookmarkDO;
import com.orion.ops.module.asset.entity.request.path.PathBookmarkGroupCreateRequest;
import com.orion.ops.module.asset.entity.request.path.PathBookmarkGroupDeleteRequest;
import com.orion.ops.module.asset.entity.request.path.PathBookmarkGroupUpdateRequest;
@@ -26,6 +28,7 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
@@ -45,6 +48,9 @@ public class PathBookmarkGroupServiceImpl implements PathBookmarkGroupService {
@Resource
private DataGroupUserApi dataGroupUserApi;
@Resource
private PathBookmarkDAO pathBookmarkDAO;
@Resource
private PathBookmarkService pathBookmarkService;
@@ -96,4 +102,37 @@ public class PathBookmarkGroupServiceImpl implements PathBookmarkGroupService {
return effect;
}
@Override
public void clearUnusedGroup() {
// 查询全部 groupId
Map<Long, List<Long>> userGroupMap = pathBookmarkDAO.of()
.createWrapper()
.select(PathBookmarkDO::getUserId, PathBookmarkDO::getGroupId)
.isNotNull(PathBookmarkDO::getGroupId)
.groupBy(PathBookmarkDO::getGroupId)
.then()
.stream()
.collect(Collectors.groupingBy(PathBookmarkDO::getUserId,
Collectors.mapping(PathBookmarkDO::getGroupId, Collectors.toList())));
userGroupMap.forEach((k, v) -> {
// 查询用户分组
List<DataGroupDTO> groups = dataGroupUserApi.getDataGroupList(DataGroupTypeEnum.PATH_BOOKMARK, k);
if (groups.isEmpty()) {
return;
}
// 不存在的则移除
List<Long> deleteGroupList = groups.stream()
.map(DataGroupDTO::getId)
.filter(s -> !v.contains(s))
.collect(Collectors.toList());
if (deleteGroupList.isEmpty()) {
return;
}
log.info("PathBookmarkGroupService.clearUnusedGroup userId: {}, deleteGroupList: {}", k, deleteGroupList);
// 删除分组
Integer effect = dataGroupUserApi.deleteDataGroupByIdList(DataGroupTypeEnum.PATH_BOOKMARK, k, deleteGroupList);
log.info("PathBookmarkGroupService.clearUnusedGroup userId: {}, effect: {}", k, effect);
});
}
}

View File

@@ -0,0 +1,58 @@
package com.orion.ops.module.asset.task;
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;
import javax.annotation.Resource;
/**
* 定时清理未使用的命令片段分组
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024/4/24 23:50
*/
@Slf4j
@Component
public class CommandSnippetGroupAutoClearTask {
/**
* 分布式锁名称
*/
private static final String LOCK_KEY = "clear:csg:lock";
@Resource
private RedissonClient redissonClient;
@Resource
private CommandSnippetGroupService commandSnippetGroupService;
/**
* 清理
*/
@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();
}
}
}

View File

@@ -0,0 +1,58 @@
package com.orion.ops.module.asset.task;
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;
import javax.annotation.Resource;
/**
* 定时清理未使用的路径标签分组
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024/4/24 23:50
*/
@Slf4j
@Component
public class PathBookmarkGroupAutoClearTask {
/**
* 分布式锁名称
*/
private static final String LOCK_KEY = "clear:pbg:lock";
@Resource
private RedissonClient redissonClient;
@Resource
private PathBookmarkGroupService pathBookmarkGroupService;
/**
* 清理
*/
@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();
}
}
}