feat: KJobがMongoDBでも動くように

This commit is contained in:
usbharu 2023-09-27 14:40:14 +09:00
parent 0bb74b3a5f
commit 3a85917f97
6 changed files with 66 additions and 1 deletions

View File

@ -128,6 +128,7 @@ dependencies {
implementation("org.drewcarlson:kjob-core:0.6.0") implementation("org.drewcarlson:kjob-core:0.6.0")
implementation("org.drewcarlson:kjob-mongo:0.6.0")
testImplementation("org.slf4j:slf4j-simple:2.0.7") testImplementation("org.slf4j:slf4j-simple:2.0.7")
detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.22.0") detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.22.0")

View File

@ -7,9 +7,11 @@ import kjob.core.dsl.ScheduleContext
import kjob.core.kjob import kjob.core.kjob
import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.Database
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
@Service @Service
@ConditionalOnProperty(name = ["hideout.job-queue.type"], havingValue = "rdb")
class KJobJobQueueParentService(private val database: Database) : JobQueueParentService { class KJobJobQueueParentService(private val database: Database) : JobQueueParentService {
private val logger = LoggerFactory.getLogger(this::class.java) private val logger = LoggerFactory.getLogger(this::class.java)

View File

@ -5,11 +5,13 @@ import kjob.core.dsl.JobRegisterContext
import kjob.core.dsl.KJobFunctions import kjob.core.dsl.KJobFunctions
import kjob.core.kjob import kjob.core.kjob
import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.Database
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import dev.usbharu.hideout.domain.model.job.HideoutJob as HJ import dev.usbharu.hideout.domain.model.job.HideoutJob as HJ
import kjob.core.dsl.JobContextWithProps as JCWP import kjob.core.dsl.JobContextWithProps as JCWP
@Service @Service
@ConditionalOnProperty(name = ["hideout.job-queue.type"], havingValue = "rdb", matchIfMissing = true)
class KJobJobQueueWorkerService(private val database: Database) : JobQueueWorkerService { class KJobJobQueueWorkerService(private val database: Database) : JobQueueWorkerService {
val kjob by lazy { val kjob by lazy {
@ -17,7 +19,7 @@ class KJobJobQueueWorkerService(private val database: Database) : JobQueueWorker
connectionDatabase = database connectionDatabase = database
nonBlockingMaxJobs = 10 nonBlockingMaxJobs = 10
blockingMaxJobs = 10 blockingMaxJobs = 10
jobExecutionPeriodInSeconds = 10 jobExecutionPeriodInSeconds = 1
}.start() }.start()
} }

View File

@ -0,0 +1,31 @@
package dev.usbharu.hideout.service.job
import kjob.core.dsl.JobRegisterContext
import kjob.core.dsl.KJobFunctions
import kjob.core.kjob
import kjob.mongo.Mongo
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.stereotype.Service
import dev.usbharu.hideout.domain.model.job.HideoutJob as HJ
import kjob.core.dsl.JobContextWithProps as JCWP
@Service
@ConditionalOnProperty(name = ["hideout.job-queue.type"], havingValue = "nosql")
class KJobMongoJobQueueWorkerService : JobQueueWorkerService {
val kjob by lazy {
kjob(Mongo) {
connectionString = "mongodb://localhost"
nonBlockingMaxJobs = 10
blockingMaxJobs = 10
jobExecutionPeriodInSeconds = 1
}.start()
}
override fun init(
defines: List<Pair<HJ, JobRegisterContext<HJ, JCWP<HJ>>.(HJ) -> KJobFunctions<HJ, JCWP<HJ>>>>
) {
defines.forEach { job ->
kjob.register(job.first, job.second)
}
}
}

View File

@ -0,0 +1,27 @@
package dev.usbharu.hideout.service.job
import kjob.core.Job
import kjob.core.dsl.ScheduleContext
import kjob.core.kjob
import kjob.mongo.Mongo
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.stereotype.Service
@Service
@ConditionalOnProperty(name = ["hideout.job-queue.type"], havingValue = "nosql")
class KjobMongoJobQueueParentService : JobQueueParentService {
override fun init(jobDefines: List<Job>) = Unit
private val kjob = kjob(Mongo) {
connectionString = "mongodb://localhost"
databaseName = "kjob"
jobCollection = "kjob-jobs"
lockCollection = "kjob-locks"
expireLockInMinutes = 5L
isWorker = false
}.start()
override suspend fun <J : Job> schedule(job: J, block: ScheduleContext<J>.(J) -> Unit) {
kjob.schedule(job, block)
}
}

View File

@ -5,6 +5,8 @@ hideout:
driver: "org.h2.Driver" driver: "org.h2.Driver"
user: "" user: ""
password: "" password: ""
job-queue:
type: "nosql"
spring: spring:
jackson: jackson:
serialization: serialization: