Skip to content

Commit

Permalink
Merge pull request #5274 from fitzcao/issues_5273
Browse files Browse the repository at this point in the history
feat: 产物趋势逻辑调整 #5273
  • Loading branch information
irwinsun authored Oct 8, 2021
2 parents 0c766da + cf255a8 commit eed8f5f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,6 @@ class SamplePipelineBuildArtifactoryService @Autowired constructor(
return fileInfoList
}

override fun synArtifactoryInfo(
userId: String,
artifactList: List<FileInfo>,
projectId: String,
pipelineId: String,
buildId: String,
buildNum: Int
) = Unit

companion object {
private val logger = LoggerFactory.getLogger(SamplePipelineBuildArtifactoryService::class.java)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,90 +27,74 @@

package com.tencent.devops.artifactory.mq

import com.tencent.devops.artifactory.pojo.FileInfo
import com.tencent.devops.artifactory.service.PipelineBuildArtifactoryService
import com.tencent.devops.common.api.util.JsonUtil
import com.tencent.devops.common.client.Client
import com.tencent.devops.common.event.dispatcher.pipeline.PipelineEventDispatcher
import com.tencent.devops.common.event.listener.pipeline.BaseListener
import com.tencent.devops.common.event.pojo.pipeline.PipelineBuildFinishBroadCastEvent
import com.tencent.devops.process.api.service.ServicePipelineRuntimeResource
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

@Component
@Suppress("ALL")
class PipelineBuildArtifactoryListener @Autowired constructor(
pipelineEventDispatcher: PipelineEventDispatcher,
private val pipelineBuildArtifactoryService: PipelineBuildArtifactoryService,
private val client: Client
pipelineEventDispatcher: PipelineEventDispatcher
) : BaseListener<PipelineBuildFinishBroadCastEvent>(pipelineEventDispatcher) {

companion object {
private val logger = LoggerFactory.getLogger(PipelineBuildArtifactoryListener::class.java)!!
}

override fun run(event: PipelineBuildFinishBroadCastEvent) {
logger.info("PipelineBuildArtifactoryListener.run, event: $event")
val projectId = event.projectId
val buildId = event.buildId
val pipelineId = event.pipelineId

val startTime = System.currentTimeMillis()
val artifactList: List<FileInfo> = try {
pipelineBuildArtifactoryService.getArtifactList(projectId, pipelineId, buildId)
} catch (ignored: Throwable) {
logger.error("[$pipelineId]|getArtifactList-$buildId exception:", ignored)
emptyList()
}
logCostCall(startTime, buildId)
logger.info("[$pipelineId]|getArtifactList-$buildId artifact: ${JsonUtil.toJson(artifactList)}")

try {
if (artifactList.isEmpty()) {
return
}

val result = client.get(ServicePipelineRuntimeResource::class).updateArtifactList(
userId = event.userId,
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
artifactoryFileList = artifactList
)

logger.info("[$buildId]|update artifact result: ${result.status} ${result.message}")

if (result.isOk() && result.data != null) {
pipelineBuildArtifactoryService.synArtifactoryInfo(
userId = event.userId,
artifactList = artifactList,
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
buildNum = result.data!!.buildNum ?: 0
)
}
} catch (e: Exception) {
logger.error("[$buildId| update artifact list fail: ${e.localizedMessage}", e)
// rollback
client.get(ServicePipelineRuntimeResource::class).updateArtifactList(
userId = event.userId,
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
artifactoryFileList = emptyList()
)
}
}

fun logCostCall(startTime: Long, buildId: String) {
val cost = System.currentTimeMillis() - startTime
if (cost > 2000) {
logger.warn("$buildId - getArtifactList cost:$cost")
} else if (cost > 5000) {
logger.error("$buildId - getArtifactList cost:$cost")
}
return
// logger.info("PipelineBuildArtifactoryListener.run, event: $event")
// val projectId = event.projectId
// val buildId = event.buildId
// val pipelineId = event.pipelineId
//
// val startTime = System.currentTimeMillis()
// val artifactList: List<FileInfo> = try {
// pipelineBuildArtifactoryService.getArtifactList(projectId, pipelineId, buildId)
// } catch (ignored: Throwable) {
// logger.error("[$pipelineId]|getArtifactList-$buildId exception:", ignored)
// emptyList()
// }
// logger.info("[$pipelineId]|getArtifactList-$buildId artifact: ${JsonUtil.toJson(artifactList)}")
//
// try {
// if (artifactList.isEmpty()) {
// return
// }
//
// val result = client.get(ServicePipelineRuntimeResource::class).updateArtifactList(
// userId = event.userId,
// projectId = projectId,
// pipelineId = pipelineId,
// buildId = buildId,
// artifactoryFileList = artifactList
// )
//
// logger.info("[$buildId]|update artifact result: ${result.status} ${result.message}")
//
// if (result.isOk() && result.data != null) {
// pipelineBuildArtifactoryService.synArtifactoryInfo(
// userId = event.userId,
// artifactList = artifactList,
// projectId = projectId,
// pipelineId = pipelineId,
// buildId = buildId,
// buildNum = result.data!!.buildNum ?: 0
// )
// }
// } catch (e: Exception) {
// logger.error("[$buildId| update artifact list fail: ${e.localizedMessage}", e)
// // rollback
// client.get(ServicePipelineRuntimeResource::class).updateArtifactList(
// userId = event.userId,
// projectId = projectId,
// pipelineId = pipelineId,
// buildId = buildId,
// artifactoryFileList = emptyList()
// )
// }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,4 @@ import com.tencent.devops.artifactory.pojo.FileInfo
interface PipelineBuildArtifactoryService {

fun getArtifactList(projectId: String, pipelineId: String, buildId: String): List<FileInfo>

fun synArtifactoryInfo(
userId: String,
artifactList: List<FileInfo>,
projectId: String,
pipelineId: String,
buildId: String,
buildNum: Int
)
}

0 comments on commit eed8f5f

Please sign in to comment.