Skip to content

Commit

Permalink
feat: 支持清理上传路径 #1479
Browse files Browse the repository at this point in the history
  • Loading branch information
felixncheng committed Nov 23, 2023
1 parent 598bfbf commit 1911f9f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@
package com.tencent.bkrepo.common.storage.core

import com.tencent.bkrepo.common.storage.credentials.StorageCredentials
import com.tencent.bkrepo.common.storage.filesystem.FileSystemClient
import com.tencent.bkrepo.common.storage.filesystem.cleanup.CleanupFileVisitor
import com.tencent.bkrepo.common.storage.filesystem.cleanup.CleanupResult
import com.tencent.bkrepo.common.storage.util.toPath
import java.nio.file.Path

/**
* 文件清理操作实现类
Expand All @@ -39,8 +42,18 @@ abstract class CleanupSupport : HealthCheckSupport() {
override fun cleanUp(storageCredentials: StorageCredentials?): CleanupResult {
val credentials = getCredentialsOrDefault(storageCredentials)
val tempPath = getTempPath(credentials)
val visitor = CleanupFileVisitor(tempPath, tempPath, null, fileStorage, fileLocator, credentials)
getTempClient(credentials).walk(visitor)
return cleanupPath(tempPath, credentials)
.merge(cleanUploadPath(credentials))
}

protected fun cleanUploadPath(credentials: StorageCredentials): CleanupResult {
return cleanupPath(credentials.upload.location.toPath(), credentials)
.merge(cleanupPath(credentials.upload.localPath.toPath(), credentials))
}

private fun cleanupPath(path: Path, credentials: StorageCredentials): CleanupResult {
val visitor = CleanupFileVisitor(path, path, null, fileStorage, fileLocator, credentials)
FileSystemClient(path).walk(visitor)
return visitor.result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class CacheStorageService(
val stagingPath = getStagingPath(credentials)
val visitor = CleanupFileVisitor(rootPath, tempPath, stagingPath, fileStorage, fileLocator, credentials)
getCacheClient(credentials).walk(visitor)
return visitor.result
return visitor.result.merge(cleanUploadPath(credentials))
}

override fun synchronizeFile(storageCredentials: StorageCredentials?): SynchronizeResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,22 @@ data class CleanupResult(
var cleanupFile: Long = 0,
var cleanupFolder: Long = 0,
var cleanupSize: Long = 0,
var errorCount: Long = 0
var errorCount: Long = 0,
) {

fun merge(vararg others: CleanupResult): CleanupResult {
others.forEach {
totalFile += it.totalFile
totalFolder += it.totalFolder
totalSize += it.totalSize
cleanupFile += it.cleanupFile
cleanupFolder += it.cleanupFolder
cleanupSize += it.cleanupSize
errorCount += it.errorCount
}
return this
}

override fun toString(): String {
return "$cleanupFile/$totalFile[${HumanReadable.size(cleanupSize)}/${HumanReadable.size(totalSize)}] " +
"files deleted,errorCount[$errorCount], $cleanupFolder/$totalFolder dirs deleted."
Expand Down

0 comments on commit 1911f9f

Please sign in to comment.