From 367e62e3daf37b3e86776576697da9ee9debbc6d Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Wed, 20 Nov 2024 09:40:40 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E4=B8=AD=E5=BF=83=20ID=20=E8=A2=AB=E9=87=8D=E7=BD=AE=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1048646 --user=赵勇 【任务中心】github#34056,任务中心中的系统即时任务ID重复,当前每天都会从100001编号重新开始编号 https://www.tapd.cn/55049933/s/1612658 --- .../metersphere/system/uid/NumGenerator.java | 42 ++++++++++++------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/uid/NumGenerator.java b/backend/services/system-setting/src/main/java/io/metersphere/system/uid/NumGenerator.java index c877d6c9c7..6a0f57096d 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/uid/NumGenerator.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/uid/NumGenerator.java @@ -61,25 +61,37 @@ public class NumGenerator { } } - private void cleanDeletedProjectResource(ApplicationNumScope value) { - String suffix = "}:allocation"; + private void cleanDeletedProjectResource(ApplicationNumScope scope) { + final String SUFFIX = "}:allocation"; + final int SCAN_COUNT = 1000; - ScanOptions options = ScanOptions.scanOptions().match("*_" + value.name()).count(1000).build(); - try ( - Cursor scan = stringRedisTemplate.scan(options) - ) { - while (scan.hasNext()) { - String key = scan.next(); - if (StringUtils.contains(key, suffix)) { - continue; + ScanOptions options = ScanOptions.scanOptions() + .match("*_" + scope.name()) + .count(SCAN_COUNT) + .build(); + + try (Cursor cursor = stringRedisTemplate.scan(options)) { + cursor.forEachRemaining(key -> { + // 如果 key 包含特定后缀,跳过处理 + if (StringUtils.contains(key, SUFFIX)) { + return; } - Project project = projectMapper.selectByPrimaryKey(key.split("_")[0]); - if (project == null) { - LogUtils.info("清理已经删除项目的num数据: " + key); + + // 提取 projectId 并跳过任务作用域的键 + String projectId = key.split("_")[0]; + if (StringUtils.equals(ApplicationNumScope.TASK.name(), projectId)) { + return; + } + + // 检查项目是否存在,删除已删除项目的相关数据 + if (projectMapper.selectByPrimaryKey(projectId) == null) { + LogUtils.info("清理已经删除项目的 num 数据: {}", key); stringRedisTemplate.delete(key); - stringRedisTemplate.delete("{" + key + suffix); + stringRedisTemplate.delete("{" + key + SUFFIX); } - } + }); + } catch (Exception e) { + LogUtils.error("清理已删除项目资源时发生错误", e); } }