Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Tencent/bk-ci into issue5…
Browse files Browse the repository at this point in the history
…408_github_branch
  • Loading branch information
sawyersong2 committed Nov 2, 2021
2 parents ae2348a + cfd7536 commit 891563f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@

package com.tencent.devops.dispatch.listener

import com.tencent.devops.common.client.Client
import com.tencent.devops.common.pipeline.enums.BuildStatus
import com.tencent.devops.common.pipeline.enums.ChannelCode
import com.tencent.devops.dispatch.service.PipelineDispatchService
import com.tencent.devops.process.api.service.ServiceBuildResource
import com.tencent.devops.process.pojo.mq.PipelineAgentShutdownEvent
import com.tencent.devops.process.pojo.mq.PipelineAgentStartupEvent
import feign.RetryableException
Expand All @@ -36,11 +40,15 @@ import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class ThirdPartyAgentListener @Autowired
constructor(private val pipelineDispatchService: PipelineDispatchService) {
class ThirdPartyAgentListener @Autowired constructor(
private val pipelineDispatchService: PipelineDispatchService,
private val client: Client
) {
fun listenAgentStartUpEvent(pipelineAgentStartupEvent: PipelineAgentStartupEvent) {
try {
pipelineDispatchService.startUp(pipelineAgentStartupEvent)
if (checkRunning(pipelineAgentStartupEvent)) {
pipelineDispatchService.startUp(pipelineAgentStartupEvent)
}
} catch (e: RetryableException) {
logger.warn("[${pipelineAgentStartupEvent.buildId}]|feign fail, do retry again", e)
pipelineDispatchService.reDispatch(pipelineAgentStartupEvent)
Expand All @@ -57,6 +65,30 @@ constructor(private val pipelineDispatchService: PipelineDispatchService) {
}
}

private fun checkRunning(event: PipelineAgentStartupEvent): Boolean {
// 判断流水线是否还在运行,如果已经停止则不在运行
// 只有detail的信息是在shutdown事件发出之前就写入的,所以这里去builddetail的信息。
// 为了兼容gitci的权限,这里把渠道号都改成GIT,以便去掉用户权限验证
val record = client.get(ServiceBuildResource::class).getBuildDetailStatusWithoutPermission(
event.userId,
event.projectId,
event.pipelineId,
event.buildId,
ChannelCode.BS
)
if (record.isNotOk() || record.data == null) {
logger.warn("The build event($event) fail to check if pipeline is running because of ${record.message}")
return false
}
val status = BuildStatus.parse(record.data)
if (!status.isRunning()) {
logger.error("The build event($event) is not running")
return false
}

return true
}

companion object {
private val logger = LoggerFactory.getLogger(ThirdPartyAgentListener::class.java)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,8 @@ class ThirdPartyAgentDispatcher @Autowired constructor(
)
return
}
log(buildLogPrinter, event, "retry: ${event.retryTime + 1} | $errorMessage")

event.retryTime += 1
event.delayMills = 10000
pipelineEventDispatcher.dispatch(event)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ class ThirdPartyAgentDao {
with(TEnvironmentThirdpartyAgent.T_ENVIRONMENT_THIRDPARTY_AGENT) {
return dslContext.selectFrom(this)
.where(ID.eq(id))
.and(PROJECT_ID.eq(projectId))
.fetchOne()
}
}
Expand Down

0 comments on commit 891563f

Please sign in to comment.