diff --git a/src/backend/common/common-metadata/metadata-service/src/main/kotlin/com/tencent/bkrepo/common/metadata/service/node/impl/NodeDeleteSupport.kt b/src/backend/common/common-metadata/metadata-service/src/main/kotlin/com/tencent/bkrepo/common/metadata/service/node/impl/NodeDeleteSupport.kt index 6407fdb38f..689bff0006 100644 --- a/src/backend/common/common-metadata/metadata-service/src/main/kotlin/com/tencent/bkrepo/common/metadata/service/node/impl/NodeDeleteSupport.kt +++ b/src/backend/common/common-metadata/metadata-service/src/main/kotlin/com/tencent/bkrepo/common/metadata/service/node/impl/NodeDeleteSupport.kt @@ -226,11 +226,6 @@ open class NodeDeleteSupport( routerControllerClient.removeNodes(projectId, repoName, fullPath) } publishEvent(buildDeletedEvent(projectId, repoName, fullPath, operator)) - val blockCriteria = buildCriteria(projectId, repoName, fullPath, deleteTime) - val node = nodeDao.findOne(Query(blockCriteria)) - if (node?.sha256 == FAKE_SHA256) { - nodeBaseService.blockNodeService.deleteBlocks(projectId, repoName, fullPath) - } } } catch (exception: DuplicateKeyException) { logger.warn("Delete node[$resourceKey] by [$operator] error: [${exception.message}]") diff --git a/src/backend/generic/biz-generic/src/main/kotlin/com/tencent/bkrepo/generic/listener/BuildDeletedEventListener.kt b/src/backend/generic/biz-generic/src/main/kotlin/com/tencent/bkrepo/generic/listener/BuildDeletedEventListener.kt new file mode 100644 index 0000000000..55954f303c --- /dev/null +++ b/src/backend/generic/biz-generic/src/main/kotlin/com/tencent/bkrepo/generic/listener/BuildDeletedEventListener.kt @@ -0,0 +1,44 @@ +package com.tencent.bkrepo.generic.listener + +import com.tencent.bkrepo.common.artifact.api.ArtifactInfo +import com.tencent.bkrepo.common.artifact.event.base.ArtifactEvent +import com.tencent.bkrepo.common.artifact.event.base.EventType +import com.tencent.bkrepo.common.metadata.condition.SyncCondition +import com.tencent.bkrepo.common.metadata.constant.FAKE_SHA256 +import com.tencent.bkrepo.common.metadata.service.blocknode.BlockNodeService +import com.tencent.bkrepo.common.metadata.service.node.NodeService +import org.slf4j.LoggerFactory +import org.springframework.context.annotation.Conditional +import org.springframework.context.event.EventListener +import org.springframework.scheduling.annotation.Async +import org.springframework.stereotype.Component + +@Component +@Conditional(SyncCondition::class) +class BuildDeletedEventListener( + private val nodeService: NodeService, + private val blockNodeService: BlockNodeService +) { + + @Async + @EventListener(ArtifactEvent::class) + fun handle(event: ArtifactEvent) { + if (event.type == EventType.NODE_DELETED) { + logger.info("accept artifact delete event: $event") + consumer(event) + } + } + + private fun consumer(event: ArtifactEvent) { + with(event) { + val node = nodeService.getDeletedNodeDetail(ArtifactInfo(projectId, repoName, resourceKey)).firstOrNull() + if (node?.sha256 == FAKE_SHA256 && !node.folder) { + blockNodeService.deleteBlocks(projectId, repoName, resourceKey) + } + } + } + + companion object { + private val logger = LoggerFactory.getLogger(BuildDeletedEventListener::class.java) + } +} \ No newline at end of file