Skip to content

Commit f838ee6

Browse files
committed
pref: 构建机上的插件缓存文件如果损坏需重新从仓库下载覆盖 #12447
1 parent beca0c0 commit f838ee6

File tree

1 file changed

+36
-0
lines changed
  • src/backend/ci/core/worker/worker-common/src/main/kotlin/com/tencent/devops/worker/common/task/market

1 file changed

+36
-0
lines changed

src/backend/ci/core/worker/worker-common/src/main/kotlin/com/tencent/devops/worker/common/task/market/MarketAtomTask.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,23 @@ open class MarketAtomTask : ITask() {
465465
val fileCacheKey = "${atomData.atomCode}-${atomData.version}-$atomExecuteFileName"
466466
bkDiskLruFileCache.get(fileCacheKey, atomExecuteFile)
467467
try {
468+
// 新增:插件缓存文件本地缓存探测功能
469+
if (atomExecuteFile.exists() && atomExecuteFile.length() > 0) {
470+
// 缓存文件存在,检查完整性
471+
val isValid = verifyCachedFile(atomExecuteFile, atomData.shaContent)
472+
if (!isValid) {
473+
// 文件损坏,删除并从缓存中移除
474+
logger.warn(
475+
"Cached atom file is corrupted! " +
476+
"atomCode=${atomData.atomCode}, version=${atomData.version}, " +
477+
"file=${atomExecuteFile.absolutePath}"
478+
)
479+
atomExecuteFile.delete()
480+
bkDiskLruFileCache.remove(fileCacheKey)
481+
logger.info("Corrupted cache file deleted, will re-download from repo")
482+
}
483+
}
484+
468485
if (!atomExecuteFile.exists() || atomExecuteFile.length() < 1) {
469486
logger.info("local file[$atomExecuteFileName] is not exist,start downloading from the repo!")
470487
val cacheFlag = atomData.atomStatus !in setOf(
@@ -1133,6 +1150,25 @@ open class MarketAtomTask : ITask() {
11331150
return context
11341151
}
11351152

1153+
private fun verifyCachedFile(file: File, expectedSha1: String?): Boolean {
1154+
if (expectedSha1.isNullOrBlank()) {
1155+
// 没有SHA1信息,无法校验,认为有效
1156+
return true
1157+
}
1158+
return try {
1159+
val fileSha1 = file.inputStream().use { ShaUtils.sha1InputStream(it) }
1160+
val isValid = fileSha1 == expectedSha1
1161+
if (!isValid) {
1162+
logger.warn("SHA1 mismatch! expected=$expectedSha1, actual=$fileSha1")
1163+
}
1164+
isValid
1165+
} catch (e: Exception) {
1166+
// 校验过程出错(如文件读取失败),认为文件损坏
1167+
logger.warn("Failed to verify cached file SHA1", e)
1168+
false
1169+
}
1170+
}
1171+
11361172
companion object {
11371173
private const val DIR_ENV = "bk_data_dir"
11381174
private const val INPUT_ENV = "bk_data_input"

0 commit comments

Comments
 (0)