-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
137 additions
and
52 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
botalka/src/main/kotlin/ru/vityaman/lms/botalka/app/spring/task/SpringNotificationTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package ru.vityaman.lms.botalka.app.spring.task | ||
|
||
import kotlinx.coroutines.runBlocking | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.scheduling.annotation.Scheduled | ||
import org.springframework.stereotype.Component | ||
import ru.vityaman.lms.botalka.commons.Consumer | ||
import ru.vityaman.lms.botalka.core.logging.Slf4jLog | ||
import ru.vityaman.lms.botalka.core.model.Homework | ||
import ru.vityaman.lms.botalka.core.publication.PublicationSupplier | ||
import ru.vityaman.lms.botalka.core.publication.logging.loggingNotificationCallbacks | ||
import ru.vityaman.lms.botalka.core.publication.task.NotificationTask | ||
import java.util.concurrent.TimeUnit | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
@Component | ||
class SpringNotificationTask( | ||
@Value("\${task.scheduled.notification.batch-duration-seconds}") | ||
batchDurationSeconds: Int, | ||
|
||
supplier: PublicationSupplier, | ||
) { | ||
private val log = Slf4jLog("NotificationTask") | ||
|
||
private val logic = NotificationTask( | ||
supplier = supplier, | ||
consumer = object : Consumer<Homework> { | ||
override suspend fun accept(value: Homework) { | ||
log.info("Consumed $value") | ||
} | ||
}, | ||
config = NotificationTask.Config( | ||
batchDuration = batchDurationSeconds.seconds, | ||
), | ||
callbacks = loggingNotificationCallbacks(log), | ||
) | ||
|
||
@Scheduled( | ||
fixedRateString = | ||
"\${task.scheduled.notification.precision-seconds}", | ||
initialDelayString = | ||
"\${task.scheduled.notification.precision-seconds}", | ||
timeUnit = TimeUnit.SECONDS, | ||
) | ||
fun run(): Unit = runBlocking { start() } | ||
|
||
suspend fun start(): Unit = logic.run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
botalka/src/main/kotlin/ru/vityaman/lms/botalka/commons/ReactiveReplacement.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package ru.vityaman.lms.botalka.commons | ||
|
||
interface Runnable { | ||
suspend fun run() | ||
} | ||
|
||
interface Consumer<V> { | ||
suspend fun accept(value: V) | ||
} |
12 changes: 0 additions & 12 deletions
12
...ka/src/main/kotlin/ru/vityaman/lms/botalka/core/publication/LoggingPublicationConsumer.kt
This file was deleted.
Oops, something went wrong.
5 changes: 2 additions & 3 deletions
5
botalka/src/main/kotlin/ru/vityaman/lms/botalka/core/publication/PublicationConsumer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package ru.vityaman.lms.botalka.core.publication | ||
|
||
import ru.vityaman.lms.botalka.commons.Consumer | ||
import ru.vityaman.lms.botalka.core.model.Homework | ||
|
||
interface PublicationConsumer { | ||
suspend fun accept(homework: Homework) | ||
} | ||
interface PublicationConsumer : Consumer<Homework> |
5 changes: 0 additions & 5 deletions
5
botalka/src/main/kotlin/ru/vityaman/lms/botalka/core/publication/Runnable.kt
This file was deleted.
Oops, something went wrong.
3 changes: 2 additions & 1 deletion
3
...e/publication/KafkaPublicationConsumer.kt → ...ication/kafka/KafkaPublicationConsumer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...e/publication/KafkaPublicationSupplier.kt → ...ication/kafka/KafkaPublicationSupplier.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...n/kotlin/ru/vityaman/lms/botalka/core/publication/logging/LoggingNotificationCallbacks.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package ru.vityaman.lms.botalka.core.publication.logging | ||
|
||
import ru.vityaman.lms.botalka.core.logging.Log | ||
import ru.vityaman.lms.botalka.core.publication.task.NotificationTask | ||
|
||
fun loggingNotificationCallbacks(log: Log) = NotificationTask.Callbacks( | ||
onStart = { log.info("Starting...") }, | ||
onNext = { | ||
buildString { | ||
append("Received homework with ") | ||
append("id '${it.id}', ") | ||
append("title '${it.title}', ") | ||
append("moment '${it.publicationMoment}'") | ||
}.let { log.info(it) } | ||
}, | ||
onSuccess = { log.info("Processed homework with id '${it.id}'") }, | ||
onError = { log.warn("Task failed because '${it.message}'") }, | ||
onFinish = { log.info("Finished.") }, | ||
) |
3 changes: 2 additions & 1 deletion
3
...ublication/LoggingPublicationCallbacks.kt → ...on/logging/LoggingPublicationCallbacks.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
botalka/src/main/kotlin/ru/vityaman/lms/botalka/core/publication/task/NotificationTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package ru.vityaman.lms.botalka.core.publication.task | ||
|
||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.onEach | ||
import ru.vityaman.lms.botalka.commons.Consumer | ||
import ru.vityaman.lms.botalka.commons.Runnable | ||
import ru.vityaman.lms.botalka.commons.takeDuring | ||
import ru.vityaman.lms.botalka.core.model.Homework | ||
import ru.vityaman.lms.botalka.core.publication.PublicationSupplier | ||
import kotlin.time.Duration | ||
|
||
class NotificationTask( | ||
private val supplier: PublicationSupplier, | ||
private val consumer: Consumer<Homework>, | ||
private val config: Config, | ||
private val callbacks: Callbacks = Callbacks(), | ||
) : Runnable { | ||
override suspend fun run() { | ||
callbacks.onStart() | ||
supplier.events() | ||
.takeDuring(config.batchDuration) | ||
.onEach { callbacks.onNext(it.payload) } | ||
.onEach { consumer.accept(it.payload) } | ||
.onEach { callbacks.onSuccess(it.payload) } | ||
.catch { callbacks.onError(it) } | ||
.onEach { it.acknowledge() } | ||
.collect {} | ||
callbacks.onFinish() | ||
} | ||
|
||
data class Config( | ||
val batchDuration: Duration, | ||
) | ||
|
||
data class Callbacks( | ||
val onStart: suspend () -> Unit = {}, | ||
val onNext: suspend (Homework) -> Unit = {}, | ||
val onSuccess: suspend (Homework) -> Unit = {}, | ||
val onError: suspend (Throwable) -> Unit = {}, | ||
val onFinish: suspend () -> Unit = {}, | ||
) | ||
} |
4 changes: 3 additions & 1 deletion
4
...talka/core/publication/PublicationTask.kt → .../core/publication/task/PublicationTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters