Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Dec 6, 2024
1 parent 5b67e97 commit 50b0418
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package com.github.zly2006.xbackup.api
import java.io.InputStream

interface CloudStorageProvider {
val bytesSentLastSecond: Long
val bytesReceivedLastSecond: Long

suspend fun uploadBackup(service: XBackupKotlinAsyncApi, id: Int)

suspend fun downloadBlob(hash: String): InputStream
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.zly2006.xbackup.api;

import kotlin.LateinitKt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -34,4 +35,6 @@ static XBackupApi setInstance(XBackupApi instance) {
int backupCount();

void setCloudStorageProvider(@NotNull CloudStorageProvider provider);

@NotNull CloudStorageProvider getCloudStorageProvider();
}
11 changes: 6 additions & 5 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ dependencies {
sharedLib("org.jetbrains.exposed:exposed-json:$exposed_version")
sharedLib("org.xerial:sqlite-jdbc:3.46.0.0")
sharedLib("org.apache.commons:commons-compress:1.26.0")
val ktor_version = property("deps.ktor_version") as String
sharedLib("io.ktor:ktor-client-content-negotiation-jvm:$ktor_version")
sharedLib("io.ktor:ktor-client-core-jvm:$ktor_version")
sharedLib("io.ktor:ktor-client-apache5:$ktor_version")
sharedLib("io.ktor:ktor-serialization-kotlinx-json-jvm:$ktor_version")
val ktorVersion = property("deps.ktor_version") as String
sharedLib("io.ktor:ktor-client-content-negotiation-jvm:$ktorVersion")
sharedLib("io.ktor:ktor-client-core-jvm:$ktorVersion")
sharedLib("io.ktor:ktor-client-cio:$ktorVersion")
sharedLib("io.ktor:ktor-client-apache5:$ktorVersion")
sharedLib("io.ktor:ktor-serialization-kotlinx-json-jvm:$ktorVersion")
sharedLib(project(":api"))
// kotlin
api("org.jetbrains.kotlin:kotlin-stdlib-jdk8:2.0.21")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,15 @@ class BackupDatabaseService(
}
}

lateinit var oneDriveService: CloudStorageProvider
private lateinit var oneDriveService: CloudStorageProvider

override fun setCloudStorageProvider(provider: CloudStorageProvider) {
oneDriveService = provider
}

override fun getCloudStorageProvider(): CloudStorageProvider {
return oneDriveService
}

private val ignoredFiles = setOf(
"", // empty string is the root directory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ import com.github.zly2006.xbackup.api.CloudStorageProvider
import com.github.zly2006.xbackup.api.XBackupKotlinAsyncApi
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.plugins.onDownload
import io.ktor.client.plugins.onUpload
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import kotlinx.atomicfu.atomic
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlin.random.Random
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.sync.withPermit
import kotlinx.serialization.json.JsonObject
Expand All @@ -31,6 +35,21 @@ class OnedriveSupport(
): CloudStorageProvider {
private var tokenExpires = 0L
private var token = ""
private val receivedBytes = atomic(0L)
private val sentBytes = atomic(0L)
override var bytesReceivedLastSecond = 0L
override var bytesSentLastSecond = 0L
private val job = GlobalScope.launch {
while (true) {
delay(1000)
bytesReceivedLastSecond = receivedBytes.getAndSet(0)
bytesSentLastSecond = sentBytes.getAndSet(0)
}
}

init {
job.invokeOnCompletion { cause -> log.info("Network stats tracker stopped", cause) }
}

private suspend fun getOneDriveToken(): String {
if (tokenExpires < System.currentTimeMillis()) {
Expand Down Expand Up @@ -94,6 +113,12 @@ class OnedriveSupport(
header("Content-Type", "application/octet-stream")
header("Content-Length", entry.zippedSize.toString())
setBody(service.getBlobFile(entry.hash).readBytes())

var lastSent = 0L
onUpload { bytesSentTotal, contentLength ->
sentBytes.addAndGet(bytesSentTotal - lastSent)
lastSent = bytesSentTotal
}
}.let { response ->
if (!response.status.isSuccess()) {
throw IllegalStateException("Upload failed $entry: ${response.status} ${response.bodyAsText()}")
Expand Down Expand Up @@ -147,6 +172,12 @@ class OnedriveSupport(
}
httpClient.get("https://graph.microsoft.com/v1.0/me/drive/root:$prefix/$hash:/content") {
header("Authorization", "Bearer $token")

var lastReceived = 0L
onDownload { bytesSentTotal, contentLength ->
receivedBytes.addAndGet(bytesSentTotal - lastReceived)
lastReceived = bytesSentTotal
}
}.let { response ->
if (!response.status.isSuccess()) {
throw IllegalStateException("Download failed: ${response.status} ${response.bodyAsText()}")
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.4-pre.6
mod.version=0.3.4-pre.7
mod.group=com.github.zly2006
mod.id=x-backup
mod.name=X Backup
Expand Down
10 changes: 10 additions & 0 deletions src/main/kotlin/com/github/zly2006/xbackup/Commands.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ fun MutableText.clickRun(cmd: String) {
}

object Commands {
fun networkStatsText(): MutableText {
val cloudStorage = XBackup.service.cloudStorageProvider
return Text.empty().apply {
append(Text.literal("" + sizeToString(cloudStorage.bytesSentLastSecond) + "/s"))
append(" ")
append(Text.literal("" + sizeToString(cloudStorage.bytesReceivedLastSecond) + "/s"))
}
}

private fun getBackup(id: Int): BackupDatabaseService.Backup {
return runBlocking {
XBackup.service.getBackupInternal(id)
Expand All @@ -115,6 +124,7 @@ object Commands {
it.source.send(Utils.translate("command.xb.background_task_status", XBackup.backgroundState.toString()))
if (XBackup.service.activeTaskProgress != -1) {
it.source.send(Text.literal("云备份任务:${XBackup.service.activeTask} ${XBackup.service.activeTaskProgress}%"))
it.source.send(networkStatsText())
}
GlobalScope.launch(it.source.server.asCoroutineDispatcher()) {
val status = XBackup.service.status()
Expand Down
25 changes: 21 additions & 4 deletions src/main/kotlin/com/github/zly2006/xbackup/XBackup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents
import net.fabricmc.loader.api.FabricLoader
import net.minecraft.client.MinecraftClient
import net.minecraft.network.packet.s2c.play.PlayerListHeaderS2CPacket
import net.minecraft.server.MinecraftServer
import net.minecraft.server.command.ServerCommandSource
import net.minecraft.text.Text
Expand All @@ -38,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-pre.5"
const val GIT_COMMIT = /*$ git_commit*/ "9a47902"
const val COMMIT_DATE = /*$ commit_date*/ "2024-12-05T23:35:08+08:00"
const val MOD_VERSION = /*$ mod_version*/ "0.3.4-pre.7"
const val GIT_COMMIT = /*$ git_commit*/ "5b67e97"
const val COMMIT_DATE = /*$ commit_date*/ "2024-12-06T07:16:48+08:00"
lateinit var service: BackupDatabaseService
lateinit var server: MinecraftServer

Expand Down Expand Up @@ -205,9 +206,25 @@ object XBackup : ModInitializer {
retryOnServerErrors(1)
}
}
service.oneDriveService = OnedriveSupport(config, httpClient)
service.cloudStorageProvider = OnedriveSupport(config, httpClient)
}
if (!config.mirrorMode) {
GlobalScope.launch(server.asCoroutineDispatcher()) {
while (XBackup.server.running) {
delay(1000)
if (service.activeTaskProgress != -1) {
runCatching {
server.playerManager.sendToAll(
PlayerListHeaderS2CPacket(
Text.empty(),
Text.literal("X Backup Network Stat\n")
.append(Commands.networkStatsText())
)
)
}
}
}
}
startCrontabJob(server)
}
}
Expand Down

0 comments on commit 50b0418

Please sign in to comment.