✨ 定时删除未使用的分组.
This commit is contained in:
@@ -47,4 +47,9 @@ public interface CommandSnippetGroupService {
|
||||
*/
|
||||
Integer deleteCommandSnippetGroup(CommandSnippetGroupDeleteRequest request);
|
||||
|
||||
/**
|
||||
* 清理未使用的分组
|
||||
*/
|
||||
void clearUnusedGroup();
|
||||
|
||||
}
|
||||
|
||||
@@ -47,4 +47,9 @@ public interface PathBookmarkGroupService {
|
||||
*/
|
||||
Integer deletePathBookmarkGroup(PathBookmarkGroupDeleteRequest request);
|
||||
|
||||
/**
|
||||
* 清理未使用的分组
|
||||
*/
|
||||
void clearUnusedGroup();
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -74,4 +74,13 @@ public interface DataGroupApi {
|
||||
*/
|
||||
Integer deleteDataGroupById(Long id);
|
||||
|
||||
/**
|
||||
* 删除数据分组
|
||||
*
|
||||
* @param type type
|
||||
* @param idList idList
|
||||
* @return effect
|
||||
*/
|
||||
Integer deleteDataGroupByIdList(DataGroupTypeEnum type, List<Long> idList);
|
||||
|
||||
}
|
||||
|
||||
@@ -43,4 +43,14 @@ public interface DataGroupUserApi {
|
||||
*/
|
||||
List<DataGroupDTO> getDataGroupTree(DataGroupTypeEnum type, Long userId);
|
||||
|
||||
/**
|
||||
* 删除数据分组
|
||||
*
|
||||
* @param type type
|
||||
* @param userId userId
|
||||
* @param idList idList
|
||||
* @return effect
|
||||
*/
|
||||
Integer deleteDataGroupByIdList(DataGroupTypeEnum type, Long userId, List<Long> idList);
|
||||
|
||||
}
|
||||
|
||||
@@ -88,4 +88,9 @@ public class DataGroupApiImpl implements DataGroupApi {
|
||||
return dataGroupService.deleteDataGroupById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer deleteDataGroupByIdList(DataGroupTypeEnum type, List<Long> idList) {
|
||||
return dataGroupService.deleteDataGroupByIdList(type.name(), Const.SYSTEM_USER_ID, idList);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -50,4 +50,9 @@ public class DataGroupUserApiImpl implements DataGroupUserApi {
|
||||
return DataGroupProviderConvert.MAPPER.toList(rows);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer deleteDataGroupByIdList(DataGroupTypeEnum type, Long userId, List<Long> idList) {
|
||||
return dataGroupService.deleteDataGroupByIdList(type.name(), userId, idList);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -66,6 +66,16 @@ public interface DataGroupService {
|
||||
*/
|
||||
Integer deleteDataGroupById(Long id);
|
||||
|
||||
/**
|
||||
* 通过 id 删除数据分组
|
||||
*
|
||||
* @param type type
|
||||
* @param userId userId
|
||||
* @param idList idList
|
||||
* @return effect
|
||||
*/
|
||||
Integer deleteDataGroupByIdList(String type, Long userId, List<Long> idList);
|
||||
|
||||
/**
|
||||
* 通过 userId 删除数据分组
|
||||
*
|
||||
|
||||
@@ -28,6 +28,7 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -66,7 +67,7 @@ public class DataGroupServiceImpl implements DataGroupService {
|
||||
Long id = record.getId();
|
||||
log.info("DataGroupService-createDataGroup id: {}, effect: {}", id, effect);
|
||||
// 删除缓存
|
||||
this.deleteCache(record.getUserId(), request.getType());
|
||||
this.deleteCache(request.getType(), record.getUserId());
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -90,7 +91,7 @@ public class DataGroupServiceImpl implements DataGroupService {
|
||||
// 更新
|
||||
int effect = dataGroupDAO.updateById(updateRecord);
|
||||
// 删除缓存
|
||||
this.deleteCache(record.getUserId(), record.getType());
|
||||
this.deleteCache(record.getType(), record.getUserId());
|
||||
return effect;
|
||||
}
|
||||
|
||||
@@ -144,7 +145,7 @@ public class DataGroupServiceImpl implements DataGroupService {
|
||||
effect = dataGroupDAO.updateById(update);
|
||||
}
|
||||
// 删除缓存
|
||||
this.deleteCache(moveRecord.getUserId(), type);
|
||||
this.deleteCache(type, moveRecord.getUserId());
|
||||
// 添加日志参数
|
||||
OperatorLogs.add(OperatorLogs.SOURCE, moveRecord.getName());
|
||||
OperatorLogs.add(OperatorLogs.TARGET, targetRecord.getName());
|
||||
@@ -219,12 +220,34 @@ public class DataGroupServiceImpl implements DataGroupService {
|
||||
dataGroupRelService.deleteByGroupIdList(type, record.getUserId(), deleteIdList);
|
||||
log.info("DataGroupService-deleteDataGroupById id: {}, effect: {}", id, effect);
|
||||
// 删除缓存
|
||||
this.deleteCache(record.getUserId(), type);
|
||||
this.deleteCache(type, record.getUserId());
|
||||
// 添加日志参数
|
||||
OperatorLogs.add(OperatorLogs.GROUP_NAME, record.getName());
|
||||
return effect;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Integer deleteDataGroupByIdList(String type, Long userId, List<Long> idList) {
|
||||
log.info("DataGroupService-deleteDataGroupByIdList idList: {}", idList);
|
||||
// 检查数据是否存在
|
||||
List<DataGroupDO> records = dataGroupDAO.selectBatchIds(idList);
|
||||
if (records.isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
// 查询子级
|
||||
List<Long> deleteIdList = new ArrayList<>(idList);
|
||||
this.flatChildrenId(idList, deleteIdList);
|
||||
// 删除分组
|
||||
int effect = dataGroupDAO.deleteBatchIds(deleteIdList);
|
||||
// 删除组内数据
|
||||
dataGroupRelService.deleteByGroupIdList(type, userId, deleteIdList);
|
||||
log.info("DataGroupService-deleteDataGroupByIdList id: {}, effect: {}", idList, effect);
|
||||
// 删除缓存
|
||||
this.deleteCache(type, userId);
|
||||
return effect;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer deleteDataGroupByUserId(Long userId) {
|
||||
// 删除分组
|
||||
@@ -283,10 +306,10 @@ public class DataGroupServiceImpl implements DataGroupService {
|
||||
/**
|
||||
* 删除缓存
|
||||
*
|
||||
* @param userId userId
|
||||
* @param type type
|
||||
* @param userId userId
|
||||
*/
|
||||
private void deleteCache(Long userId, String type) {
|
||||
private void deleteCache(String type, Long userId) {
|
||||
RedisStrings.delete(DataGroupCacheKeyDefine.DATA_GROUP_LIST.format(type, userId),
|
||||
DataGroupCacheKeyDefine.DATA_GROUP_TREE.format(type, userId));
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ public class TagAutoClearTask {
|
||||
/**
|
||||
* 分布式锁名称
|
||||
*/
|
||||
private static final String LOCK_KEY = "tag:clear:lock";
|
||||
private static final String LOCK_KEY = "clear:tag:lock";
|
||||
|
||||
@Resource
|
||||
private RedissonClient redissonClient;
|
||||
|
||||
Reference in New Issue
Block a user