diff --git a/komga/src/main/kotlin/org/gotson/komga/infrastructure/jms/JmsQueueLifecycle.kt b/komga/src/main/kotlin/org/gotson/komga/infrastructure/jms/JmsQueueLifecycle.kt new file mode 100644 index 000000000..2e5410558 --- /dev/null +++ b/komga/src/main/kotlin/org/gotson/komga/infrastructure/jms/JmsQueueLifecycle.kt @@ -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" } } +} diff --git a/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/TaskController.kt b/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/TaskController.kt new file mode 100644 index 000000000..9722ec53f --- /dev/null +++ b/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/TaskController.kt @@ -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() +}