Skip to content

Commit

Permalink
Refactor to have separate modules for transport and pubsub (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
vooft authored Aug 4, 2024
1 parent 1db458f commit 48b3488
Show file tree
Hide file tree
Showing 50 changed files with 469 additions and 502 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ dependencies {
```kotlin
val dataSource = createMyDataSource()

val kueue = Kueue.jdbc(dataSource)
val kueue = KueuePubSub.jdbc(dataSource)
val subscription = kueue.subscribe(KueueTopic("my_topic")) { message: String ->
println("Received message: $message")
}

kueue.send(KueueTopic("my_topic"), "Hello, world!")
kueue.publish(KueueTopic("my_topic"), "Hello, world!")
// will print after a tiny delay: "Received message: Hello, world!"
```

Expand All @@ -60,18 +60,18 @@ kueue.close()
```

## Transactional usage
To send a message using existing transaction, you should provide the transactional connection.
To publish a message using existing transaction, you should provide the transactional connection.

Normally, API accepts a instance of a wrapped connection `KueueConnection`, there is a helper method to create it:
```kotlin
val transactionalConnection = myBeginTransaction()
kueue.send(KueueTopic("my_topic"), "Hello, world!", kueue.wrap(transactionalConnection))
kueue.publish(KueueTopic("my_topic"), "Hello, world!", kueue.wrap(transactionalConnection))
```

There is also an extension function for a specific library to simplify transactional sending:
There is also an extension function for a specific library to simplify transactional publishing:
```kotlin
val transactionalConnection = myBeginTransaction()
kueue.send(KueueTopic("my_topic"), "Hello, world!", transactionalConnection) // an extension function must be imported explicitly
kueue.publish(KueueTopic("my_topic"), "Hello, world!", transactionalConnection) // an extension function must be imported explicitly
```

## Persistence
Expand All @@ -92,17 +92,17 @@ repositories {
}

dependencies {
implementation("io.github.vooft:pg-kueue-jooq-jdbc:<version>")
implementation("io.github.vooft:pg-kueue-pubsub-jdbc:<version>")
}
```

### Usage
```kotlin
val dslContext = createMyDslContext()

// there is a helper method to create a Kueue instance using, for example, a non-transactional DSLContext
val kueue = Kueue.jooq(dslContext)
// there is a helper method to create a KueuePubSub instance using, for example, a non-transactional DSLContext
val kueue = KueuePubSub.jooq(dslContext)

// also there is an extension method that accepts a transactional DSLContext to send notification within a transaction
kueue.send(KueueTopic("my_topic"), "Hello, world!", myTransactionalDslContext)
// also there is an extension method that accepts a transactional DSLContext to publish notification within a transaction
kueue.publish(KueueTopic("my_topic"), "Hello, world!", myTransactionalDslContext)
```
142 changes: 0 additions & 142 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
import com.vanniktech.maven.publish.SonatypeHost
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jmailen.gradle.kotlinter.tasks.FormatTask
import org.jmailen.gradle.kotlinter.tasks.LintTask

plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.ktlint)
alias(libs.plugins.detekt)
alias(libs.plugins.maven.central.publish)
alias(libs.plugins.dokka)
}

allprojects {
group = "io.github.vooft"
version = System.getenv("TAG") ?: "1.0-SNAPSHOT"
Expand All @@ -23,129 +7,3 @@ allprojects {
}
}

val publishAllTaskName = "publishAndReleaseToMavenCentralAll"
tasks.create(publishAllTaskName)

val publishAllToMavenLocalTaskName = "publishAllToMavenLocal"
tasks.create(publishAllToMavenLocalTaskName)

