diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/ApJobService.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/ApJobService.kt index fe909b0d..3b50b777 100644 --- a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/ApJobService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/ApJobService.kt @@ -4,5 +4,5 @@ import dev.usbharu.hideout.core.external.job.HideoutJob import kjob.core.dsl.JobContextWithProps interface ApJobService { - suspend fun processActivity(job: JobContextWithProps, hideoutJob: HideoutJob) + suspend fun > processActivity(job: JobContextWithProps, hideoutJob: HideoutJob) } diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/ApJobServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/ApJobServiceImpl.kt index 5da0356c..150e93a3 100644 --- a/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/ApJobServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/service/common/ApJobServiceImpl.kt @@ -21,7 +21,10 @@ class ApJobServiceImpl( private val inboxJobProcessor: InboxJobProcessor ) : ApJobService { @Suppress("REDUNDANT_ELSE_IN_WHEN") - override suspend fun processActivity(job: JobContextWithProps, hideoutJob: HideoutJob) { + override suspend fun > processActivity( + job: JobContextWithProps, + hideoutJob: HideoutJob + ) { logger.debug("processActivity: ${hideoutJob.name}") @Suppress("ElseCaseInsteadOfExhaustiveWhen") diff --git a/src/main/kotlin/dev/usbharu/hideout/application/config/JobQueueRunner.kt b/src/main/kotlin/dev/usbharu/hideout/application/config/JobQueueRunner.kt index 4f7f1aaf..bb22e6d6 100644 --- a/src/main/kotlin/dev/usbharu/hideout/application/config/JobQueueRunner.kt +++ b/src/main/kotlin/dev/usbharu/hideout/application/config/JobQueueRunner.kt @@ -11,7 +11,10 @@ import org.springframework.boot.ApplicationRunner import org.springframework.stereotype.Component @Component -class JobQueueRunner(private val jobQueueParentService: JobQueueParentService, private val jobs: List) : +class JobQueueRunner( + private val jobQueueParentService: JobQueueParentService, + private val jobs: List> +) : ApplicationRunner { override fun run(args: ApplicationArguments?) { LOGGER.info("Init job queue. ${jobs.size}") @@ -26,7 +29,7 @@ class JobQueueRunner(private val jobQueueParentService: JobQueueParentService, p @Component class JobQueueWorkerRunner( private val jobQueueWorkerService: JobQueueWorkerService, - private val jobs: List, + private val jobs: List>, private val apJobService: ApJobService ) : ApplicationRunner { override fun run(args: ApplicationArguments?) { diff --git a/src/main/kotlin/dev/usbharu/hideout/core/external/job/HideoutJob.kt b/src/main/kotlin/dev/usbharu/hideout/core/external/job/HideoutJob.kt index 30c02371..72878f6e 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/external/job/HideoutJob.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/external/job/HideoutJob.kt @@ -6,9 +6,10 @@ import kjob.core.dsl.ScheduleContext import kjob.core.job.JobProps import org.springframework.stereotype.Component -abstract class HideoutJob>(name: String = "") : Job(name) { - abstract fun convert(value: T): ScheduleContext.(R) -> Unit - abstract fun convert(props: JobProps): T +abstract class HideoutJob>(name: String = "") : Job(name) { + abstract fun convert(value: @UnsafeVariance T): ScheduleContext<@UnsafeVariance R>.(@UnsafeVariance R) -> Unit + fun convertUnsafe(props: JobProps<*>): T = convert(props as JobProps) + abstract fun convert(props: JobProps<@UnsafeVariance R>): T } data class ReceiveFollowJobParam( diff --git a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/kjobexposed/KJobJobQueueWorkerService.kt b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/kjobexposed/KJobJobQueueWorkerService.kt index 8404a2db..872a3249 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/kjobexposed/KJobJobQueueWorkerService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/kjobexposed/KJobJobQueueWorkerService.kt @@ -1,6 +1,7 @@ package dev.usbharu.hideout.core.infrastructure.kjobexposed import dev.usbharu.hideout.core.external.job.HideoutJob +import dev.usbharu.hideout.core.service.job.JobProcessor import dev.usbharu.hideout.core.service.job.JobQueueWorkerService import kjob.core.dsl.JobContextWithProps import kjob.core.dsl.JobRegisterContext @@ -12,7 +13,7 @@ import org.springframework.stereotype.Service @Service @ConditionalOnProperty(name = ["hideout.use-mongodb"], havingValue = "false", matchIfMissing = true) -class KJobJobQueueWorkerService() : JobQueueWorkerService { +class KJobJobQueueWorkerService(private val jobQueueProcessorList: List>) : JobQueueWorkerService { val kjob by lazy { kjob(ExposedKJob) { @@ -27,5 +28,14 @@ class KJobJobQueueWorkerService() : JobQueueWorkerService { defines.forEach { job -> kjob.register(job.first, job.second) } + + for (jobProcessor in jobQueueProcessorList) { + kjob.register(jobProcessor.job()) { + execute { + val param = it.convertUnsafe(props) + jobProcessor.process(param) + } + } + } } } diff --git a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/kjobmongodb/KJobMongoJobQueueWorkerService.kt b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/kjobmongodb/KJobMongoJobQueueWorkerService.kt index 5d4ed22c..25fbdefe 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/kjobmongodb/KJobMongoJobQueueWorkerService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/kjobmongodb/KJobMongoJobQueueWorkerService.kt @@ -1,15 +1,15 @@ package dev.usbharu.hideout.core.infrastructure.kjobmongodb import com.mongodb.reactivestreams.client.MongoClient +import dev.usbharu.hideout.core.external.job.HideoutJob import dev.usbharu.hideout.core.service.job.JobQueueWorkerService +import kjob.core.dsl.JobContextWithProps 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.core.external.job.HideoutJob as HJ -import kjob.core.dsl.JobContextWithProps as JCWP @Service @ConditionalOnProperty(name = ["hideout.use-mongodb"], havingValue = "true", matchIfMissing = false) @@ -23,9 +23,7 @@ class KJobMongoJobQueueWorkerService(private val mongoClient: MongoClient) : Job }.start() } - override fun init( - defines: List>.(HJ) -> KJobFunctions>>> - ) { + override fun > init(defines: List>.(R) -> KJobFunctions>>>) { defines.forEach { job -> kjob.register(job.first, job.second) } diff --git a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/kjobmongodb/KjobMongoJobQueueParentService.kt b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/kjobmongodb/KjobMongoJobQueueParentService.kt index 0875325d..846e8dff 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/kjobmongodb/KjobMongoJobQueueParentService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/kjobmongodb/KjobMongoJobQueueParentService.kt @@ -1,6 +1,7 @@ package dev.usbharu.hideout.core.infrastructure.kjobmongodb import com.mongodb.reactivestreams.client.MongoClient +import dev.usbharu.hideout.core.external.job.HideoutJob import dev.usbharu.hideout.core.service.job.JobQueueParentService import kjob.core.Job import kjob.core.dsl.ScheduleContext @@ -23,10 +24,15 @@ class KjobMongoJobQueueParentService(private val mongoClient: MongoClient) : Job override fun init(jobDefines: List) = Unit + @Deprecated("use type safe → scheduleTypeSafe") override suspend fun schedule(job: J, block: ScheduleContext.(J) -> Unit) { kjob.schedule(job, block) } + override suspend fun > scheduleTypeSafe(job: J, jobProps: T) { + TODO("Not yet implemented") + } + override fun close() { kjob.shutdown() } diff --git a/src/main/kotlin/dev/usbharu/hideout/core/service/job/JobProcessor.kt b/src/main/kotlin/dev/usbharu/hideout/core/service/job/JobProcessor.kt new file mode 100644 index 00000000..f6adc74b --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/core/service/job/JobProcessor.kt @@ -0,0 +1,8 @@ +package dev.usbharu.hideout.core.service.job + +import dev.usbharu.hideout.core.external.job.HideoutJob + +interface JobProcessor> { + suspend fun process(param: @UnsafeVariance T) + fun job(): R +} diff --git a/src/main/kotlin/dev/usbharu/hideout/core/service/job/JobProcessorService.kt b/src/main/kotlin/dev/usbharu/hideout/core/service/job/JobProcessorService.kt deleted file mode 100644 index 07798007..00000000 --- a/src/main/kotlin/dev/usbharu/hideout/core/service/job/JobProcessorService.kt +++ /dev/null @@ -1,8 +0,0 @@ -package dev.usbharu.hideout.core.service.job - -import dev.usbharu.hideout.core.external.job.HideoutJob - -interface JobProcessorService> { - suspend fun process(param: T) - suspend fun job(): Class -}