Skip to content

Commit

Permalink
fix: deleteUnusedBlobs
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Dec 11, 2024
1 parent fc9161b commit 1954b9b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,24 @@ class BackupDatabaseService(
}
}

suspend fun deleteUnusedBlobs(): Int {
val used = dbQuery {
val column = Substring(BackupEntryTable.hash, intLiteral(3), intLiteral(30))
BackupEntryTable.select(
column // 32 -2 = 30
).withDistinct(true).map { row -> row[column] }
}.toSet()
val unused = getBlobFile("").toFile().walk().filter { it.isFile }.filterNot {
it.name in used
}.toList()
log.info("Deleting ${unused.size} unused blobs")
unused.forEach {
it.delete()
}
log.info("Deleted ${unused.size} unused blobs")
return unused.size
}

override suspend fun <T> dbQuery(block: suspend Transaction.() -> T): T =
newSuspendedTransaction(Dispatchers.IO, database, statement = block)

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ org.gradle.caching.debug=false
org.gradle.configureondemand=true

# Mod properties
mod.version=0.3.5
mod.version=0.3.6
mod.group=com.github.zly2006
mod.id=x-backup
mod.name=X Backup
Expand Down
9 changes: 9 additions & 0 deletions src/main/kotlin/com/github/zly2006/xbackup/Commands.kt
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,15 @@ object Commands {
1
}
}
literal("rm-unused-blobs") {
executes {
XBackup.ensureNotBusy {
val result = XBackup.service.deleteUnusedBlobs()
it.source.send(Text.literal("Deleted $result unused blobs"))
}
1
}
}
literal("upload") {
argument("id", IntegerArgumentType.integer(1)).executes {
val id = IntegerArgumentType.getInteger(it, "id")
Expand Down
21 changes: 21 additions & 0 deletions src/main/kotlin/com/github/zly2006/xbackup/Task.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.zly2006.xbackup

import net.minecraft.text.Text

interface Task {
val name: String
val displayName: Text
val startedAt: Long
val finishedAt: Long
val status: Status
val failure: Throwable?

enum class Status {
WAITING,
RUNNING,
FINISHED,
FAILED
}

suspend fun execute()
}
38 changes: 20 additions & 18 deletions src/main/kotlin/com/github/zly2006/xbackup/XBackup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ object XBackup : ModInitializer {
lateinit var config: Config
private val configPath = FabricLoader.getInstance().configDir.resolve("x-backup.config.json")
val log = LoggerFactory.getLogger("XBackup")!!
const val MOD_VERSION = /*$ mod_version*/ "0.3.4"
const val GIT_COMMIT = /*$ git_commit*/ "56ce0e2"
const val COMMIT_DATE = /*$ commit_date*/ "2024-12-10T00:12:23+08:00"
const val MOD_VERSION = /*$ mod_version*/ "0.3.6"
const val GIT_COMMIT = /*$ git_commit*/ "fc9161b"
const val COMMIT_DATE = /*$ commit_date*/ "2024-12-11T08:01:52+08:00"
lateinit var service: BackupDatabaseService
lateinit var server: MinecraftServer

Expand Down Expand Up @@ -215,26 +215,28 @@ object XBackup : ModInitializer {
service.cloudStorageProvider = OnedriveSupport(config, httpClient)
}
if (!config.mirrorMode) {
GlobalScope.launch(server.asCoroutineDispatcher()) {
while (XBackup.server.running) {
delay(1000)
val cs = service.cloudStorageProvider
if (service.activeTaskProgress != -1 &&
(cs.bytesSentLastSecond > 0 || cs.bytesReceivedLastSecond > 0)
) {
runCatching {
server.playerManager.sendToAll(
PlayerListHeaderS2CPacket(
Text.empty(),
Text.literal("X Backup Network Stat\n")
.append(Commands.networkStatsText())
startCrontabJob(server)
if (config.cloudBackupToken != null) {
GlobalScope.launch(server.asCoroutineDispatcher()) {
while (XBackup.server.running) {
delay(1000)
val cs = service.cloudStorageProvider
if (service.activeTaskProgress != -1 &&
(cs.bytesSentLastSecond > 0 || cs.bytesReceivedLastSecond > 0)
) {
runCatching {
server.playerManager.sendToAll(
PlayerListHeaderS2CPacket(
Text.empty(),
Text.literal("X Backup Network Stat\n")
.append(Commands.networkStatsText())
)
)
)
}
}
}
}
}
startCrontabJob(server)
}
}
ServerLifecycleEvents.SERVER_STOPPING.register {
Expand Down

0 comments on commit 1954b9b

Please sign in to comment.