|
| 1 | +package ru.ifmo.se.dating.logic |
| 2 | + |
| 3 | +import kotlinx.coroutines.flow.Flow |
| 4 | +import ru.ifmo.se.dating.logging.Log |
| 5 | + |
| 6 | +class LoggingTransactionalOutbox<E, Id>( |
| 7 | + private val origin: TransactionalOutbox<E, Id> |
| 8 | +) : TransactionalOutbox<E, Id> by origin { |
| 9 | + private val log = Log.forClass(origin.javaClass) |
| 10 | + |
| 11 | + override fun publishable(): Flow<Id> = |
| 12 | + runCatching { origin.publishable() } |
| 13 | + .onSuccess { log.info("Retrieved publishable events") } |
| 14 | + .onFailure { log.warn("Failed to retrieve publishable events") } |
| 15 | + .getOrThrow() |
| 16 | + |
| 17 | + override suspend fun acquireById(id: Id): E = |
| 18 | + runCatching { origin.acquireById(id) } |
| 19 | + .onSuccess { event -> |
| 20 | + val status = if (isPublished(event)) { |
| 21 | + "a published" |
| 22 | + } else { |
| 23 | + "an unpublished" |
| 24 | + } |
| 25 | + log.info("Acquired $status event with id $id") |
| 26 | + } |
| 27 | + .onFailure { log.warn("Failed to acquire an event with id $id") } |
| 28 | + .getOrThrow() |
| 29 | + |
| 30 | + override suspend fun isPublished(event: E): Boolean = |
| 31 | + origin.isPublished(event) |
| 32 | + |
| 33 | + override suspend fun doProcess(event: E) = |
| 34 | + runCatching { origin.doProcess(event) } |
| 35 | + .onSuccess { log.info("Executed an event processing") } |
| 36 | + .onFailure { e -> log.warn("Failed to process an event: ${e.message}") } |
| 37 | + .getOrThrow() |
| 38 | + |
| 39 | + override suspend fun markPublished(event: E) = |
| 40 | + runCatching { origin.markPublished(event) } |
| 41 | + .onSuccess { log.info("Marked an event as published") } |
| 42 | + .onFailure { e -> log.warn("Failed to mark an event as published: ${e.message}") } |
| 43 | + .getOrThrow() |
| 44 | + |
| 45 | + override suspend fun process(id: Id) { |
| 46 | + log.info("Processing an event with id $id...") |
| 47 | + runCatching { super.process(id) } |
| 48 | + .onSuccess { log.info("An event with id $id was processed") } |
| 49 | + .onFailure { log.warn("Failed to process an event with id $id") } |
| 50 | + .getOrThrow() |
| 51 | + } |
| 52 | + |
| 53 | + override suspend fun recover() { |
| 54 | + log.info("Starting the recovery...") |
| 55 | + runCatching { super.recover() } |
| 56 | + .onSuccess { log.info("Recovery was completed.") } |
| 57 | + .onFailure { e -> log.warn("Recovery was failed", e) } |
| 58 | + .getOrThrow() |
| 59 | + } |
| 60 | +} |
0 commit comments