Skip to content
Snippets Groups Projects
Verified Commit 808dfd1b authored by Jan-Niclas Strüwer's avatar Jan-Niclas Strüwer
Browse files

long running tasks now don't block the start of a new task

parent 140e4a62
No related branches found
No related tags found
No related merge requests found
package de.fraunhofer.iem.dataprovider.git package de.fraunhofer.iem.dataprovider.git
import de.fraunhofer.iem.dataprovider.taskManager.TaskManager
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
@Service @Service
class GitService(private val taskManager: TaskManager) { class GitService(private val taskManager: TaskManager) {
......
package de.fraunhofer.iem.dataprovider.git package de.fraunhofer.iem.dataprovider.taskManager
import de.fraunhofer.iem.dataprovider.logger.getLogger
import jakarta.annotation.PostConstruct import jakarta.annotation.PostConstruct
import jakarta.annotation.PreDestroy import jakarta.annotation.PreDestroy
import kotlinx.coroutines.* import kotlinx.coroutines.*
...@@ -7,6 +8,10 @@ import kotlinx.coroutines.channels.Channel ...@@ -7,6 +8,10 @@ import kotlinx.coroutines.channels.Channel
import org.springframework.stereotype.Component import org.springframework.stereotype.Component
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
enum class TaskType{
REPO_CHANGED
}
data class Task(val type: TaskType, val payload: Any)
@Component @Component
class TaskManager : CoroutineScope { class TaskManager : CoroutineScope {
...@@ -16,21 +21,25 @@ class TaskManager : CoroutineScope { ...@@ -16,21 +21,25 @@ class TaskManager : CoroutineScope {
// TODO: this could be dangerous to give it unlimited memory // TODO: this could be dangerous to give it unlimited memory
private val tasks = Channel<String>(Channel.UNLIMITED) private val tasks = Channel<String>(Channel.UNLIMITED)
private val numCores = Runtime.getRuntime().availableProcessors()
private val logger = getLogger(javaClass)
@PostConstruct @PostConstruct
fun startWorkers() { fun startWorkers() {
// TODO: spawn threads equal to number of cores logger.info("Starting $numCores workers.")
repeat(10) { repeat(numCores-1) {
println("[${Thread.currentThread().name}] launching coroutine") logger.info("[${Thread.currentThread().name}] launching coroutine")
launchWorker(it) launchWorker(it)
} }
} }
private fun launchWorker(id: Int) = launch { private fun launchWorker(id: Int) = launch {
for (task in tasks) { for (task in tasks) {
println("[${Thread.currentThread().name}] Processor #$id received $task") launch {
executeTask(task) logger.debug("[${Thread.currentThread().name}] Processor #$id received $task")
println("[${Thread.currentThread().name}] Processor #$id finished $task") executeTask(task)
logger.debug("[${Thread.currentThread().name}] Processor #$id finished $task")
}
} }
} }
......
spring.config.import=optional:classpath:.env[.properties] spring.config.import=optional:classpath:.env[.properties]
OPENCODE_GITLAB_URL=https://gitlab.opencode.de/ OPENCODE_GITLAB_URL=https://gitlab.opencode.de/
OPENCODE_GITLAB_TOKEN=${OPENCODE_TOKEN} OPENCODE_GITLAB_TOKEN=${OPENCODE_TOKEN}
spring.r2dbc.url=${R2DBC_URL} spring.r2dbc.url=${R2DBC_URL}
\ No newline at end of file logging.level.root=DEBUG
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment

Consent

On this website, we use the web analytics service Matomo to analyze and review the use of our website. Through the collected statistics, we can improve our offerings and make them more appealing for you. Here, you can decide whether to allow us to process your data and set corresponding cookies for these purposes, in addition to technically necessary cookies. Further information on data protection—especially regarding "cookies" and "Matomo"—can be found in our privacy policy. You can withdraw your consent at any time.