Skip to content

Commit

Permalink
fix: 修复过期节点删除任务导致统计数据不准的问题 TencentBlueKing#1323 (TencentBlueKing#1486)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaoxuwan authored Nov 25, 2023
1 parent 07d0061 commit c4db370
Showing 1 changed file with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@

package com.tencent.bkrepo.job.batch

import com.tencent.bkrepo.common.mongo.constant.ID
import com.tencent.bkrepo.job.SHARDING_COUNT
import com.tencent.bkrepo.job.batch.base.DefaultContextMongoDbJob
import com.tencent.bkrepo.job.batch.base.JobContext
import com.tencent.bkrepo.job.batch.utils.TimeUtils
import com.tencent.bkrepo.job.config.properties.ExpiredNodeMarkupJobProperties
import com.tencent.bkrepo.repository.api.NodeClient
import com.tencent.bkrepo.repository.constant.SYSTEM_USER
import com.tencent.bkrepo.repository.pojo.node.service.NodeDeleteRequest
import org.slf4j.LoggerFactory
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.data.mongodb.core.query.Criteria
import org.springframework.data.mongodb.core.query.Query
import org.springframework.data.mongodb.core.query.Update
import org.springframework.data.mongodb.core.query.and
import org.springframework.data.mongodb.core.query.isEqualTo
import org.springframework.data.mongodb.core.query.where
Expand All @@ -51,10 +52,13 @@ import java.time.LocalDateTime
@EnableConfigurationProperties(ExpiredNodeMarkupJobProperties::class)
class ExpiredNodeMarkupJob(
properties: ExpiredNodeMarkupJobProperties,
private val nodeClient: NodeClient
) : DefaultContextMongoDbJob<ExpiredNodeMarkupJob.Node>(properties) {

data class Node(
val id: String,
val projectId: String,
val repoName: String,
val fullPath: String,
val expireDate: LocalDateTime,
val deleted: LocalDateTime?
)
Expand All @@ -78,7 +82,9 @@ class ExpiredNodeMarkupJob(

override fun mapToEntity(row: Map<String, Any?>): Node {
return Node(
row[ID].toString(),
row[Node::projectId.name].toString(),
row[Node::repoName.name].toString(),
row[Node::fullPath.name].toString(),
TimeUtils.parseMongoDateTimeStr(row[Node::expireDate.name].toString())!!,
TimeUtils.parseMongoDateTimeStr(row[Node::deleted.name].toString())
)
Expand All @@ -89,15 +95,15 @@ class ExpiredNodeMarkupJob(
}

override fun run(row: Node, collectionName: String, context: JobContext) {
val query = Query.query(Criteria.where(ID).isEqualTo(row.id))
mongoTemplate.updateFirst(
query,
Update().set(Node::deleted.name, LocalDateTime.now()),
collectionName
)
try {
nodeClient.deleteNode(NodeDeleteRequest(row.projectId, row.repoName, row.fullPath, SYSTEM_USER))
} catch (e: Exception) {
logger.warn("delete expired node[$row] failed: $e")
}
}

companion object {
private const val COLLECTION_NAME_PREFIX = "node_"
private val logger = LoggerFactory.getLogger(ExpiredNodeMarkupJob::class.java)
}
}

0 comments on commit c4db370

Please sign in to comment.