From 9eac07503ad98431e7a593db8b1c74412456192f Mon Sep 17 00:00:00 2001 From: carlyin <449036249@qq.com> Date: Fri, 24 Mar 2023 16:22:08 +0800 Subject: [PATCH 1/3] =?UTF-8?q?pref:=E6=A8=A1=E6=9D=BF=E5=BC=82=E6=AD=A5?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BB=BB=E5=8A=A1=E6=8C=89=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E8=8E=B7=E5=8F=96redis=E9=94=81=20#8536?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/tencent/devops/common/service/Profile.kt | 4 ++++ .../service/template/TemplateInstanceCronService.kt | 13 +++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/backend/ci/core/common/common-service/src/main/kotlin/com/tencent/devops/common/service/Profile.kt b/src/backend/ci/core/common/common-service/src/main/kotlin/com/tencent/devops/common/service/Profile.kt index 3b899dee9fd..6d0dfee53c6 100644 --- a/src/backend/ci/core/common/common-service/src/main/kotlin/com/tencent/devops/common/service/Profile.kt +++ b/src/backend/ci/core/common/common-service/src/main/kotlin/com/tencent/devops/common/service/Profile.kt @@ -98,5 +98,9 @@ class Profile(private val environment: Environment) { } } + fun getActiveProfiles(): Array { + return activeProfiles + } + fun getApplicationName() = environment.getProperty("spring.application.name") } diff --git a/src/backend/ci/core/process/biz-process/src/main/kotlin/com/tencent/devops/process/engine/service/template/TemplateInstanceCronService.kt b/src/backend/ci/core/process/biz-process/src/main/kotlin/com/tencent/devops/process/engine/service/template/TemplateInstanceCronService.kt index 8d34d437617..44b3111f253 100644 --- a/src/backend/ci/core/process/biz-process/src/main/kotlin/com/tencent/devops/process/engine/service/template/TemplateInstanceCronService.kt +++ b/src/backend/ci/core/process/biz-process/src/main/kotlin/com/tencent/devops/process/engine/service/template/TemplateInstanceCronService.kt @@ -36,6 +36,8 @@ import com.tencent.devops.common.pipeline.pojo.BuildFormProperty import com.tencent.devops.common.pipeline.pojo.BuildNo import com.tencent.devops.common.redis.RedisLock import com.tencent.devops.common.redis.RedisOperation +import com.tencent.devops.common.service.Profile +import com.tencent.devops.common.service.utils.SpringContextUtil import com.tencent.devops.model.process.tables.records.TTemplateRecord import com.tencent.devops.process.constant.ProcessMessageCode import com.tencent.devops.process.engine.dao.template.TemplateDao @@ -82,10 +84,17 @@ class TemplateInstanceCronService @Autowired constructor( @Scheduled(cron = "0 0/1 * * * ?") fun templateInstance() { - val lock = RedisLock(redisOperation, LOCK_KEY, 3000) + val profile = SpringContextUtil.getBean(Profile::class.java) + val activeProfiles = profile.getActiveProfiles() + val key = if (activeProfiles.size > 1) { + "${activeProfiles[0]}:$LOCK_KEY" + } else { + LOCK_KEY + } + val lock = RedisLock(redisOperation, key, 3000) try { if (!lock.tryLock()) { - logger.info("get lock failed, skip") + logger.info("get lock[$key] failed, skip") return } val statusList = listOf(TemplateInstanceBaseStatus.INIT.name, TemplateInstanceBaseStatus.INSTANCING.name) From e97543a17219d03613713c67cad0206f6752e10f Mon Sep 17 00:00:00 2001 From: carlyin <449036249@qq.com> Date: Fri, 24 Mar 2023 16:35:06 +0800 Subject: [PATCH 2/3] =?UTF-8?q?pref:=E6=A8=A1=E6=9D=BF=E5=BC=82=E6=AD=A5?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BB=BB=E5=8A=A1=E6=8C=89=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E8=8E=B7=E5=8F=96redis=E9=94=81=20#8536?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../engine/service/template/TemplateInstanceCronService.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/backend/ci/core/process/biz-process/src/main/kotlin/com/tencent/devops/process/engine/service/template/TemplateInstanceCronService.kt b/src/backend/ci/core/process/biz-process/src/main/kotlin/com/tencent/devops/process/engine/service/template/TemplateInstanceCronService.kt index 44b3111f253..24d8ff406d2 100644 --- a/src/backend/ci/core/process/biz-process/src/main/kotlin/com/tencent/devops/process/engine/service/template/TemplateInstanceCronService.kt +++ b/src/backend/ci/core/process/biz-process/src/main/kotlin/com/tencent/devops/process/engine/service/template/TemplateInstanceCronService.kt @@ -87,7 +87,11 @@ class TemplateInstanceCronService @Autowired constructor( val profile = SpringContextUtil.getBean(Profile::class.java) val activeProfiles = profile.getActiveProfiles() val key = if (activeProfiles.size > 1) { - "${activeProfiles[0]}:$LOCK_KEY" + val sb = StringBuilder() + activeProfiles.forEach { activeProfile -> + sb.append("$activeProfile:") + } + sb.append(LOCK_KEY) } else { LOCK_KEY } From 4252009d6695ee0ad8eb30b046966184cdc899e2 Mon Sep 17 00:00:00 2001 From: carlyin <449036249@qq.com> Date: Fri, 24 Mar 2023 16:38:46 +0800 Subject: [PATCH 3/3] =?UTF-8?q?pref:=E6=A8=A1=E6=9D=BF=E5=BC=82=E6=AD=A5?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BB=BB=E5=8A=A1=E6=8C=89=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E8=8E=B7=E5=8F=96redis=E9=94=81=20#8536?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../engine/service/template/TemplateInstanceCronService.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/ci/core/process/biz-process/src/main/kotlin/com/tencent/devops/process/engine/service/template/TemplateInstanceCronService.kt b/src/backend/ci/core/process/biz-process/src/main/kotlin/com/tencent/devops/process/engine/service/template/TemplateInstanceCronService.kt index 24d8ff406d2..573e681183b 100644 --- a/src/backend/ci/core/process/biz-process/src/main/kotlin/com/tencent/devops/process/engine/service/template/TemplateInstanceCronService.kt +++ b/src/backend/ci/core/process/biz-process/src/main/kotlin/com/tencent/devops/process/engine/service/template/TemplateInstanceCronService.kt @@ -91,7 +91,7 @@ class TemplateInstanceCronService @Autowired constructor( activeProfiles.forEach { activeProfile -> sb.append("$activeProfile:") } - sb.append(LOCK_KEY) + sb.append(LOCK_KEY).toString() } else { LOCK_KEY }