Skip to content

Commit

Permalink
Merge pull request #8537 from carlyin0801/issue_8536_template_instanc…
Browse files Browse the repository at this point in the history
…e_update_fix

pref:模板异步更新任务按环境获取redis锁 #8536
  • Loading branch information
irwinsun authored Mar 24, 2023
2 parents a991d1e + 4252009 commit 48415b7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,9 @@ class Profile(private val environment: Environment) {
}
}

fun getActiveProfiles(): Array<String> {
return activeProfiles
}

fun getApplicationName() = environment.getProperty("spring.application.name")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -82,10 +84,21 @@ 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) {
val sb = StringBuilder()
activeProfiles.forEach { activeProfile ->
sb.append("$activeProfile:")
}
sb.append(LOCK_KEY).toString()
} 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)
Expand Down

0 comments on commit 48415b7

Please sign in to comment.