Add delay to starting of generation tasks

Add a delay to the starts of the tasks so that they don't all start on the same tick. To completely avoid two tasks being executed on the same tick, the "period" config option should be set to the expected number of tasks or higher.
pull/29/head
trivernis 5 years ago
parent 6cb39ce843
commit ecc3165f1f

@ -2,6 +2,7 @@ package net.trivernis.chunkmaster.lib.generation
import io.papermc.lib.PaperLib
import net.trivernis.chunkmaster.Chunkmaster
import org.bukkit.Chunk
import org.bukkit.Server
import org.bukkit.World
@ -83,13 +84,14 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
center: ChunkCoordinates,
last: ChunkCoordinates,
id: Int,
stopAfter: Int = -1
stopAfter: Int = -1,
delay: Long = 200L
) {
if (!paused) {
chunkmaster.logger.info(chunkmaster.langManager.getLocalized("RESUME_FOR_WORLD", world.name))
val generationTask = createGenerationTask(world, center, last, stopAfter)
val task = server.scheduler.runTaskTimer(
chunkmaster, generationTask, 200, // 10 sec delay
chunkmaster, generationTask, delay, // 10 sec delay
chunkmaster.config.getLong("generation.period")
)
tasks.add(RunningTaskEntry(id, task, generationTask))
@ -149,11 +151,14 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
* Stops all generation tasks
*/
fun stopAll() {
saveProgress()
val removalSet = HashSet<RunningTaskEntry>()
for (task in tasks) {
task.generationTask.cancel()
val lastChunk = task.generationTask.lastChunkCoords
val id = task.id
chunkmaster.logger.info(chunkmaster.langManager.getLocalized("SAVING_TASK_PROGRESS", task.id))
saveProgressToDatabase(lastChunk, id)
task.task.cancel()
task.generationTask.cancel()
if (task.task.isCancelled) {
removalSet.add(task)
}
@ -167,7 +172,9 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
*/
fun startAll() {
chunkmaster.sqliteManager.executeStatement("SELECT * FROM generation_tasks", HashMap()) { res ->
var count = 0
while (res.next()) {
count++
try {
val id = res.getInt("id")
val world = server.getWorld(res.getString("world"))
@ -175,7 +182,7 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
val last = ChunkCoordinates(res.getInt("last_x"), res.getInt("last_z"))
val stopAfter = res.getInt("stop_after")
if (this.tasks.find { it.id == id } == null) {
resumeTask(world!!, center, last, id, stopAfter)
resumeTask(world!!, center, last, id, stopAfter, 200L + count)
}
} catch (error: NullPointerException) {
chunkmaster.logger.severe(chunkmaster.langManager.getLocalized("TASK_LOAD_FAILED", res.getInt("id")))
@ -238,18 +245,25 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
speed,
genTask.lastChunk.x,
genTask.lastChunk.z))
saveProgressToDatabase(genTask.lastChunkCoords, task.id)
} catch (error: Exception) {
chunkmaster.logger.warning(chunkmaster.langManager.getLocalized("TASK_SAVE_FAILED", error.toString()))
}
}
}
/**
* Saves the generation progress to the database
*/
private fun saveProgressToDatabase(lastChunk: ChunkCoordinates, id: Int) {
chunkmaster.sqliteManager.executeStatement(
"""
UPDATE generation_tasks SET last_x = ?, last_z = ?
WHERE id = ?
""".trimIndent(),
HashMap(mapOf(1 to genTask.lastChunk.x, 2 to genTask.lastChunk.z, 3 to task.id)),
HashMap(mapOf(1 to lastChunk.x, 2 to lastChunk.z, 3 to id)),
null
)
} catch (error: Exception) {
chunkmaster.logger.warning(chunkmaster.langManager.getLocalized("TASK_SAVE_FAILED", error.toString()))
}
}
}
/**

@ -17,7 +17,8 @@ abstract class GenerationTask(plugin: Chunkmaster, centerChunk: ChunkCoordinates
protected val spiral: Spiral =
Spiral(Pair(centerChunk.x, centerChunk.z), Pair(startChunk.x, startChunk.z))
protected val loadedChunks: HashSet<Chunk> = HashSet()
protected var lastChunkCoords = ChunkCoordinates(startChunk.x, startChunk.z)
var lastChunkCoords = ChunkCoordinates(startChunk.x, startChunk.z)
protected set
protected val chunkSkips = plugin.config.getInt("generation.chunk-skips-per-step")
protected val msptThreshold = plugin.config.getLong("generation.mspt-pause-threshold")
protected val maxLoadedChunks = plugin.config.getInt("generation.max-loaded-chunks")

@ -2,6 +2,7 @@ package net.trivernis.chunkmaster.lib.generation
import net.trivernis.chunkmaster.Chunkmaster
import org.bukkit.World
import java.lang.Exception
class GenerationTaskSpigot(
private val plugin: Chunkmaster, override val world: World,
@ -62,7 +63,11 @@ class GenerationTaskSpigot(
override fun cancel() {
for (chunk in loadedChunks) {
if (chunk.isLoaded) {
try {
chunk.unload(true)
} catch (e: Exception) {
plugin.logger.severe(e.toString())
}
}
}
}

@ -35,6 +35,7 @@ NO_PERMISSION = §cYou do not have the permission for this command!
SUBCOMMAND_NOT_FOUND = §cSubcommand §2%s §cnot found!
STOPPING_ALL_TASKS = Stopping all generation tasks...
SAVING_TASK_PROGRESS = Saving the progress of task #%s to the database...
DB_INIT = Initializing database...
DB_INIT_FINISHED = Database fully initialized.
DB_INIT_EROR = Failed to init database: %s.

@ -35,6 +35,7 @@ NO_PERMISSION = §cDu hast nicht die Rechte für diesen Befehl!
SUBCOMMAND_NOT_FOUND = §cUnteraktion §2%s §cwurde nicht gefunden!
STOPPING_ALL_TASKS = Stoppt alle Aufgaben...
SAVING_TASK_PROGRESS = Speichert den Fortschritt der Aufgabe #%s in der Datenbank...
DB_INIT = Initialisiere Datenbank...
DB_INIT_FINISHED = Die Datenbank wurde initialisiert.
DB_INIT_EROR = Fehler beim Initalisieren der Datenbank: %s.

@ -35,6 +35,7 @@ NO_PERMISSION = §cYou do not have the permission for this command!
SUBCOMMAND_NOT_FOUND = §cSubcommand §2%s §cnot found!
STOPPING_ALL_TASKS = Stopping all generation tasks...
SAVING_TASK_PROGRESS = Saving the progress of task #%s to the database...
DB_INIT = Initializing database...
DB_INIT_FINISHED = Database fully initialized.
DB_INIT_EROR = Failed to init database: %s.

Loading…
Cancel
Save