57 lines
1.8 KiB
Java
57 lines
1.8 KiB
Java
package com.magamochi.task;
|
|
|
|
import com.magamochi.catalog.model.entity.Manga;
|
|
import com.magamochi.catalog.model.repository.MangaRepository;
|
|
import com.magamochi.model.dto.UpdateMangaFollowChapterListCommand;
|
|
import com.magamochi.queue.UpdateMangaFollowChapterListProducer;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.log4j.Log4j2;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
@Log4j2
|
|
@Component
|
|
@RequiredArgsConstructor
|
|
public class MangaFollowUpdateTask {
|
|
@Value("${manga-follow.update-enabled}")
|
|
private Boolean updateEnabled;
|
|
|
|
private final MangaRepository mangaRepository;
|
|
|
|
private final UpdateMangaFollowChapterListProducer producer;
|
|
|
|
@Scheduled(cron = "${manga-follow.cron-expression}")
|
|
@Transactional
|
|
public void updateMangaFollow() {
|
|
if (!updateEnabled) {
|
|
return;
|
|
}
|
|
|
|
updateMangaList();
|
|
}
|
|
|
|
public void updateMangaList() {
|
|
log.info("Queuing followed manga updates...");
|
|
|
|
var followedMangas = mangaRepository.findByFollowTrue();
|
|
followedMangas.forEach(this::updateFollowedManga);
|
|
}
|
|
|
|
private void updateFollowedManga(Manga manga) {
|
|
log.info("Fetching available mangas for followed Manga {}", manga.getTitle());
|
|
|
|
var mangaProviders = manga.getMangaContentProviders();
|
|
|
|
mangaProviders.stream()
|
|
.filter(mangaProvider -> mangaProvider.getContentProvider().isActive())
|
|
.forEach(
|
|
mangaProvider ->
|
|
producer.sendUpdateMangaFollowChapterListCommand(
|
|
new UpdateMangaFollowChapterListCommand(mangaProvider.getId())));
|
|
|
|
log.info("Followed Manga ({}) chapter list update queued.", manga.getTitle());
|
|
}
|
|
}
|