refactor: jobを別のクラスに切り出し2

This commit is contained in:
usbharu 2023-10-27 13:54:33 +09:00
parent ab25e136dd
commit 61ee7ab59f
4 changed files with 54 additions and 31 deletions

View File

@ -1,7 +1,7 @@
package dev.usbharu.hideout package dev.usbharu.hideout
import dev.usbharu.hideout.domain.model.job.HideoutJob import dev.usbharu.hideout.domain.model.job.HideoutJob
import dev.usbharu.hideout.service.ap.APService import dev.usbharu.hideout.service.ap.job.ApJobService
import dev.usbharu.hideout.service.job.JobQueueParentService import dev.usbharu.hideout.service.job.JobQueueParentService
import dev.usbharu.hideout.service.job.JobQueueWorkerService import dev.usbharu.hideout.service.job.JobQueueWorkerService
import org.slf4j.Logger import org.slf4j.Logger
@ -27,7 +27,7 @@ class JobQueueRunner(private val jobQueueParentService: JobQueueParentService, p
class JobQueueWorkerRunner( class JobQueueWorkerRunner(
private val jobQueueWorkerService: JobQueueWorkerService, private val jobQueueWorkerService: JobQueueWorkerService,
private val jobs: List<HideoutJob>, private val jobs: List<HideoutJob>,
private val apService: APService private val apJobService: ApJobService
) : ApplicationRunner { ) : ApplicationRunner {
override fun run(args: ApplicationArguments?) { override fun run(args: ApplicationArguments?) {
LOGGER.info("Init job queue worker.") LOGGER.info("Init job queue worker.")
@ -36,7 +36,7 @@ class JobQueueWorkerRunner(
it to { it to {
execute { execute {
LOGGER.debug("excute job ${it.name}") LOGGER.debug("excute job ${it.name}")
apService.processActivity( apJobService.processActivity(
job = this, job = this,
hideoutJob = it hideoutJob = it
) )

View File

@ -5,13 +5,10 @@ import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.readValue
import dev.usbharu.hideout.domain.model.ActivityPubResponse import dev.usbharu.hideout.domain.model.ActivityPubResponse
import dev.usbharu.hideout.domain.model.ap.Follow import dev.usbharu.hideout.domain.model.ap.Follow
import dev.usbharu.hideout.domain.model.job.*
import dev.usbharu.hideout.exception.JsonParseException import dev.usbharu.hideout.exception.JsonParseException
import dev.usbharu.hideout.service.ap.job.APReceiveFollowJobService import dev.usbharu.hideout.service.ap.job.APReceiveFollowJobService
import dev.usbharu.hideout.service.ap.job.ApNoteJobService import dev.usbharu.hideout.service.ap.job.ApNoteJobService
import dev.usbharu.hideout.service.ap.job.ApReactionJobService import dev.usbharu.hideout.service.ap.job.ApReactionJobService
import kjob.core.dsl.JobContextWithProps
import kjob.core.job.JobProps
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Qualifier import org.springframework.beans.factory.annotation.Qualifier
@ -21,8 +18,6 @@ interface APService {
fun parseActivity(json: String): ActivityType fun parseActivity(json: String): ActivityType
suspend fun processActivity(json: String, type: ActivityType): ActivityPubResponse? suspend fun processActivity(json: String, type: ActivityType): ActivityPubResponse?
suspend fun <T : HideoutJob> processActivity(job: JobContextWithProps<T>, hideoutJob: HideoutJob)
} }
enum class ActivityType { enum class ActivityType {
@ -233,28 +228,5 @@ class APServiceImpl(
} }
} }
@Suppress("REDUNDANT_ELSE_IN_WHEN")
override suspend fun <T : HideoutJob> processActivity(job: JobContextWithProps<T>, hideoutJob: HideoutJob) {
logger.debug("processActivity: ${hideoutJob.name}")
@Suppress("ElseCaseInsteadOfExhaustiveWhen")
// Springで作成されるプロキシの都合上パターンマッチングが壊れるので必須
when (hideoutJob) {
is ReceiveFollowJob -> {
apReceiveFollowJobService.receiveFollowJob(
job.props as JobProps<ReceiveFollowJob>
)
}
is DeliverPostJob -> apNoteJobService.createNoteJob(job.props as JobProps<DeliverPostJob>)
is DeliverReactionJob -> apReactionJobService.reactionJob(job.props as JobProps<DeliverReactionJob>)
is DeliverRemoveReactionJob -> apReactionJobService.removeReactionJob(
job.props as JobProps<DeliverRemoveReactionJob>
)
else -> {
throw IllegalStateException("WTF")
}
}
}
} }

View File

@ -0,0 +1,8 @@
package dev.usbharu.hideout.service.ap.job
import dev.usbharu.hideout.domain.model.job.HideoutJob
import kjob.core.dsl.JobContextWithProps
interface ApJobService {
suspend fun <T : HideoutJob> processActivity(job: JobContextWithProps<T>, hideoutJob: HideoutJob)
}

View File

@ -0,0 +1,43 @@
package dev.usbharu.hideout.service.ap.job
import dev.usbharu.hideout.domain.model.job.*
import kjob.core.dsl.JobContextWithProps
import kjob.core.job.JobProps
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
@Service
class ApJobServiceImpl(
private val apReceiveFollowJobService: APReceiveFollowJobService,
private val apNoteJobService: ApNoteJobService,
private val apReactionJobService: ApReactionJobService
) : ApJobService {
@Suppress("REDUNDANT_ELSE_IN_WHEN")
override suspend fun <T : HideoutJob> processActivity(job: JobContextWithProps<T>, hideoutJob: HideoutJob) {
logger.debug("processActivity: ${hideoutJob.name}")
@Suppress("ElseCaseInsteadOfExhaustiveWhen")
// Springで作成されるプロキシの都合上パターンマッチングが壊れるので必須
when (hideoutJob) {
is ReceiveFollowJob -> {
apReceiveFollowJobService.receiveFollowJob(
job.props as JobProps<ReceiveFollowJob>
)
}
is DeliverPostJob -> apNoteJobService.createNoteJob(job.props as JobProps<DeliverPostJob>)
is DeliverReactionJob -> apReactionJobService.reactionJob(job.props as JobProps<DeliverReactionJob>)
is DeliverRemoveReactionJob -> apReactionJobService.removeReactionJob(
job.props as JobProps<DeliverRemoveReactionJob>
)
else -> {
throw IllegalStateException("WTF")
}
}
}
companion object {
private val logger = LoggerFactory.getLogger(ApJobServiceImpl::class.java)
}
}