logging
This commit is contained in:
1 parent
0143c6df7e
commit
603725a1ce
1 file changed
+86
-8
@@ -14,18 +14,27 @@ abstract class TaskPollerImplementation(
|
||||
) {
|
||||
private val log = KotlinLogging.logger {}
|
||||
|
||||
|
||||
open var backoff = Duration.ofSeconds(2)
|
||||
protected set
|
||||
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() {
|
||||
log.info { "TaskPoller starting with initial backoff=$backoff" }
|
||||
while (true) {
|
||||
try {
|
||||
pollOnce()
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
log.error(e) { "Unhandled error in poll loop, backing off for $backoff" }
|
||||
delay(backoff.toMillis())
|
||||
backoff = backoff.multipliedBy(2).coerceAtMost(maxBackoff)
|
||||
}
|
||||
@@ -43,30 +52,53 @@ abstract class TaskPollerImplementation(
|
||||
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() }
|
||||
var acceptedAny = false
|
||||
val results = mutableListOf<PollResult>()
|
||||
|
||||
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 accepted = try {
|
||||
listener.accept(task, reporter)
|
||||
} catch (e: Exception) {
|
||||
log.error("Error while processing task ${task.taskId} by listener ${listener.getWorkerId()}: ${e.message}")
|
||||
e.printStackTrace()
|
||||
log.error(e) { "Error while processing task ${task.taskId} by listener ${listener.getWorkerId()}" }
|
||||
false
|
||||
}
|
||||
|
||||
if (accepted) {
|
||||
log.debug { "Task ${task.taskId} accepted by ${listener.getWorkerId()}" }
|
||||
// Plain status (used for alignment) and colored status (used for display)
|
||||
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
|
||||
}
|
||||
|
||||
// Log a compact, column-aligned report
|
||||
log.debug { formatTaskReport(results) }
|
||||
|
||||
if (!acceptedAny) {
|
||||
log.debug { "No tasks were accepted. Backing off for $backoff" }
|
||||
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