Skip to content

Commit

Permalink
Improve server initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rken committed Sep 15, 2024
1 parent bf88884 commit 7682fc9
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ bin/
### Mac OS ###
.DS_Store

zdatapath
zdatapath*

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/main/kotlin/eu/darken/octi/kserver/account/Account.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ data class Account(
data class Data(
@Contextual val id: AccountId = UUID.randomUUID(),
@Contextual val createdAt: Instant = Instant.now(),
)
) {
override fun toString(): String = "Account.Data(created=$createdAt, $id)"
}
}

typealias AccountId = UUID
53 changes: 28 additions & 25 deletions src/main/kotlin/eu/darken/octi/kserver/account/AccountRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,37 @@ class AccountRepo @Inject constructor(

init {
runBlocking {
Files.newDirectoryStream(accountsPath)
.filter {
if (it.isDirectory()) {
true
} else {
log(TAG, WARN) { "Not a directory: $it" }
false
Files.newDirectoryStream(accountsPath).use { stream ->
stream.asSequence()
.filter {
log(TAG, VERBOSE) { "Reading $it" }
if (it.isDirectory()) {
true
} else {
log(TAG, WARN) { "Not a directory: $it" }
false
}
}
}
.forEach { accDir ->
val configPath = accDir.resolve(ACC_FILENAME)
if (configPath.exists()) {
val accData = try {
serializer.decodeFromString<Account.Data>(configPath.readText())
} catch (e: IOException) {
log(TAG, ERROR) { "Failed to read $accDir: ${e.asLog()}" }
return@forEach
.map { it to it.resolve(ACC_FILENAME) }
.forEach { (accDir, configPath) ->
if (configPath.exists()) {
val accData = try {
serializer.decodeFromString<Account.Data>(configPath.readText())
} catch (e: IOException) {
log(TAG, ERROR) { "Failed to read $accDir: ${e.asLog()}" }
return@forEach
}
log(TAG) { "Account info loaded: $accData" }
accounts[accData.id] = Account(
data = accData,
path = accDir,
)
} else {
log(TAG, WARN) { "Missing account config for $accDir, cleaning up..." }
accDir.deleteRecursively()
}
log(TAG) { "Account info loaded: $accData" }
accounts[accData.id] = Account(
data = accData,
path = accDir,
)
} else {
log(TAG, WARN) { "Missing account config for $accDir, cleaning up..." }
accDir.deleteRecursively()
}
}
}

log(TAG, INFO) { "${accounts.size} accounts loaded into memory" }
}
Expand Down
26 changes: 12 additions & 14 deletions src/main/kotlin/eu/darken/octi/kserver/account/share/ShareRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ import java.time.Instant
import java.util.concurrent.ConcurrentHashMap
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.io.path.deleteIfExists
import kotlin.io.path.exists
import kotlin.io.path.readText
import kotlin.io.path.writeText
import kotlin.io.path.*
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds

Expand All @@ -46,23 +43,24 @@ class ShareRepo @Inject constructor(
.asSequence()
.mapNotNull { account ->
try {
Files
.newDirectoryStream(account.path.resolve(SHARES_DIR))
.map { account to it }
.toList().also {
log(TAG) { "Loading ${it.size} shares from account with ID=${account.id}" }
}
account.path.resolve(SHARES_DIR)
.takeIf { it.exists() }
?.listDirectoryEntries()
?.takeIf { it.isNotEmpty() }
?.map { account to it }
?.toList()
?.also { log(TAG) { "Loading ${it.size} shares from account ${account.id}" } }
} catch (e: IOException) {
log(TAG, ERROR) { "Failed to list shares for $account" }
log(TAG, ERROR) { "Failed to list shares for $account\n${e.asLog()}" }
null
}
}
.flatten()
.forEach { (account, path) ->
val data: Share.Data = try {
serializer.decodeFromString(path.readText())
val data = try {
serializer.decodeFromString<Share.Data>(path.readText())
} catch (e: IOException) {
log(TAG, ERROR) { "Failed to read $path: ${e.asLog()}" }
log(TAG, ERROR) { "Failed to read share $path: ${e.asLog()}" }
return@forEach
}
log(TAG) { "Share info loaded: $data" }
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/eu/darken/octi/kserver/device/Device.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ data class Device(
@Contextual val addedAt: Instant = Instant.now(),
@Contextual val lastSeen: Instant = Instant.now(),
) {
override fun toString(): String = "Device.Data($addedAt, $lastSeen, $id, ${password.take(16)})"
override fun toString(): String = "Device.Data(added=$addedAt, seen=$lastSeen, $id, ${password.take(8)}...)"
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/eu/darken/octi/kserver/device/DeviceRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import kotlinx.coroutines.sync.withLock
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import java.io.IOException
import java.nio.file.Files
import java.time.Duration
import java.time.Instant
import java.util.concurrent.ConcurrentHashMap
Expand All @@ -41,17 +40,18 @@ class DeviceRepo @Inject constructor(
.asSequence()
.mapNotNull { account ->
try {
Files
.newDirectoryStream(account.path.resolve(DEVICES_DIR))
account.path.resolve(DEVICES_DIR)
.listDirectoryEntries()
.map { account to it }
.also { log(TAG) { "Listing devices for ${account.id}" } }
.also { log(TAG, VERBOSE) { "Listing ${it.size} device(s) for account ${account.id}" } }
} catch (e: IOException) {
log(TAG, ERROR) { "Failed to list devices for $account" }
null
}
}
.flatten()
.forEach { (account, deviceDir) ->
log(TAG, VERBOSE) { "Reading $deviceDir" }
val deviceData = try {
serializer.decodeFromString<Device.Data>(deviceDir.resolve(DEVICE_FILENAME).readText())
} catch (e: IOException) {
Expand Down

0 comments on commit 7682fc9

Please sign in to comment.