insert error entry one by one

This commit is contained in:
Cuong-Tran 2024-10-24 03:37:46 +07:00
parent 2979caa676
commit 28fa0dec6a
No known key found for this signature in database
GPG Key ID: 733AA7624B9315C2
10 changed files with 54 additions and 57 deletions

View File

@ -24,7 +24,6 @@ import eu.kanade.tachiyomi.data.download.DownloadManager
import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.source.model.SManga
import eu.kanade.tachiyomi.source.model.UpdateStrategy
import eu.kanade.tachiyomi.util.system.createFileInCacheDir
import eu.kanade.tachiyomi.util.system.isConnectedToWifi
import eu.kanade.tachiyomi.util.system.isRunning
import eu.kanade.tachiyomi.util.system.setForegroundSafely
@ -54,10 +53,8 @@ import tachiyomi.domain.library.service.LibraryPreferences.Companion.MANGA_HAS_U
import tachiyomi.domain.library.service.LibraryPreferences.Companion.MANGA_NON_COMPLETED
import tachiyomi.domain.library.service.LibraryPreferences.Companion.MANGA_NON_READ
import tachiyomi.domain.library.service.LibraryPreferences.Companion.MANGA_OUTSIDE_RELEASE_PERIOD
import tachiyomi.domain.libraryUpdateError.interactor.DeleteLibraryUpdateErrors
import tachiyomi.domain.libraryUpdateError.interactor.InsertLibraryUpdateErrors
import tachiyomi.domain.libraryUpdateError.model.LibraryUpdateError
import tachiyomi.domain.libraryUpdateErrorMessage.interactor.DeleteLibraryUpdateErrorMessages
import tachiyomi.domain.libraryUpdateErrorMessage.interactor.InsertLibraryUpdateErrorMessages
import tachiyomi.domain.libraryUpdateErrorMessage.model.LibraryUpdateErrorMessage
import tachiyomi.domain.manga.interactor.FetchInterval
@ -69,7 +66,6 @@ import tachiyomi.domain.source.service.SourceManager
import tachiyomi.i18n.MR
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.io.File
import java.time.Instant
import java.time.ZonedDateTime
import java.util.concurrent.CopyOnWriteArrayList
@ -91,8 +87,6 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
private val fetchInterval: FetchInterval = Injekt.get()
private val filterChaptersForDownload: FilterChaptersForDownload = Injekt.get()
private val deleteLibraryUpdateErrorMessages: DeleteLibraryUpdateErrorMessages = Injekt.get()
private val deleteLibraryUpdateErrors: DeleteLibraryUpdateErrors = Injekt.get()
private val insertLibraryUpdateErrors: InsertLibraryUpdateErrors = Injekt.get()
private val insertLibraryUpdateErrorMessages: InsertLibraryUpdateErrorMessages = Injekt.get()
@ -300,6 +294,7 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
)
else -> e.message
}
writeErrorToDB(manga to errorMessage)
failedUpdates.add(manga to errorMessage)
}
}
@ -320,7 +315,6 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
}
if (failedUpdates.isNotEmpty()) {
writeErrorsToDB(failedUpdates)
notifier.showUpdateErrorNotification(
failedUpdates.size,
)
@ -385,52 +379,16 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
)
}
/**
* Writes basic file of update errors to cache dir.
*/
private fun writeErrorFile(errors: List<Pair<Manga, String?>>): File {
try {
if (errors.isNotEmpty()) {
val file = context.createFileInCacheDir("mihon_update_errors.txt")
file.bufferedWriter().use { out ->
out.write(context.stringResource(MR.strings.library_errors_help, ERROR_LOG_HELP_URL) + "\n\n")
// Error file format:
// ! Error
// # Source
// - Manga
errors.groupBy({ it.second }, { it.first }).forEach { (error, mangas) ->
out.write("\n! ${error}\n")
mangas.groupBy { it.source }.forEach { (srcId, mangas) ->
val source = sourceManager.getOrStub(srcId)
out.write(" # $source\n")
mangas.forEach {
out.write(" - ${it.title}\n")
}
}
}
}
return file
}
} catch (_: Exception) {}
return File("")
}
private suspend fun writeErrorsToDB(errors: List<Pair<Manga, String?>>) {
deleteLibraryUpdateErrorMessages.await()
deleteLibraryUpdateErrors.await()
val libraryErrors = errors.groupBy({ it.second }, { it.first })
val errorMessages = insertLibraryUpdateErrorMessages.insertAll(
libraryUpdateErrorMessages = libraryErrors.keys.map { errorMessage ->
LibraryUpdateErrorMessage(-1L, errorMessage.orEmpty())
},
private suspend fun writeErrorToDB(error: Pair<Manga, String?>) {
val errorMessage = error.second ?: "???"
val errorMessageId = insertLibraryUpdateErrorMessages.get(errorMessage)
?: insertLibraryUpdateErrorMessages.insert(
libraryUpdateErrorMessage = LibraryUpdateErrorMessage(-1L, errorMessage),
)
insertLibraryUpdateErrors.upsert(
LibraryUpdateError(id = -1L, mangaId = error.first.id, messageId = errorMessageId),
)
val errorList = mutableListOf<LibraryUpdateError>()
errorMessages.forEach {
libraryErrors[it.second]?.forEach { manga ->
errorList.add(LibraryUpdateError(id = -1L, mangaId = manga.id, messageId = it.first))
}
}
insertLibraryUpdateErrors.insertAll(errorList)
}
companion object {

View File

@ -37,6 +37,15 @@ class LibraryUpdateErrorRepositoryImpl(
}
}
override suspend fun upsert(libraryUpdateError: LibraryUpdateError) {
return handler.await(inTransaction = true) {
libraryUpdateErrorQueries.upsert(
mangaId = libraryUpdateError.mangaId,
messageId = libraryUpdateError.messageId,
)
}
}
override suspend fun insert(libraryUpdateError: LibraryUpdateError) {
return handler.await(inTransaction = true) {
libraryUpdateErrorQueries.insert(

View File

@ -29,8 +29,14 @@ class LibraryUpdateErrorMessageRepositoryImpl(
return handler.await { libraryUpdateErrorMessageQueries.deleteAllErrorMessages() }
}
override suspend fun insert(libraryUpdateErrorMessage: LibraryUpdateErrorMessage): Long? {
return handler.awaitOneOrNullExecutable(inTransaction = true) {
override suspend fun get(message: String): Long? {
return handler.awaitOneOrNullExecutable {
libraryUpdateErrorMessageQueries.getErrorMessages(message) { id, _ -> id }
}
}
override suspend fun insert(libraryUpdateErrorMessage: LibraryUpdateErrorMessage): Long {
return handler.awaitOneExecutable(inTransaction = true) {
libraryUpdateErrorMessageQueries.insert(libraryUpdateErrorMessage.message)
libraryUpdateErrorMessageQueries.selectLastInsertedRowId()
}

View File

@ -1,6 +1,6 @@
CREATE TABLE libraryUpdateError (
_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
manga_id INTEGER NOT NULL,
manga_id INTEGER NOT NULL UNIQUE,
message_id INTEGER NOT NULL
);
@ -11,6 +11,15 @@ FROM libraryUpdateError;
insert:
INSERT INTO libraryUpdateError(manga_id, message_id) VALUES (:mangaId, :messageId);
upsert:
INSERT INTO libraryUpdateError(manga_id, message_id)
VALUES (:mangaId, :messageId)
ON CONFLICT(manga_id)
DO UPDATE
SET
message_id = :messageId
WHERE manga_id = :mangaId;
deleteAllErrors:
DELETE FROM libraryUpdateError;

View File

@ -7,6 +7,10 @@ getAllErrorMessages:
SELECT *
FROM libraryUpdateErrorMessage;
getErrorMessages:
SELECT *
FROM libraryUpdateErrorMessage WHERE message == :message;
insert:
INSERT INTO libraryUpdateErrorMessage(message) VALUES (:message);

View File

@ -2,7 +2,7 @@ DROP VIEW IF EXISTS libraryUpdateErrorView;
CREATE TABLE IF NOT EXISTS libraryUpdateError (
_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
manga_id INTEGER NOT NULL,
manga_id INTEGER NOT NULL UNIQUE,
message_id INTEGER NOT NULL
);

View File

@ -6,6 +6,10 @@ import tachiyomi.domain.libraryUpdateError.repository.LibraryUpdateErrorReposito
class InsertLibraryUpdateErrors(
private val libraryUpdateErrorRepository: LibraryUpdateErrorRepository,
) {
suspend fun upsert(libraryUpdateError: LibraryUpdateError) {
return libraryUpdateErrorRepository.upsert(libraryUpdateError)
}
suspend fun insert(libraryUpdateError: LibraryUpdateError) {
return libraryUpdateErrorRepository.insert(libraryUpdateError)
}

View File

@ -13,6 +13,8 @@ interface LibraryUpdateErrorRepository {
suspend fun delete(errorId: Long)
suspend fun upsert(libraryUpdateError: LibraryUpdateError)
suspend fun insert(libraryUpdateError: LibraryUpdateError)
suspend fun insertAll(libraryUpdateErrors: List<LibraryUpdateError>)

View File

@ -6,8 +6,11 @@ import tachiyomi.domain.libraryUpdateErrorMessage.repository.LibraryUpdateErrorM
class InsertLibraryUpdateErrorMessages(
private val libraryUpdateErrorMessageRepository: LibraryUpdateErrorMessageRepository,
) {
suspend fun get(message: String): Long? {
return libraryUpdateErrorMessageRepository.get(message)
}
suspend fun insert(libraryUpdateErrorMessage: LibraryUpdateErrorMessage): Long? {
suspend fun insert(libraryUpdateErrorMessage: LibraryUpdateErrorMessage): Long {
return libraryUpdateErrorMessageRepository.insert(libraryUpdateErrorMessage)
}

View File

@ -11,7 +11,9 @@ interface LibraryUpdateErrorMessageRepository {
suspend fun deleteAll()
suspend fun insert(libraryUpdateErrorMessage: LibraryUpdateErrorMessage): Long?
suspend fun get(message: String): Long?
suspend fun insert(libraryUpdateErrorMessage: LibraryUpdateErrorMessage): Long
suspend fun insertAll(libraryUpdateErrorMessages: List<LibraryUpdateErrorMessage>): List<Pair<Long, String>>
}