Add Comments

This commit is contained in:
2025-03-03 21:08:21 +01:00
parent ae5c229e4b
commit de3d4a1021
18 changed files with 111 additions and 12 deletions

View File

@@ -4,6 +4,9 @@ import eventDemo.plugins.CommandIdSerializer
import kotlinx.serialization.Serializable
import java.util.UUID
/**
* An ID for the [Command]
*/
@JvmInline
@Serializable(with = CommandIdSerializer::class)
value class CommandId(
@@ -14,6 +17,11 @@ value class CommandId(
override fun toString(): String = id.toString()
}
/**
* Interface to represent a Command.
*
* A command is a request for an action.
*/
interface Command {
val id: CommandId
val name: String

View File

@@ -2,6 +2,11 @@ package eventDemo.libs.command
import kotlin.reflect.KClass
/**
* Represent a Command stream.
*
* The stream contains a list of all actions yet to be executed.
*/
interface CommandStream<C : Command> {
/**
* Send a new [Command] to the queue.
@@ -22,7 +27,7 @@ interface CommandStream<C : Command> {
}
/**
* A class to implement succes/faild action.
* A class to implement success/failed action.
*/
interface ComputeStatus {
fun ack()
@@ -30,7 +35,10 @@ interface CommandStream<C : Command> {
fun nack()
}
suspend fun process(block: CommandBlock<C>)
/**
* Apply an action to all command income in the stream.
*/
suspend fun process(action: CommandBlock<C>)
}
suspend inline fun <reified C : Command> CommandStream<C>.send(vararg command: C) = send(C::class, *command)

View File

@@ -31,18 +31,18 @@ abstract class CommandStreamInMemory<C : Command> : CommandStream<C> {
queue.send(command)
}
override suspend fun process(block: CommandBlock<C>) {
override suspend fun process(action: CommandBlock<C>) {
queue.consumeEach { command ->
compute(command, block)
compute(command, action)
}
for (command in queue) {
compute(command, block)
compute(command, action)
}
}
private fun compute(
command: C,
block: CommandBlock<C>,
action: CommandBlock<C>,
) {
val status = object : CommandStream.ComputeStatus {
var isSet: Boolean = false
@@ -58,7 +58,7 @@ abstract class CommandStreamInMemory<C : Command> : CommandStream<C> {
}
}
if (runCatching { status.block(command) }.isFailure) {
if (runCatching { status.action(command) }.isFailure) {
markAsFailed(command)
} else if (!status.isSet) {
status.ack()
@@ -75,7 +75,7 @@ abstract class CommandStreamInMemory<C : Command> : CommandStream<C> {
private fun <C : Command> markAsFailed(command: C) {
failedCommand.add(command)
logger.atWarn {
message = "Compute command FAILD and it put on the top of the stack : $command"
message = "Compute command FAILED and it put it ot the top of the stack : $command"
payload = mapOf("command" to command)
}
}