override fun cleanupBuildsData()

in google-storage-server/src/main/kotlin/jetbrains/buildServer/serverSide/artifacts/google/cleanup/GoogleCleanupExtension.kt [26:62]


    override fun cleanupBuildsData(cleanupContext: BuildCleanupContext) {
        for (build in cleanupContext.builds) {
            val artifactsInfo = helper.getArtifactList(build) ?: continue
            val pathPrefix = GoogleUtils.getPathPrefix(artifactsInfo.commonProperties) ?: continue

            val pathsToDelete = ArtifactPathsEvaluator.getPathsToDelete(cleanupContext as BuildCleanupContextEx, build, artifactsInfo)
            if (pathsToDelete.isEmpty()) continue

            val parameters = settingsProvider.getStorageSettings(build)
            val bucket: Bucket
            try {
                bucket = GoogleUtils.getStorageBucket(parameters)
            } catch (e: Throwable) {
                Loggers.CLEANUP.debug("Failed to connect to bucket in Google Storage: ${e.message}")
                continue
            }

            var succeededNum = 0
            val blobs = pathsToDelete.map {
                GoogleUtils.getArtifactPath(artifactsInfo.commonProperties, it)
            }

            bucket.get(blobs)?.filterNotNull()?.forEach {
                try {
                    it.delete()
                    succeededNum++
                } catch (e: Throwable) {
                    Loggers.CLEANUP.debug("Failed to remove ${it.selfLink} from Google Storage: ${e.message}")
                }
            }

            val suffix = " from bucket [${bucket.name}] from path [$pathPrefix]"
            Loggers.CLEANUP.info("Removed [" + succeededNum + "] Google Storage " + StringUtil.pluralize("blob", succeededNum) + suffix)

            helper.removeFromArtifactList(build, pathsToDelete)
        }
    }