Skip to content

Commit

Permalink
feat: 流水线Job配额管理 TencentBlueKing#5154
Browse files Browse the repository at this point in the history
  • Loading branch information
sawyersong2 committed Oct 15, 2021
1 parent d532a93 commit 497c199
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,13 @@ interface BuildListener {
logger.warn("Fail to handle the shutdown message - ($event)", t)
} finally {
val jobQuotaService = getJobQuotaService()
jobQuotaService.removeRunningJob(event.projectId, event.buildId, event.vmSeqId, event.executeCount)
jobQuotaService.removeRunningJob(
projectId = event.projectId,
pipelineId = event.pipelineId,
buildId = event.buildId,
vmSeqId = event.vmSeqId,
executeCount = event.executeCount
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,19 @@ class JobQuotaService constructor(
}
}

fun removeRunningJob(projectId: String, buildId: String, vmSeqId: String?, executeCount: Int?) {
fun removeRunningJob(
projectId: String,
pipelineId: String,
buildId: String,
vmSeqId: String?,
executeCount: Int?
) {
if (jobQuotaEnable) {
logger.info("Remove running job to dispatch:[$projectId|$buildId|$vmSeqId]")
try {
client.get(ServiceJobQuotaBusinessResource::class).removeRunningJob(
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
vmSeqId = vmSeqId ?: "1",
executeCount = executeCount ?: 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@

package com.tencent.devops.dispatch.api

import com.tencent.devops.common.api.auth.AUTH_HEADER_DEVOPS_BUILD_ID
import com.tencent.devops.common.api.auth.AUTH_HEADER_DEVOPS_PIPELINE_ID
import com.tencent.devops.common.api.auth.AUTH_HEADER_DEVOPS_PROJECT_ID
import com.tencent.devops.common.api.auth.AUTH_HEADER_DEVOPS_VM_SEQ_ID
import com.tencent.devops.common.api.pojo.Result
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import javax.ws.rs.Consumes
import javax.ws.rs.DELETE
import javax.ws.rs.HeaderParam
import javax.ws.rs.POST
import javax.ws.rs.Path
import javax.ws.rs.PathParam
Expand All @@ -48,16 +53,19 @@ interface BuildJobQuotaBusinessResource {

@ApiOperation("上报一个Agent启动")
@POST
@Path("/agent/projects/{projectId}/builds/{buildId}/vmSeqs/{vmSeqId}")
@Path("/agent/start")
fun addRunningAgent(
@ApiParam(value = "projectId", required = true)
@PathParam("projectId")
@ApiParam("项目ID", required = true)
@HeaderParam(AUTH_HEADER_DEVOPS_PROJECT_ID)
projectId: String,
@ApiParam(value = "buildId", required = true)
@PathParam("buildId")
@ApiParam(value = "流水线ID", required = true)
@HeaderParam(AUTH_HEADER_DEVOPS_PIPELINE_ID)
pipelineId: String,
@ApiParam(value = "构建ID", required = true)
@HeaderParam(AUTH_HEADER_DEVOPS_BUILD_ID)
buildId: String,
@ApiParam(value = "vmSeqId", required = true)
@PathParam("vmSeqId")
@ApiParam(value = "构建job序号", required = true)
@HeaderParam(AUTH_HEADER_DEVOPS_VM_SEQ_ID)
vmSeqId: String,
@ApiParam(value = "executeCount", required = true)
@QueryParam("executeCount")
Expand All @@ -66,16 +74,19 @@ interface BuildJobQuotaBusinessResource {

@ApiOperation("上报一个Agent结束")
@DELETE
@Path("/agent/projects/{projectId}/builds/{buildId}/vmSeqs/{vmSeqId}")
@Path("/agent/shutdown")
fun removeRunningAgent(
@ApiParam(value = "projectId", required = true)
@PathParam("projectId")
@ApiParam("项目ID", required = true)
@HeaderParam(AUTH_HEADER_DEVOPS_PROJECT_ID)
projectId: String,
@ApiParam(value = "buildId", required = true)
@PathParam("buildId")
@ApiParam(value = "流水线ID", required = true)
@HeaderParam(AUTH_HEADER_DEVOPS_PIPELINE_ID)
pipelineId: String,
@ApiParam(value = "构建ID", required = true)
@HeaderParam(AUTH_HEADER_DEVOPS_BUILD_ID)
buildId: String,
@ApiParam(value = "vmSeqId", required = true)
@PathParam("vmSeqId")
@ApiParam(value = "构建job序号", required = true)
@HeaderParam(AUTH_HEADER_DEVOPS_VM_SEQ_ID)
vmSeqId: String,
@ApiParam(value = "executeCount", required = true)
@QueryParam("executeCount")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,14 @@ interface ServiceJobQuotaBusinessResource {

@ApiOperation("上报一个JOB结束")
@DELETE
@Path("/job/projects/{projectId}/builds/{buildId}/vmSeqs/{vmSeqId}")
@Path("/job/projects/{projectId}/pipelines/{pipelineId}/builds/{buildId}/vmSeqs/{vmSeqId}")
fun removeRunningJob(
@ApiParam(value = "projectId", required = true)
@PathParam("projectId")
projectId: String,
@ApiParam(value = "pipelineId", required = true)
@PathParam("pipelineId")
pipelineId: String,
@ApiParam(value = "buildId", required = true)
@PathParam("buildId")
buildId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,23 @@ class BuildJobQuotaBusinessResourceImpl @Autowired constructor(

override fun addRunningAgent(
projectId: String,
pipelineId: String,
buildId: String,
vmSeqId: String,
executeCount: Int
): Result<Boolean> {
jobQuotaBusinessService.updateAgentStartTime(projectId, buildId, vmSeqId, executeCount)
jobQuotaBusinessService.updateAgentStartTime(projectId, pipelineId, buildId, vmSeqId, executeCount)
return Result(true)
}

override fun removeRunningAgent(
projectId: String,
pipelineId: String,
buildId: String,
vmSeqId: String,
executeCount: Int
): Result<Boolean> {
jobQuotaBusinessService.updateRunningTime(projectId, buildId, vmSeqId, executeCount)
jobQuotaBusinessService.updateRunningTime(projectId, pipelineId, buildId, vmSeqId, executeCount)
return Result(true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ class ServiceJobQuotaBusinessResourceImpl @Autowired constructor(

override fun removeRunningJob(
projectId: String,
pipelineId: String,
buildId: String,
vmSeqId: String,
executeCount: Int
): Result<Boolean> {
jobQuotaBusinessService.deleteRunningJob(projectId, buildId, vmSeqId, executeCount)
jobQuotaBusinessService.deleteRunningJob(projectId, pipelineId, buildId, vmSeqId, executeCount)
return Result(true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ package com.tencent.devops.dispatch.pojo

data class JobQuotaHistory(
val projectId: String,
val pipelineId: String,
val buildId: String,
val vmSeqId: String,
val executeCount: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,21 @@ class JobQuotaBusinessService @Autowired constructor(
*/
fun deleteRunningJob(
projectId: String,
pipelineId: String,
buildId: String,
vmSeqId: String,
executeCount: Int
) {
LOG.info("$projectId|$buildId|$vmSeqId|$executeCount >>> update agent startTime.")
jobAgentFinish(projectId, buildId, vmSeqId, executeCount)
jobAgentFinish(projectId, pipelineId, buildId, vmSeqId, executeCount)
}

/**
* agent成功启动时更新
*/
fun updateAgentStartTime(
projectId: String,
pipelineId: String,
buildId: String,
vmSeqId: String,
executeCount: Int
Expand All @@ -127,12 +129,13 @@ class JobQuotaBusinessService @Autowired constructor(
*/
fun updateRunningTime(
projectId: String,
pipelineId: String,
buildId: String,
vmSeqId: String,
executeCount: Int
) {
LOG.info("$projectId|$buildId|$vmSeqId|$executeCount >>> update agent running time.")
jobAgentFinish(projectId, buildId, vmSeqId, executeCount)
jobAgentFinish(projectId, pipelineId, buildId, vmSeqId, executeCount)
}

private fun getProjectRunningJobStatus(projectId: String, vmType: JobQuotaVmType): JobQuotaStatus {
Expand Down Expand Up @@ -264,6 +267,7 @@ class JobQuotaBusinessService @Autowired constructor(

private fun jobAgentFinish(
projectId: String,
pipelineId: String,
buildId: String,
vmSeqId: String,
executeCount: Int
Expand Down Expand Up @@ -293,6 +297,7 @@ class JobQuotaBusinessService @Autowired constructor(
jobQuotaInterface.saveJobQuotaHistory(
JobQuotaHistory(
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
vmSeqId = vmSeqId,
executeCount = executeCount,
Expand Down

0 comments on commit 497c199

Please sign in to comment.