Skip to content

Commit

Permalink
feat: 流水线支持设置并发组执行策略 TencentBlueKing#6521
Browse files Browse the repository at this point in the history
  • Loading branch information
yongyiduan committed Apr 17, 2022
1 parent e0626f8 commit a5d5a38
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import org.springframework.stereotype.Repository
import java.sql.Timestamp
import java.time.LocalDateTime
import javax.ws.rs.core.Response
import org.jooq.Record2

@Suppress("ALL")
@Repository
Expand Down Expand Up @@ -171,12 +172,12 @@ class PipelineBuildDao {
dslContext: DSLContext,
projectId: String,
concurrencyGroup: String,
statusSet: BuildStatus
): Result<TPipelineBuildHistoryRecord?> {
statusSet: List<BuildStatus>
): List<Record2<String, String>> {
return with(T_PIPELINE_BUILD_HISTORY) {
dslContext.selectFrom(this)
dslContext.select(PIPELINE_ID, BUILD_ID).from(this)
.where(PROJECT_ID.eq(projectId))
.and(STATUS.eq(statusSet.ordinal))
.and(STATUS.`in`(statusSet.map { it.ordinal }))
.and(CONCURRENCY_GROUP.eq(concurrencyGroup))
.fetch()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,26 +123,15 @@ class QueueInterceptor @Autowired constructor(
Response(data = BuildStatus.RUNNING)
!concurrencyGroup.isNullOrBlank() && setting.concurrencyCancelInProgress -> {
// cancel-in-progress: true时, 若有相同 group 的流水线正在执行,则取消正在执行的流水线,新来的触发开始执行
val buildPipelineList = pipelineRuntimeService.getBuildInfoListByConcurrencyGroup(
pipelineRuntimeService.getBuildInfoListByConcurrencyGroup(
projectId = projectId,
concurrencyGroup = concurrencyGroup,
status = BuildStatus.RUNNING
).plus(
pipelineRuntimeService.getBuildInfoListByConcurrencyGroup(
projectId = projectId,
concurrencyGroup = concurrencyGroup,
status = BuildStatus.QUEUE
).apply {
pipelineRuntimeExtService.popAllConcurrencyGroupBuildInfo(this)
}
)

buildPipelineList.forEach { buildInfo ->
if (buildInfo == null) return@forEach
status = listOf(BuildStatus.RUNNING, BuildStatus.QUEUE)
).forEach { (pipelineId, buildId) ->
cancelBuildPipeline(
projectId = projectId,
pipelineId = buildInfo.pipelineId,
buildId = buildInfo.buildId,
pipelineId = pipelineId,
buildId = buildId,
userId = latestStartUser ?: task.pipelineInfo.creator,
groupName = concurrencyGroup
)
Expand Down Expand Up @@ -217,7 +206,7 @@ class QueueInterceptor @Autowired constructor(
buildLogPrinter.addRedLine(
buildId = buildId,
message = "$pipelineId] because concurrency cancel in progress with group($groupName)" +
", cancel all queue build",
", cancel all queue build",
tag = "QueueInterceptor",
jobId = "",
executeCount = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,29 +118,4 @@ class PipelineRuntimeExtService @Autowired constructor(
redisLock.unlock()
}
}

fun popAllConcurrencyGroupBuildInfo(
buildInfoList: List<BuildInfo?>
) {
buildInfoList.forEach { buildInfo ->
if (buildInfo == null) return@forEach
val redisLock = RedisLock(
redisOperation = redisOperation,
lockKey = "$nextBuildKey:${buildInfo.pipelineId}",
expiredTimeInSeconds = expiredTimeInSeconds
)
try {
redisLock.lock()
pipelineBuildDao.updateStatus(
dslContext = dslContext,
projectId = buildInfo.projectId,
buildId = buildInfo.buildId,
oldBuildStatus = BuildStatus.QUEUE,
newBuildStatus = BuildStatus.UNEXEC
)
} finally {
redisLock.unlock()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -263,16 +263,14 @@ class PipelineRuntimeService @Autowired constructor(
fun getBuildInfoListByConcurrencyGroup(
projectId: String,
concurrencyGroup: String,
status: BuildStatus
): List<BuildInfo?> {
status: List<BuildStatus>
): List<Pair<String, String>> {
return pipelineBuildDao.getBuildTasksByConcurrencyGroup(
dslContext = dslContext,
projectId = projectId,
concurrencyGroup = concurrencyGroup,
statusSet = status
).map {
pipelineBuildDao.convert(it)
}
).map { Pair(it.value1(), it.value2()) }
}

fun getBuildNoByByPair(buildIds: Set<String>, projectId: String?): MutableMap<String, String> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class BuildStartControl @Autowired constructor(
pipelineRuntimeService.getBuildInfoListByConcurrencyGroup(
projectId = projectId,
concurrencyGroup = it,
status = BuildStatus.RUNNING
status = listOf(BuildStatus.RUNNING)
).size
} ?: 0

Expand Down

0 comments on commit a5d5a38

Please sign in to comment.