mirror of https://github.com/usbharu/Hideout.git
feat: KJobがMongoDBでも動くように
This commit is contained in:
parent
0bb74b3a5f
commit
3a85917f97
|
@ -128,6 +128,7 @@ dependencies {
|
|||
|
||||
|
||||
implementation("org.drewcarlson:kjob-core:0.6.0")
|
||||
implementation("org.drewcarlson:kjob-mongo:0.6.0")
|
||||
testImplementation("org.slf4j:slf4j-simple:2.0.7")
|
||||
|
||||
detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.22.0")
|
||||
|
|
|
@ -7,9 +7,11 @@ import kjob.core.dsl.ScheduleContext
|
|||
import kjob.core.kjob
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
|
||||
import org.springframework.stereotype.Service
|
||||
|
||||
@Service
|
||||
@ConditionalOnProperty(name = ["hideout.job-queue.type"], havingValue = "rdb")
|
||||
class KJobJobQueueParentService(private val database: Database) : JobQueueParentService {
|
||||
|
||||
private val logger = LoggerFactory.getLogger(this::class.java)
|
||||
|
|
|
@ -5,11 +5,13 @@ import kjob.core.dsl.JobRegisterContext
|
|||
import kjob.core.dsl.KJobFunctions
|
||||
import kjob.core.kjob
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
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 = "rdb", matchIfMissing = true)
|
||||
class KJobJobQueueWorkerService(private val database: Database) : JobQueueWorkerService {
|
||||
|
||||
val kjob by lazy {
|
||||
|
@ -17,7 +19,7 @@ class KJobJobQueueWorkerService(private val database: Database) : JobQueueWorker
|
|||
connectionDatabase = database
|
||||
nonBlockingMaxJobs = 10
|
||||
blockingMaxJobs = 10
|
||||
jobExecutionPeriodInSeconds = 10
|
||||
jobExecutionPeriodInSeconds = 1
|
||||
}.start()
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -5,6 +5,8 @@ hideout:
|
|||
driver: "org.h2.Driver"
|
||||
user: ""
|
||||
password: ""
|
||||
job-queue:
|
||||
type: "nosql"
|
||||
spring:
|
||||
jackson:
|
||||
serialization:
|
||||
|
|
Loading…
Reference in New Issue