feat: 型安全ジョブキューをすべてのジョブキューに適用

This commit is contained in:
usbharu 2023-11-25 16:46:03 +09:00
parent ea9a999ae9
commit dcac609b94
2 changed files with 107 additions and 13 deletions

View File

@ -11,54 +11,146 @@ abstract class HideoutJob<T, R : HideoutJob<T, R>>(name: String = "") : Job(name
abstract fun convert(props: JobProps<R>): T abstract fun convert(props: JobProps<R>): T
} }
data class ReceiveFollowJobParam(
val actor: String,
val follow: String,
val targetActor: String
)
@Component @Component
object ReceiveFollowJob : HideoutJob<String, ReceiveFollowJob>("ReceiveFollowJob") { object ReceiveFollowJob : HideoutJob<ReceiveFollowJobParam, ReceiveFollowJob>("ReceiveFollowJob") {
val actor: Prop<ReceiveFollowJob, String> = string("actor") val actor: Prop<ReceiveFollowJob, String> = string("actor")
val follow: Prop<ReceiveFollowJob, String> = string("follow") val follow: Prop<ReceiveFollowJob, String> = string("follow")
val targetActor: Prop<ReceiveFollowJob, String> = string("targetActor") val targetActor: Prop<ReceiveFollowJob, String> = string("targetActor")
override fun convert(value: String): ScheduleContext<ReceiveFollowJob>.(ReceiveFollowJob) -> Unit = { override fun convert(value: ReceiveFollowJobParam): ScheduleContext<ReceiveFollowJob>.(ReceiveFollowJob) -> Unit = {
props[it.follow] = value props[follow] = value.follow
props[actor] = value.actor
props[targetActor] = value.targetActor
} }
override fun convert(props: JobProps<ReceiveFollowJob>): String = TODO("Not yet implemented") override fun convert(props: JobProps<ReceiveFollowJob>): ReceiveFollowJobParam = ReceiveFollowJobParam(
actor = props[actor],
follow = props[follow],
targetActor = props[targetActor]
)
} }
data class DeliverPostJobParam(
val create: String,
val inbox: String,
val actor: String
)
@Component @Component
object DeliverPostJob : HideoutJob<String, DeliverPostJob>("DeliverPostJob") { object DeliverPostJob : HideoutJob<DeliverPostJobParam, DeliverPostJob>("DeliverPostJob") {
val create = string("create") val create = string("create")
val inbox = string("inbox") val inbox = string("inbox")
val actor = string("actor") val actor = string("actor")
override fun convert(value: String): ScheduleContext<DeliverPostJob>.(DeliverPostJob) -> Unit { override fun convert(value: DeliverPostJobParam): ScheduleContext<DeliverPostJob>.(DeliverPostJob) -> Unit = {
TODO("Not yet implemented") props[create] = value.create
props[inbox] = value.inbox
props[actor] = value.actor
} }
override fun convert(props: JobProps<DeliverPostJob>): String { override fun convert(props: JobProps<DeliverPostJob>): DeliverPostJobParam = DeliverPostJobParam(
TODO("Not yet implemented") create = props[create],
} inbox = props[inbox],
actor = props[actor]
)
} }
data class DeliverReactionJobParam(
val reaction: String,
val postUrl: String,
val actor: String,
val inbox: String,
val id: String
)
@Component @Component
object DeliverReactionJob : HideoutJob<String, DeliverReactionJob>("DeliverReactionJob") { object DeliverReactionJob : HideoutJob<DeliverReactionJobParam, DeliverReactionJob>("DeliverReactionJob") {
val reaction: Prop<DeliverReactionJob, String> = string("reaction") val reaction: Prop<DeliverReactionJob, String> = string("reaction")
val postUrl: Prop<DeliverReactionJob, String> = string("postUrl") val postUrl: Prop<DeliverReactionJob, String> = string("postUrl")
val actor: Prop<DeliverReactionJob, String> = string("actor") val actor: Prop<DeliverReactionJob, String> = string("actor")
val inbox: Prop<DeliverReactionJob, String> = string("inbox") val inbox: Prop<DeliverReactionJob, String> = string("inbox")
val id: Prop<DeliverReactionJob, String> = string("id") val id: Prop<DeliverReactionJob, String> = string("id")
override fun convert(value: DeliverReactionJobParam): ScheduleContext<DeliverReactionJob>.(DeliverReactionJob) -> Unit =
{
props[reaction] = value.reaction
props[postUrl] = value.postUrl
props[actor] = value.actor
props[inbox] = value.inbox
props[id] = value.id
}
override fun convert(props: JobProps<DeliverReactionJob>): DeliverReactionJobParam = DeliverReactionJobParam(
props[reaction],
props[postUrl],
props[actor],
props[inbox],
props[id]
)
} }
data class DeliverRemoveReactionJobParam(
val id: String,
val inbox: String,
val actor: String,
val like: String
)
@Component @Component
object DeliverRemoveReactionJob : HideoutJob<String, DeliverRemoveReactionJob>("DeliverRemoveReactionJob") { object DeliverRemoveReactionJob :
HideoutJob<DeliverRemoveReactionJobParam, DeliverRemoveReactionJob>("DeliverRemoveReactionJob") {
val id: Prop<DeliverRemoveReactionJob, String> = string("id") val id: Prop<DeliverRemoveReactionJob, String> = string("id")
val inbox: Prop<DeliverRemoveReactionJob, String> = string("inbox") val inbox: Prop<DeliverRemoveReactionJob, String> = string("inbox")
val actor: Prop<DeliverRemoveReactionJob, String> = string("actor") val actor: Prop<DeliverRemoveReactionJob, String> = string("actor")
val like: Prop<DeliverRemoveReactionJob, String> = string("like") val like: Prop<DeliverRemoveReactionJob, String> = string("like")
override fun convert(value: DeliverRemoveReactionJobParam): ScheduleContext<DeliverRemoveReactionJob>.(DeliverRemoveReactionJob) -> Unit =
{
props[id] = value.id
props[inbox] = value.inbox
props[actor] = value.actor
props[like] = value.like
}
override fun convert(props: JobProps<DeliverRemoveReactionJob>): DeliverRemoveReactionJobParam =
DeliverRemoveReactionJobParam(
id = props[id],
inbox = props[inbox],
actor = props[actor],
like = props[like]
)
} }
data class InboxJobParam(
val json: String,
val type: String,
val httpRequest: String,
val headers: String
)
@Component @Component
object InboxJob : HideoutJob<String, InboxJob>("InboxJob") { object InboxJob : HideoutJob<InboxJobParam, InboxJob>("InboxJob") {
val json = string("json") val json = string("json")
val type = string("type") val type = string("type")
val httpRequest = string("http_request") val httpRequest = string("http_request")
val headers = string("headers") val headers = string("headers")
override fun convert(value: InboxJobParam): ScheduleContext<InboxJob>.(InboxJob) -> Unit = {
props[json] = value.json
props[type] = value.type
props[httpRequest] = value.httpRequest
props[headers] = value.headers
}
override fun convert(props: JobProps<InboxJob>): InboxJobParam = InboxJobParam(
props[json],
props[type],
props[httpRequest],
props[headers]
)
} }

View File

@ -9,6 +9,8 @@ import org.springframework.stereotype.Service
interface JobQueueParentService { interface JobQueueParentService {
fun init(jobDefines: List<Job>) fun init(jobDefines: List<Job>)
@Deprecated("use type safe → scheduleTypeSafe")
suspend fun <J : Job> schedule(job: J, block: ScheduleContext<J>.(J) -> Unit = {}) suspend fun <J : Job> schedule(job: J, block: ScheduleContext<J>.(J) -> Unit = {})
suspend fun <T, J : HideoutJob<T, J>> scheduleTypeSafe(job: J, jobProps: T) suspend fun <T, J : HideoutJob<T, J>> scheduleTypeSafe(job: J, jobProps: T)
} }