From 071303c421237301241ceca1be0d756f27f01aa3 Mon Sep 17 00:00:00 2001 From: Gauthier Roebroeck Date: Tue, 4 May 2021 09:37:10 +0800 Subject: [PATCH] refactor: handle priority for import tasks --- .../gotson/komga/application/tasks/Task.kt | 7 +++-- .../komga/application/tasks/TaskHandler.kt | 3 +- .../komga/application/tasks/TaskReceiver.kt | 4 +-- .../komga/domain/service/BookImporter.kt | 8 ++--- .../komga/interfaces/rest/BookController.kt | 6 ++-- .../interfaces/rest/LibraryController.kt | 6 ++-- .../komga/interfaces/rest/SeriesController.kt | 6 ++-- .../application/tasks/TaskHandlerTest.kt | 2 +- .../komga/domain/service/BookImporterTest.kt | 29 ------------------- 9 files changed, 23 insertions(+), 48 deletions(-) diff --git a/komga/src/main/kotlin/org/gotson/komga/application/tasks/Task.kt b/komga/src/main/kotlin/org/gotson/komga/application/tasks/Task.kt index 5b8c869f1..0d2fabfb3 100644 --- a/komga/src/main/kotlin/org/gotson/komga/application/tasks/Task.kt +++ b/komga/src/main/kotlin/org/gotson/komga/application/tasks/Task.kt @@ -4,7 +4,8 @@ import org.gotson.komga.domain.model.BookMetadataPatchCapability import org.gotson.komga.domain.model.CopyMode import java.io.Serializable -const val HIGHEST_PRIORITY = 9 +const val HIGHEST_PRIORITY = 8 +const val HIGH_PRIORITY = 6 const val DEFAULT_PRIORITY = 4 sealed class Task(priority: Int = DEFAULT_PRIORITY) : Serializable { @@ -38,7 +39,9 @@ sealed class Task(priority: Int = DEFAULT_PRIORITY) : Serializable { override fun uniqueId() = "AGGREGATE_SERIES_METADATA_$seriesId" } - data class ImportBook(val sourceFile: String, val seriesId: String, val copyMode: CopyMode, val destinationName: String?, val upgradeBookId: String?) : Task() { + class ImportBook(val sourceFile: String, val seriesId: String, val copyMode: CopyMode, val destinationName: String?, val upgradeBookId: String?, priority: Int = DEFAULT_PRIORITY) : Task(priority) { override fun uniqueId(): String = "IMPORT_BOOK_${seriesId}_$sourceFile" + override fun toString(): String = + "ImportBook(sourceFile='$sourceFile', seriesId='$seriesId', copyMode=$copyMode, destinationName=$destinationName, upgradeBookId=$upgradeBookId, priority='$priority')" } } diff --git a/komga/src/main/kotlin/org/gotson/komga/application/tasks/TaskHandler.kt b/komga/src/main/kotlin/org/gotson/komga/application/tasks/TaskHandler.kt index 43bc42427..27cc3e978 100644 --- a/komga/src/main/kotlin/org/gotson/komga/application/tasks/TaskHandler.kt +++ b/komga/src/main/kotlin/org/gotson/komga/application/tasks/TaskHandler.kt @@ -74,7 +74,8 @@ class TaskHandler( is Task.ImportBook -> seriesRepository.findByIdOrNull(task.seriesId)?.let { series -> - bookImporter.importBook(Paths.get(task.sourceFile), series, task.copyMode, task.destinationName, task.upgradeBookId) + val importedBook = bookImporter.importBook(Paths.get(task.sourceFile), series, task.copyMode, task.destinationName, task.upgradeBookId) + taskReceiver.analyzeBook(importedBook, priority = task.priority + 1) } ?: logger.warn { "Cannot execute task $task: Series does not exist" } } }.also { diff --git a/komga/src/main/kotlin/org/gotson/komga/application/tasks/TaskReceiver.kt b/komga/src/main/kotlin/org/gotson/komga/application/tasks/TaskReceiver.kt index 882813633..817e3447e 100644 --- a/komga/src/main/kotlin/org/gotson/komga/application/tasks/TaskReceiver.kt +++ b/komga/src/main/kotlin/org/gotson/komga/application/tasks/TaskReceiver.kt @@ -82,8 +82,8 @@ class TaskReceiver( submitTask(Task.AggregateSeriesMetadata(seriesId)) } - fun importBook(sourceFile: String, seriesId: String, copyMode: CopyMode, destinationName: String?, upgradeBookId: String?) { - submitTask(Task.ImportBook(sourceFile, seriesId, copyMode, destinationName, upgradeBookId)) + fun importBook(sourceFile: String, seriesId: String, copyMode: CopyMode, destinationName: String?, upgradeBookId: String?, priority: Int = DEFAULT_PRIORITY) { + submitTask(Task.ImportBook(sourceFile, seriesId, copyMode, destinationName, upgradeBookId, priority)) } private fun submitTask(task: Task) { diff --git a/komga/src/main/kotlin/org/gotson/komga/domain/service/BookImporter.kt b/komga/src/main/kotlin/org/gotson/komga/domain/service/BookImporter.kt index b66d0e522..e32c26822 100644 --- a/komga/src/main/kotlin/org/gotson/komga/domain/service/BookImporter.kt +++ b/komga/src/main/kotlin/org/gotson/komga/domain/service/BookImporter.kt @@ -1,8 +1,7 @@ package org.gotson.komga.domain.service import mu.KotlinLogging -import org.gotson.komga.application.tasks.HIGHEST_PRIORITY -import org.gotson.komga.application.tasks.TaskReceiver +import org.gotson.komga.domain.model.Book import org.gotson.komga.domain.model.CopyMode import org.gotson.komga.domain.model.Media import org.gotson.komga.domain.model.PathContainedInPath @@ -41,11 +40,10 @@ class BookImporter( private val metadataRepository: BookMetadataRepository, private val readProgressRepository: ReadProgressRepository, private val readListRepository: ReadListRepository, - private val taskReceiver: TaskReceiver, private val libraryRepository: LibraryRepository, ) { - fun importBook(sourceFile: Path, series: Series, copyMode: CopyMode, destinationName: String? = null, upgradeBookId: String? = null) { + fun importBook(sourceFile: Path, series: Series, copyMode: CopyMode, destinationName: String? = null, upgradeBookId: String? = null): Book { if (sourceFile.notExists()) throw FileNotFoundException("File not found: $sourceFile") libraryRepository.findAll().forEach { library -> @@ -148,6 +146,6 @@ class BookImporter( seriesLifecycle.sortBooks(series) - taskReceiver.analyzeBook(importedBook, HIGHEST_PRIORITY) + return importedBook } } diff --git a/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/BookController.kt b/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/BookController.kt index 970ca741e..485817387 100644 --- a/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/BookController.kt +++ b/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/BookController.kt @@ -8,6 +8,7 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse import mu.KotlinLogging import org.apache.commons.io.IOUtils import org.gotson.komga.application.tasks.HIGHEST_PRIORITY +import org.gotson.komga.application.tasks.HIGH_PRIORITY import org.gotson.komga.application.tasks.TaskReceiver import org.gotson.komga.domain.model.Author import org.gotson.komga.domain.model.BookSearchWithReadProgress @@ -419,7 +420,7 @@ class BookController( @ResponseStatus(HttpStatus.ACCEPTED) fun analyze(@PathVariable bookId: String) { bookRepository.findByIdOrNull(bookId)?.let { book -> - taskReceiver.analyzeBook(book, HIGHEST_PRIORITY) + taskReceiver.analyzeBook(book, HIGH_PRIORITY) } ?: throw ResponseStatusException(HttpStatus.NOT_FOUND) } @@ -428,7 +429,7 @@ class BookController( @ResponseStatus(HttpStatus.ACCEPTED) fun refreshMetadata(@PathVariable bookId: String) { bookRepository.findByIdOrNull(bookId)?.let { book -> - taskReceiver.refreshBookMetadata(book, priority = HIGHEST_PRIORITY) + taskReceiver.refreshBookMetadata(book, priority = HIGH_PRIORITY) } ?: throw ResponseStatusException(HttpStatus.NOT_FOUND) } @@ -519,6 +520,7 @@ class BookController( copyMode = bookImportBatch.copyMode, destinationName = it.destinationName, upgradeBookId = it.upgradeBookId, + priority = HIGHEST_PRIORITY, ) } catch (e: Exception) { logger.error(e) { "Error while creating import task for: $it" } diff --git a/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/LibraryController.kt b/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/LibraryController.kt index 7ef4013e5..48858cf20 100644 --- a/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/LibraryController.kt +++ b/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/LibraryController.kt @@ -1,7 +1,7 @@ package org.gotson.komga.interfaces.rest import mu.KotlinLogging -import org.gotson.komga.application.tasks.HIGHEST_PRIORITY +import org.gotson.komga.application.tasks.HIGH_PRIORITY import org.gotson.komga.application.tasks.TaskReceiver import org.gotson.komga.domain.model.DirectoryNotFoundException import org.gotson.komga.domain.model.DuplicateNameException @@ -147,7 +147,7 @@ class LibraryController( @ResponseStatus(HttpStatus.ACCEPTED) fun analyze(@PathVariable libraryId: String) { bookRepository.findAllIdByLibraryId(libraryId).forEach { - taskReceiver.analyzeBook(it, HIGHEST_PRIORITY) + taskReceiver.analyzeBook(it, HIGH_PRIORITY) } } @@ -156,7 +156,7 @@ class LibraryController( @ResponseStatus(HttpStatus.ACCEPTED) fun refreshMetadata(@PathVariable libraryId: String) { bookRepository.findAllIdByLibraryId(libraryId).forEach { - taskReceiver.refreshBookMetadata(it, priority = HIGHEST_PRIORITY) + taskReceiver.refreshBookMetadata(it, priority = HIGH_PRIORITY) } } } diff --git a/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/SeriesController.kt b/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/SeriesController.kt index 8d174d9b0..e542049bd 100644 --- a/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/SeriesController.kt +++ b/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/SeriesController.kt @@ -9,7 +9,7 @@ import mu.KotlinLogging import org.apache.commons.compress.archivers.zip.ZipArchiveEntry import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream import org.apache.commons.io.IOUtils -import org.gotson.komga.application.tasks.HIGHEST_PRIORITY +import org.gotson.komga.application.tasks.HIGH_PRIORITY import org.gotson.komga.application.tasks.TaskReceiver import org.gotson.komga.domain.model.Author import org.gotson.komga.domain.model.BookSearchWithReadProgress @@ -294,7 +294,7 @@ class SeriesController( @ResponseStatus(HttpStatus.ACCEPTED) fun analyze(@PathVariable seriesId: String) { bookRepository.findAllIdBySeriesId(seriesId).forEach { - taskReceiver.analyzeBook(it, HIGHEST_PRIORITY) + taskReceiver.analyzeBook(it, HIGH_PRIORITY) } } @@ -303,7 +303,7 @@ class SeriesController( @ResponseStatus(HttpStatus.ACCEPTED) fun refreshMetadata(@PathVariable seriesId: String) { bookRepository.findAllIdBySeriesId(seriesId).forEach { - taskReceiver.refreshBookMetadata(it, priority = HIGHEST_PRIORITY) + taskReceiver.refreshBookMetadata(it, priority = HIGH_PRIORITY) } } diff --git a/komga/src/test/kotlin/org/gotson/komga/application/tasks/TaskHandlerTest.kt b/komga/src/test/kotlin/org/gotson/komga/application/tasks/TaskHandlerTest.kt index 1e4e39ca3..03c9ea91d 100644 --- a/komga/src/test/kotlin/org/gotson/komga/application/tasks/TaskHandlerTest.kt +++ b/komga/src/test/kotlin/org/gotson/komga/application/tasks/TaskHandlerTest.kt @@ -76,7 +76,7 @@ class TaskHandlerTest( jmsListenerEndpointRegistry.stop() taskReceiver.analyzeBook("1") - taskReceiver.analyzeBook("2", HIGHEST_PRIORITY) + taskReceiver.analyzeBook("2", HIGH_PRIORITY) jmsListenerEndpointRegistry.start() diff --git a/komga/src/test/kotlin/org/gotson/komga/domain/service/BookImporterTest.kt b/komga/src/test/kotlin/org/gotson/komga/domain/service/BookImporterTest.kt index 1a7b5f73e..0d88e144b 100644 --- a/komga/src/test/kotlin/org/gotson/komga/domain/service/BookImporterTest.kt +++ b/komga/src/test/kotlin/org/gotson/komga/domain/service/BookImporterTest.kt @@ -2,15 +2,8 @@ package org.gotson.komga.domain.service import com.google.common.jimfs.Configuration import com.google.common.jimfs.Jimfs -import com.ninjasquad.springmockk.MockkBean -import io.mockk.Runs -import io.mockk.every -import io.mockk.just -import io.mockk.verify import org.assertj.core.api.Assertions import org.assertj.core.api.Assertions.assertThat -import org.gotson.komga.application.tasks.TaskReceiver -import org.gotson.komga.domain.model.Book import org.gotson.komga.domain.model.BookPage import org.gotson.komga.domain.model.CopyMode import org.gotson.komga.domain.model.KomgaUser @@ -32,7 +25,6 @@ import org.gotson.komga.infrastructure.language.toIndexedMap import org.junit.jupiter.api.AfterAll import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.BeforeAll -import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import org.junit.jupiter.api.extension.ExtendWith import org.springframework.beans.factory.annotation.Autowired @@ -63,9 +55,6 @@ class BookImporterTest( @Autowired private val readListLifecycle: ReadListLifecycle, ) { - @MockkBean - private lateinit var mockTaskReceiver: TaskReceiver - private val library = makeLibrary("lib", "file:/library") private val user1 = KomgaUser("user1@example.org", "", false) private val user2 = KomgaUser("user2@example.org", "", false) @@ -78,12 +67,6 @@ class BookImporterTest( userRepository.insert(user2) } - @BeforeEach - fun beforeEach() { - every { mockTaskReceiver.analyzeBook(any(), any()) } just Runs - every { mockTaskReceiver.refreshBookMetadata(any(), any(), any()) } just Runs - } - @AfterAll fun teardown() { libraryRepository.deleteAll() @@ -214,8 +197,6 @@ class BookImporterTest( val newMedia = mediaRepository.findById(id) assertThat(newMedia.status).isEqualTo(Media.Status.UNKNOWN) } - - verify(exactly = 1) { mockTaskReceiver.analyzeBook(any(), any()) } } } @@ -256,8 +237,6 @@ class BookImporterTest( assertThat(upgradedMedia.status).isEqualTo(Media.Status.OUTDATED) assertThat(Files.notExists(sourceFile)).isTrue - - verify(exactly = 1) { mockTaskReceiver.analyzeBook(any(), any()) } } } @@ -318,8 +297,6 @@ class BookImporterTest( } assertThat(Files.notExists(sourceFile)).isTrue - - verify(exactly = 1) { mockTaskReceiver.analyzeBook(any(), any()) } } } @@ -360,8 +337,6 @@ class BookImporterTest( assertThat(upgradedMedia.status).isEqualTo(Media.Status.OUTDATED) assertThat(Files.exists(sourceFile)).isTrue - - verify(exactly = 1) { mockTaskReceiver.analyzeBook(any(), any()) } } } @@ -410,8 +385,6 @@ class BookImporterTest( assertThat(completed).isFalse assertThat(page).isEqualTo(4) } - - verify(exactly = 1) { mockTaskReceiver.analyzeBook(any(), any()) } } } @@ -449,8 +422,6 @@ class BookImporterTest( assertThat(bookIds).hasSize(1) assertThat(bookIds[0]).isEqualTo(books[0].id) } - - verify(exactly = 1) { mockTaskReceiver.analyzeBook(any(), any()) } } } }