subprojects {

apply(plugin = "org.jetbrains.kotlin.jvm")
apply(plugin = "org.jmailen.kotlinter")
apply(plugin = "io.gitlab.arturbosch.detekt")
apply(plugin = "com.vanniktech.maven.publish")
apply(plugin = "org.jetbrains.dokka")

repositories {
mavenCentral()
}

dependencies {
addPlatform(this@subprojects, platform("org.jetbrains.kotlin:kotlin-bom:${rootProject.project.libs.versions.kotlin.get()}"))
}

detekt {
buildUponDefaultConfig = true
config.from(files("$rootDir/detekt.yaml"))
basePath = rootDir.absolutePath
}

tasks.withType<JavaCompile> {
sourceCompatibility = "21"
targetCompatibility = "21"
}

configurations {
compileOnly {
extendsFrom(configurations.annotationProcessor.get())
}
}

tasks.withType<Test> {
useJUnitPlatform()
testLogging {
exceptionFormat = TestExceptionFormat.FULL
events = mutableSetOf(TestLogEvent.FAILED, TestLogEvent.PASSED, TestLogEvent.SKIPPED)
showStandardStreams = true
}
}

tasks.withType<LintTask> {
source("build.gradle.kts", "settings.gradle.kts")
exclude {
it.file.path.startsWith("$buildDir") && !it.file.path.endsWith("gradle.kts")
}
dependsOn("formatKotlin")
}

tasks.withType<FormatTask> {
source("build.gradle.kts", "settings.gradle.kts")
exclude {
it.file.path.startsWith("$buildDir") && !it.file.path.endsWith("gradle.kts")
}
}

tasks.withType<KotlinCompile> {
compilerOptions {
freeCompilerArgs.addAll("-Xjsr305=strict", "-Xcontext-receivers")
allWarningsAsErrors = true
jvmTarget.set(JvmTarget.JVM_21)
}
// doLast {
// configurations.compileClasspath.get().forEach {
// println("TRACER checking: " + it.name)
// assert(it.exists())
// }
// }
}

mavenPublishing {
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)

signAllPublications()

pom {
name = "pg-kueue"
description = "Kotlin Coroutines PostgresSQL-based message queue using LISTEN/NOTIFYt"
url = "https://github.com/vooft/pg-kueue"
licenses {
license {
name = "The Apache License, Version 2.0"
url = "https://www.apache.org/licenses/LICENSE-2.0.txt"
}
}
scm {
connection = "https://github.com/vooft/pg-kueue"
url = "https://github.com/vooft/pg-kueue"
}
developers {
developer {
name = "pg-kueue team"
}
}
}
}

tasks.findByName("publishAndReleaseToMavenCentralAll")?.dependsOn(publishAllTaskName)
tasks.findByName("publishToMavenLocal")?.dependsOn(publishAllToMavenLocalTaskName)
}

//tasks.create("publish") {
// subprojects.forEach { project -> project.tasks.findByName("publish")?.let { dependsOn(it) } }
//}

repositories {
mavenCentral()
}

fun DependencyHandler.addPlatform(project: Project, platform: Dependency) {
val availableConfigurations = project.configurations.map { it.name }.toSet()
availableConfigurations.intersect(
setOf("api", "implementation", "testImplementation")
).forEach { configuration ->
add(configuration, platform)
}
}

fun <D : Dependency> DependencyHandler.addPlatform(project: Project, platform: Provider<D>) = addPlatform(project, platform.get())
21 changes: 21 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
plugins {
`kotlin-dsl`
}

repositories {
mavenCentral()
gradlePluginPortal()
}

dependencies {
// kotlin
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:${libs.versions.kotlin.get()}")

// detekt / ktlint
implementation("io.gitlab.arturbosch.detekt:detekt-gradle-plugin:${libs.plugins.detekt.get().version}")
implementation("org.jmailen.gradle:kotlinter-gradle:${libs.plugins.ktlint.get().version}")

// publishing
implementation("org.jetbrains.dokka:dokka-gradle-plugin:${libs.plugins.dokka.get().version}")
implementation("com.vanniktech:gradle-maven-publish-plugin:${libs.plugins.maven.central.publish.get().version}")
}
7 changes: 7 additions & 0 deletions buildSrc/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dependencyResolutionManagement {
versionCatalogs {
create("libs") {
from(files("../gradle/libs.versions.toml"))
}
}
}
4 changes: 4 additions & 0 deletions buildSrc/src/main/kotlin/Plugins.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import org.gradle.plugin.use.PluginDependenciesSpec

