Skip to content

Commit 11dec16

Browse files
committed
Feature: Config periodic save
1 parent 8cfcf05 commit 11dec16

File tree

6 files changed

+38
-21
lines changed

6 files changed

+38
-21
lines changed

common/src/main/kotlin/com/lambda/config/Configurable.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,17 @@ import java.awt.Color
3131
*
3232
* @property settings A set of [AbstractSetting]s that this configurable manages.
3333
*/
34-
abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
34+
abstract class Configurable(
35+
private val configuration: Configuration
36+
) : Jsonable, Nameable {
3537
val settings = mutableSetOf<AbstractSetting<*>>()
3638

3739
init {
38-
configuration.configurables.add(this) // ToDo: Find non-leaking solution
40+
register()
3941
}
4042

43+
private fun register() = configuration.configurables.add(this)
44+
4145
override fun toJson() =
4246
JsonObject().apply {
4347
settings.forEach { setting ->

common/src/main/kotlin/com/lambda/config/Configuration.kt

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import com.lambda.util.StringUtils.capitalize
1515
import kotlinx.coroutines.Dispatchers
1616
import kotlinx.coroutines.launch
1717
import java.io.File
18+
import java.time.Duration
19+
import java.util.concurrent.Executors
20+
import kotlin.concurrent.fixedRateTimer
21+
1822

1923
/**
2024
* Represents a compound of [Configurable] objects whose [AbstractSetting]s
@@ -47,7 +51,18 @@ abstract class Configuration : Jsonable {
4751
}
4852

4953
// Avoid context-leaking warning
50-
private fun register() = configurations.add(this)
54+
private fun register() {
55+
fixedRateTimer(
56+
daemon = true,
57+
name = "Scheduler-config-${configName}",
58+
initialDelay = Duration.ofMinutes(5).toMillis(),
59+
period = Duration.ofMinutes(5).toMillis()
60+
) {
61+
trySave()
62+
}
63+
64+
configurations.add(this)
65+
}
5166

5267
override fun toJson() =
5368
JsonObject().apply {
@@ -109,19 +124,17 @@ abstract class Configuration : Jsonable {
109124
}
110125

111126
fun trySave() {
112-
lambdaScope.launch(Dispatchers.IO) {
113-
runCatching { save() }
114-
.onSuccess {
115-
val message = "Saved ${configName.capitalize()} config."
116-
LOG.info(message)
117-
this@Configuration.info(message)
118-
}
119-
.onFailure {
120-
val message = "Failed to save ${configName.capitalize()} config"
121-
LOG.error(message, it)
122-
this@Configuration.logError(message)
123-
}
124-
}
127+
runCatching { save() }
128+
.onSuccess {
129+
val message = "Saved ${configName.capitalize()} config."
130+
LOG.info(message)
131+
this@Configuration.info(message)
132+
}
133+
.onFailure {
134+
val message = "Failed to save ${configName.capitalize()} config"
135+
LOG.error(message, it)
136+
this@Configuration.logError(message)
137+
}
125138
}
126139

127140
companion object {

common/src/main/kotlin/com/lambda/config/configurations/FriendConfig.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import com.lambda.config.Configuration
44
import com.lambda.util.FolderRegister
55

66
object FriendConfig : Configuration() {
7-
override val configName = "friends"
7+
override val configName get() = "friends"
88
override val primary = FolderRegister.config.resolve("$configName.json")
99
}

common/src/main/kotlin/com/lambda/config/configurations/GuiConfig.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import com.lambda.config.Configuration
44
import com.lambda.util.FolderRegister
55

66
object GuiConfig : Configuration() {
7-
override val configName = "gui"
7+
override val configName get() = "gui"
88
override val primary = FolderRegister.config.resolve("$configName.json")
99
}

common/src/main/kotlin/com/lambda/config/configurations/LambdaConfig.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import com.lambda.config.Configuration
44
import com.lambda.util.FolderRegister
55

66
object LambdaConfig : Configuration() {
7-
override val configName = "lambda"
7+
override val configName get() = "lambda"
88
override val primary = FolderRegister.config.resolve("$configName.json")
9-
}
9+
}

common/src/main/kotlin/com/lambda/config/configurations/ModuleConfig.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ import com.lambda.util.FolderRegister
1515
* @property primary The primary file where the configuration is saved.
1616
*/
1717
object ModuleConfig : Configuration() {
18-
override val configName = "modules"
18+
override val configName get() = "modules"
1919
override val primary = FolderRegister.config.resolve("$configName.json")
2020
}

0 commit comments

Comments
 (0)