fix(regenerate-covers): wrap book query stream in transaction to prevent lazy init exception (#916)

This commit is contained in:
brios
2026-05-05 12:18:08 +02:00
committed by GitHub
parent 2fcbb5c708
commit 2dcbfd14e7
4 changed files with 86 additions and 20 deletions
@@ -143,6 +143,10 @@ public interface BookRepository extends JpaRepository<BookEntity, Long>, JpaSpec
@Query("SELECT b FROM BookEntity b WHERE (b.deleted IS NULL OR b.deleted = false)")
List<BookEntity> findAllFullBooks();
@EntityGraph(attributePaths = {"metadata", "metadata.comicMetadata", "library", "bookFiles"})
@Query("SELECT b FROM BookEntity b WHERE (b.deleted IS NULL OR b.deleted = false)")
List<BookEntity> findAllFullBooksWithFiles();
@Query("SELECT b FROM BookEntity b WHERE (b.deleted IS NULL OR b.deleted = false) AND b.id > :afterId ORDER BY b.id")
List<BookEntity> findBooksForMigrationBatch(@Param("afterId") long afterId, Pageable pageable);
@@ -81,6 +81,10 @@ public class BookQueryService {
return bookRepository.findAllFullBooks();
}
public List<BookEntity> getAllFullBookEntitiesWithFiles() {
return bookRepository.findAllFullBooksWithFiles();
}
@Transactional
public void saveAll(List<BookEntity> books) {
bookRepository.saveAll(books);
@@ -78,9 +78,6 @@ public class BookCoverService {
private record BookCoverInfo(Long id, String title) {
}
private record BookRegenerationInfo(Long id, String title, BookFileType bookType, boolean coverLocked) {
}
// =========================
// SECTION: COVER UPDATES
// =========================
@@ -304,7 +301,7 @@ public class BookCoverService {
* Regenerate covers for a set of books.
*/
public void regenerateCoversForBooks(Set<Long> bookIds) {
List<BookRegenerationInfo> unlockedBooks = getUnlockedBookRegenerationInfos(bookIds);
List<BookCoverInfo> unlockedBooks = getUnlockedBookRegenerationInfos(bookIds);
String username = getCurrentUsername();
taskExecutor.execute(() -> processBulkCoverRegeneration(unlockedBooks, username));
}
@@ -325,11 +322,12 @@ public class BookCoverService {
String username = getCurrentUsername();
taskExecutor.execute(() -> {
try {
List<BookRegenerationInfo> books = bookQueryService.getAllFullBookEntities().stream()
List<BookCoverInfo> books = bookQueryService.getAllFullBookEntitiesWithFiles().stream()
.filter(book -> book.getMetadata() != null)
.filter(book -> !isCoverLocked(book))
.filter(book -> book.getPrimaryBookFile() != null)
.filter(book -> !missingOnly || book.getBookCoverHash() == null)
.map(book -> new BookRegenerationInfo(book.getId(), book.getMetadata().getTitle(), book.getPrimaryBookFile().getBookType(), false))
.map(book -> new BookCoverInfo(book.getId(), book.getMetadata().getTitle()))
.toList();
int total = books.size();
String label = missingOnly ? "missing" : "all";
@@ -337,7 +335,7 @@ public class BookCoverService {
int current = 1;
for (BookRegenerationInfo bookInfo : books) {
for (BookCoverInfo bookInfo : books) {
try {
String progress = "(" + current + "/" + total + ") ";
sendNotification(username, Topic.LOG, LogNotification.info(progress + "Regenerating cover for: " + bookInfo.title()));
@@ -418,21 +416,26 @@ public class BookCoverService {
}
}
private void processBulkCoverRegeneration(List<BookRegenerationInfo> books, String username) {
private void processBulkCoverRegeneration(List<BookCoverInfo> books, String username) {
try {
int total = books.size();
sendNotification(username, Topic.LOG, LogNotification.info("Started regenerating covers for " + total + " selected book(s)"));
int current = 1;
for (BookRegenerationInfo bookInfo : books) {
for (BookCoverInfo bookInfo : books) {
try {
String progress = "(" + current + "/" + total + ") ";
sendNotification(username, Topic.LOG, LogNotification.info(progress + "Regenerating cover for: " + bookInfo.title()));
transactionTemplate.execute(status -> {
bookRepository.findByIdWithBookFiles(bookInfo.id()).ifPresent(book -> {
BookFileProcessor processor = processorRegistry.getProcessorOrThrow(bookInfo.bookType());
var primaryFile = book.getPrimaryBookFile();
if (primaryFile == null) {
log.warn("{}Skipping book ID {} ({}) - no primary file", progress, book.getId(), bookInfo.title());
return;
}
BookFileProcessor processor = processorRegistry.getProcessorOrThrow(primaryFile.getBookType());
boolean success = processor.generateCover(book);
if (success) {
@@ -547,25 +550,27 @@ public class BookCoverService {
private List<BookCoverInfo> getUnlockedBookCoverInfos(Set<Long> bookIds) {
return bookQueryService.findAllWithMetadataByIds(bookIds).stream()
.filter(book -> book.getMetadata() != null)
.filter(book -> !isCoverLocked(book))
.map(book -> new BookCoverInfo(book.getId(), book.getMetadata().getTitle()))
.toList();
}
private List<BookRegenerationInfo> getUnlockedBookRegenerationInfos(Set<Long> bookIds) {
private List<BookCoverInfo> getUnlockedBookRegenerationInfos(Set<Long> bookIds) {
return bookQueryService.findAllWithMetadataByIds(bookIds).stream()
.filter(book -> book.getMetadata() != null)
.filter(book -> !isCoverLocked(book))
.filter(book -> book.getPrimaryBookFile() != null)
.map(book -> new BookRegenerationInfo(book.getId(), book.getMetadata().getTitle(), book.getPrimaryBookFile().getBookType(), false))
.map(book -> new BookCoverInfo(book.getId(), book.getMetadata().getTitle()))
.toList();
}
private boolean isCoverLocked(BookEntity book) {
return book.getMetadata().getCoverLocked() != null && book.getMetadata().getCoverLocked();
return book.getMetadata() != null && Boolean.TRUE.equals(book.getMetadata().getCoverLocked());
}
private boolean isAudiobookCoverLocked(BookEntity book) {
return book.getMetadata().getAudiobookCoverLocked() != null && book.getMetadata().getAudiobookCoverLocked();
return book.getMetadata() != null && Boolean.TRUE.equals(book.getMetadata().getAudiobookCoverLocked());
}
private String getAuthorNames(BookEntity bookEntity) {