feat: 型安全ジョブキュー

This commit is contained in:
usbharu 2023-11-25 16:23:00 +09:00
parent 3009ca1532
commit ea9a999ae9
3 changed files with 32 additions and 6 deletions

View File

@ -2,26 +2,44 @@ package dev.usbharu.hideout.core.external.job
import kjob.core.Job
import kjob.core.Prop
import kjob.core.dsl.ScheduleContext
import kjob.core.job.JobProps
import org.springframework.stereotype.Component
sealed class HideoutJob(name: String = "") : Job(name)
abstract class HideoutJob<T, R : HideoutJob<T, R>>(name: String = "") : Job(name) {
abstract fun convert(value: T): ScheduleContext<R>.(R) -> Unit
abstract fun convert(props: JobProps<R>): T
}
@Component
object ReceiveFollowJob : HideoutJob("ReceiveFollowJob") {
object ReceiveFollowJob : HideoutJob<String, ReceiveFollowJob>("ReceiveFollowJob") {
val actor: Prop<ReceiveFollowJob, String> = string("actor")
val follow: Prop<ReceiveFollowJob, String> = string("follow")
val targetActor: Prop<ReceiveFollowJob, String> = string("targetActor")
override fun convert(value: String): ScheduleContext<ReceiveFollowJob>.(ReceiveFollowJob) -> Unit = {
props[it.follow] = value
}
override fun convert(props: JobProps<ReceiveFollowJob>): String = TODO("Not yet implemented")
}
@Component
object DeliverPostJob : HideoutJob("DeliverPostJob") {
object DeliverPostJob : HideoutJob<String, DeliverPostJob>("DeliverPostJob") {
val create = string("create")
val inbox = string("inbox")
val actor = string("actor")
override fun convert(value: String): ScheduleContext<DeliverPostJob>.(DeliverPostJob) -> Unit {
TODO("Not yet implemented")
}
override fun convert(props: JobProps<DeliverPostJob>): String {
TODO("Not yet implemented")
}
}
@Component
object DeliverReactionJob : HideoutJob("DeliverReactionJob") {
object DeliverReactionJob : HideoutJob<String, DeliverReactionJob>("DeliverReactionJob") {
val reaction: Prop<DeliverReactionJob, String> = string("reaction")
val postUrl: Prop<DeliverReactionJob, String> = string("postUrl")
val actor: Prop<DeliverReactionJob, String> = string("actor")
@ -30,7 +48,7 @@ object DeliverReactionJob : HideoutJob("DeliverReactionJob") {
}
@Component
object DeliverRemoveReactionJob : HideoutJob("DeliverRemoveReactionJob") {
object DeliverRemoveReactionJob : HideoutJob<String, DeliverRemoveReactionJob>("DeliverRemoveReactionJob") {
val id: Prop<DeliverRemoveReactionJob, String> = string("id")
val inbox: Prop<DeliverRemoveReactionJob, String> = string("inbox")
val actor: Prop<DeliverRemoveReactionJob, String> = string("actor")
@ -38,7 +56,7 @@ object DeliverRemoveReactionJob : HideoutJob("DeliverRemoveReactionJob") {
}
@Component
object InboxJob : HideoutJob("InboxJob") {
object InboxJob : HideoutJob<String, InboxJob>("InboxJob") {
val json = string("json")
val type = string("type")
val httpRequest = string("http_request")

View File

@ -1,5 +1,6 @@
package dev.usbharu.hideout.core.infrastructure.kjobexposed
import dev.usbharu.hideout.core.external.job.HideoutJob
import dev.usbharu.hideout.core.service.job.JobQueueParentService
import kjob.core.Job
import kjob.core.KJob
@ -29,4 +30,9 @@ class KJobJobQueueParentService() : JobQueueParentService {
logger.debug("schedule job={}", job.name)
kjob.schedule(job, block)
}
override suspend fun <T, J : HideoutJob<T, J>> scheduleTypeSafe(job: J, jobProps: T) {
val convert: ScheduleContext<J>.(J) -> Unit = job.convert(jobProps)
kjob.schedule(job, convert)
}
}

View File

@ -1,5 +1,6 @@
package dev.usbharu.hideout.core.service.job
import dev.usbharu.hideout.core.external.job.HideoutJob
import kjob.core.Job
import kjob.core.dsl.ScheduleContext
import org.springframework.stereotype.Service
@ -9,4 +10,5 @@ interface JobQueueParentService {
fun init(jobDefines: List<Job>)
suspend fun <J : Job> schedule(job: J, block: ScheduleContext<J>.(J) -> Unit = {})
suspend fun <T, J : HideoutJob<T, J>> scheduleTypeSafe(job: J, jobProps: T)
}