logging
This commit is contained in:
1 parent
0143c6df7e
commit
603725a1ce
1 file changed
+87
-9
@@ -14,18 +14,27 @@ abstract class TaskPollerImplementation(
|
|||||||
) {
|
) {
|
||||||
private val log = KotlinLogging.logger {}
|
private val log = KotlinLogging.logger {}
|
||||||
|
|
||||||
|
|
||||||
open var backoff = Duration.ofSeconds(2)
|
open var backoff = Duration.ofSeconds(2)
|
||||||
protected set
|
protected set
|
||||||
private val maxBackoff = Duration.ofMinutes(1)
|
private val maxBackoff = Duration.ofMinutes(1)
|
||||||
|
|
||||||
|
// Toggle ANSI colors in logs (set to false if your logging sink doesn't support ANSI)
|
||||||
|
protected open val enableColors: Boolean = true
|
||||||
|
|
||||||
|
private companion object {
|
||||||
|
const val ANSI_GREEN = "\u001B[32m"
|
||||||
|
const val ANSI_YELLOW = "\u001B[33m"
|
||||||
|
const val ANSI_RED = "\u001B[31m"
|
||||||
|
const val ANSI_RESET = "\u001B[0m"
|
||||||
|
}
|
||||||
|
|
||||||
open suspend fun start() {
|
open suspend fun start() {
|
||||||
log.info { "TaskPoller starting with initial backoff=$backoff" }
|
log.info { "TaskPoller starting with initial backoff=$backoff" }
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
pollOnce()
|
pollOnce()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
e.printStackTrace()
|
log.error(e) { "Unhandled error in poll loop, backing off for $backoff" }
|
||||||
delay(backoff.toMillis())
|
delay(backoff.toMillis())
|
||||||
backoff = backoff.multipliedBy(2).coerceAtMost(maxBackoff)
|
backoff = backoff.multipliedBy(2).coerceAtMost(maxBackoff)
|
||||||
}
|
}
|
||||||
@@ -43,30 +52,53 @@ abstract class TaskPollerImplementation(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.debug { "Found ${newPersistedTasks.size} persisted tasks" }
|
val count = newPersistedTasks.size
|
||||||
|
|
||||||
|
log.debug {
|
||||||
|
buildString {
|
||||||
|
append("Found $count pending ${if (count == 1) "task" else "tasks"}:\n")
|
||||||
|
newPersistedTasks.forEach { t ->
|
||||||
|
append("\t${t.taskId} - ${t.task}\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val tasks = newPersistedTasks.mapNotNull { it.toTask() }
|
val tasks = newPersistedTasks.mapNotNull { it.toTask() }
|
||||||
var acceptedAny = false
|
var acceptedAny = false
|
||||||
|
val results = mutableListOf<PollResult>()
|
||||||
|
|
||||||
for (task in tasks) {
|
for (task in tasks) {
|
||||||
val listener = TaskListenerRegistry.getListeners().firstOrNull { it.supports(task) && !it.isBusy } ?: continue
|
val listener = TaskListenerRegistry.getListeners()
|
||||||
|
.firstOrNull { it.supports(task) && !it.isBusy } ?: continue
|
||||||
|
|
||||||
val reporter = reporterFactory(task)
|
val reporter = reporterFactory(task)
|
||||||
val accepted = try {
|
val accepted = try {
|
||||||
listener.accept(task, reporter)
|
listener.accept(task, reporter)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
log.error("Error while processing task ${task.taskId} by listener ${listener.getWorkerId()}: ${e.message}")
|
log.error(e) { "Error while processing task ${task.taskId} by listener ${listener.getWorkerId()}" }
|
||||||
e.printStackTrace()
|
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
if (accepted) {
|
// Plain status (used for alignment) and colored status (used for display)
|
||||||
log.debug { "Task ${task.taskId} accepted by ${listener.getWorkerId()}" }
|
val (statusPlain, statusColored) = when {
|
||||||
|
accepted -> Pair("[ACCEPTED]", colorize("[ACCEPTED]", Color.GREEN))
|
||||||
|
else -> Pair("[IGNORED]", colorize("[IGNORED]", Color.YELLOW))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
results += PollResult(
|
||||||
|
taskId = task.taskId.toString(),
|
||||||
|
type = task::class.simpleName.toString(),
|
||||||
|
statusPlain = statusPlain,
|
||||||
|
statusColored = statusColored,
|
||||||
|
listener = listener.getWorkerId()
|
||||||
|
)
|
||||||
|
|
||||||
acceptedAny = acceptedAny || accepted
|
acceptedAny = acceptedAny || accepted
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Log a compact, column-aligned report
|
||||||
|
log.debug { formatTaskReport(results) }
|
||||||
|
|
||||||
if (!acceptedAny) {
|
if (!acceptedAny) {
|
||||||
log.debug { "No tasks were accepted. Backing off for $backoff" }
|
log.debug { "No tasks were accepted. Backing off for $backoff" }
|
||||||
delay(backoff.toMillis())
|
delay(backoff.toMillis())
|
||||||
@@ -77,4 +109,50 @@ abstract class TaskPollerImplementation(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
private enum class Color { GREEN, YELLOW, RED }
|
||||||
|
|
||||||
|
private fun colorize(text: String, color: Color): String {
|
||||||
|
if (!enableColors) return text
|
||||||
|
val code = when (color) {
|
||||||
|
Color.GREEN -> ANSI_GREEN
|
||||||
|
Color.YELLOW -> ANSI_YELLOW
|
||||||
|
Color.RED -> ANSI_RED
|
||||||
|
}
|
||||||
|
return "$code$text$ANSI_RESET"
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun formatTaskReport(results: List<PollResult>): String {
|
||||||
|
if (results.isEmpty()) return "\t(no tasks processed)"
|
||||||
|
|
||||||
|
// Compute widths based on plain (non-ANSI) status strings
|
||||||
|
val statusWidth = results.maxOf { it.statusPlain.length }
|
||||||
|
val idWidth = results.maxOf { it.taskId.length }
|
||||||
|
val typeWidth = results.maxOf { it.type.length }
|
||||||
|
|
||||||
|
return buildString {
|
||||||
|
append("Task processing report:\n")
|
||||||
|
results.forEach { r ->
|
||||||
|
// statusColored may contain ANSI codes; pad using plain length
|
||||||
|
val statusPadding = " ".repeat((statusWidth - r.statusPlain.length).coerceAtLeast(0))
|
||||||
|
append("\t")
|
||||||
|
append(r.statusColored)
|
||||||
|
append(statusPadding)
|
||||||
|
append(" ")
|
||||||
|
append(r.taskId.padEnd(idWidth))
|
||||||
|
append(" ")
|
||||||
|
append(r.type.padEnd(typeWidth))
|
||||||
|
append(" ")
|
||||||
|
append("(listener=${r.listener})")
|
||||||
|
append("\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal data class PollResult(
|
||||||
|
val taskId: String,
|
||||||
|
val type: String,
|
||||||
|
val statusPlain: String,
|
||||||
|
val statusColored: String,
|
||||||
|
val listener: String
|
||||||
|
)
|
||||||
|
}
|
||||||
Reference in new issue
Block a user