feat(api): cancel all tasks

This commit is contained in:
Gauthier Roebroeck 2021-09-16 14:01:29 +08:00
parent 2395f6e3ac
commit aff4418256
2 changed files with 45 additions and 0 deletions

View file

@ -0,0 +1,21 @@
package org.gotson.komga.infrastructure.jms
import mu.KotlinLogging
import org.apache.activemq.artemis.api.core.management.QueueControl
import org.apache.activemq.artemis.api.core.management.ResourceNames
import org.apache.activemq.artemis.core.server.embedded.EmbeddedActiveMQ
import org.springframework.stereotype.Service
private val logger = KotlinLogging.logger {}
@Service
class JmsQueueLifecycle(
embeddedActiveMQ: EmbeddedActiveMQ,
) {
private val queueControl = embeddedActiveMQ.activeMQServer.managementService.getResource("${ResourceNames.QUEUE}$QUEUE_TASKS") as QueueControl
fun emptyTaskQueue(): Int =
queueControl.removeAllMessages()
.also { logger.info { "Removed $it messages from $QUEUE_TASKS" } }
}

View file

@ -0,0 +1,24 @@
package org.gotson.komga.interfaces.rest
import org.gotson.komga.domain.model.ROLE_ADMIN
import org.gotson.komga.infrastructure.jms.JmsQueueLifecycle
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController
@RestController
@RequestMapping(produces = [MediaType.APPLICATION_JSON_VALUE])
class TaskController(
private val jmsQueueLifecycle: JmsQueueLifecycle,
) {
@DeleteMapping("api/v1/tasks")
@ResponseStatus(HttpStatus.OK)
@PreAuthorize("hasRole('$ROLE_ADMIN')")
fun emptyTaskQueue(): Int =
jmsQueueLifecycle.emptyTaskQueue()
}