val PluginDependenciesSpec.`pg-kueue-base` get() = id("pg-kueue-base")
val PluginDependenciesSpec.`pg-kueue-publish` get() = id("pg-kueue-publish")
75 changes: 75 additions & 0 deletions buildSrc/src/main/kotlin/pg-kueue-base.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import io.gitlab.arturbosch.detekt.Detekt
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jmailen.gradle.kotlinter.tasks.FormatTask
import org.jmailen.gradle.kotlinter.tasks.LintTask

plugins {
kotlin("jvm")
id("io.gitlab.arturbosch.detekt")
id("org.jmailen.kotlinter")
}

dependencies {
addPlatform(project, platform("org.jetbrains.kotlin:kotlin-bom:${getKotlinPluginVersion()}"))
}

tasks.withType<Detekt> {
buildUponDefaultConfig = true
config.from(files("${rootDir.absolutePath}/detekt.yaml"))
basePath = rootDir.absolutePath

tasks.getByName("check").dependsOn(this)
}

tasks.withType<JavaCompile> {
sourceCompatibility = "21"
targetCompatibility = "21"
}

tasks.withType<Test> {
useJUnitPlatform()
testLogging {
exceptionFormat = TestExceptionFormat.FULL
events = mutableSetOf(TestLogEvent.FAILED, TestLogEvent.PASSED, TestLogEvent.SKIPPED)
showStandardStreams = true
showExceptions = true
}
}

tasks.withType<LintTask> {
source("build.gradle.kts", "settings.gradle.kts")
exclude {
it.file.path.startsWith("$buildDir") && !it.file.path.endsWith("gradle.kts")
}
dependsOn("formatKotlin")
}

tasks.withType<FormatTask> {
source("build.gradle.kts", "settings.gradle.kts")
exclude {
it.file.path.startsWith("$buildDir") && !it.file.path.endsWith("gradle.kts")
}
}

tasks.withType<KotlinCompile> {
compilerOptions {
freeCompilerArgs.addAll("-Xjsr305=strict", "-Xcontext-receivers")
allWarningsAsErrors = true
jvmTarget.set(JvmTarget.JVM_21)
}
}

private fun DependencyHandler.addPlatform(project: Project, platform: Dependency) {
val availableConfigurations = project.configurations.map { it.name }.toSet()
availableConfigurations.intersect(
setOf("api", "implementation", "testImplementation")
).forEach { configuration ->
add(configuration, platform)
}
}

private fun <D : Dependency> DependencyHandler.addPlatform(project: Project, platform: Provider<D>) = addPlatform(project, platform.get())
33 changes: 33 additions & 0 deletions buildSrc/src/main/kotlin/pg-kueue-publish.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import com.vanniktech.maven.publish.SonatypeHost

plugins {
id("org.jetbrains.dokka")
id("com.vanniktech.maven.publish")
}

mavenPublishing {
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)

signAllPublications()

pom {
name = "pg-kueue"
description = "Kotlin Coroutines PostgresSQL-based message queue using LISTEN/NOTIFYt"
url = "https://github.com/vooft/pg-kueue"
licenses {
license {
name = "The Apache License, Version 2.0"
url = "https://www.apache.org/licenses/LICENSE-2.0.txt"
}
}
scm {
connection = "https://github.com/vooft/pg-kueue"
url = "https://github.com/vooft/pg-kueue"
}
developers {
developer {
name = "pg-kueue team"
}
}
}
}
8 changes: 6 additions & 2 deletions detekt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ style:
value: 'STOPSHIP:'
MagicNumber:
active: false
ignoreCompanionObjectPropertyDeclaration: true
ignoreRanges: true
ReturnCount:
max: 3
UseOrEmpty:
active: false

coroutines:
SuspendFunWithFlowReturnType:
active: false
3 changes: 0 additions & 3 deletions pg-kueue-core/build.gradle.kts

This file was deleted.

Loading

0 comments on commit 48b3488

Please sign in to comment.