refactor: handle priority for import tasks

This commit is contained in:
Gauthier Roebroeck 2021-05-04 09:37:10 +08:00
parent bfc5fc6f48
commit 071303c421
9 changed files with 23 additions and 48 deletions

View file

@ -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')"
}
}

View file

@ -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 {

View file

@ -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) {

View file

@ -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
}
}

View file

@ -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" }

View file

@ -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)
}
}
}

View file

@ -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)
}
}

View file

@ -76,7 +76,7 @@ class TaskHandlerTest(
jmsListenerEndpointRegistry.stop()
taskReceiver.analyzeBook("1")
taskReceiver.analyzeBook("2", HIGHEST_PRIORITY)
taskReceiver.analyzeBook("2", HIGH_PRIORITY)
jmsListenerEndpointRegistry.start()

View file

@ -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<Book>(), any()) } just Runs
every { mockTaskReceiver.refreshBookMetadata(any<String>(), 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<Book>(), 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<Book>(), any()) }
}
}
@ -318,8 +297,6 @@ class BookImporterTest(
}
assertThat(Files.notExists(sourceFile)).isTrue
verify(exactly = 1) { mockTaskReceiver.analyzeBook(any<Book>(), 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<Book>(), any()) }
}
}
@ -410,8 +385,6 @@ class BookImporterTest(
assertThat(completed).isFalse
assertThat(page).isEqualTo(4)
}
verify(exactly = 1) { mockTaskReceiver.analyzeBook(any<Book>(), any()) }
}
}
@ -449,8 +422,6 @@ class BookImporterTest(
assertThat(bookIds).hasSize(1)
assertThat(bookIds[0]).isEqualTo(books[0].id)
}
verify(exactly = 1) { mockTaskReceiver.analyzeBook(any<Book>(), any()) }
}
}
}