mirror of
https://github.com/wushuo894/ani-rss.git
synced 2026-03-15 01:20:23 +00:00
refactor: 优化 Task
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package ani.rss.service;
|
||||
|
||||
import ani.rss.task.BaseTask;
|
||||
import ani.rss.task.BgmTask;
|
||||
import ani.rss.task.RenameTask;
|
||||
import ani.rss.task.RssTask;
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -45,9 +47,14 @@ public class TaskService {
|
||||
return;
|
||||
}
|
||||
LOOP.set(true);
|
||||
THREADS.add(new RenameTask(LOOP));
|
||||
THREADS.add(new RssTask(LOOP));
|
||||
THREADS.add(new BgmTask(LOOP));
|
||||
|
||||
List<Class<? extends BaseTask>> classList = List.of(RenameTask.class, RssTask.class, BgmTask.class);
|
||||
|
||||
for (Class<? extends BaseTask> aClass : classList) {
|
||||
BaseTask task = SpringUtil.getBean(aClass);
|
||||
String name = aClass.getSimpleName();
|
||||
THREADS.add(new Thread(() -> task.run(name, LOOP)));
|
||||
}
|
||||
for (Thread thread : THREADS) {
|
||||
thread.start();
|
||||
}
|
||||
|
||||
29
ani-rss-application/src/main/java/ani/rss/task/BaseTask.java
Normal file
29
ani-rss-application/src/main/java/ani/rss/task/BaseTask.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package ani.rss.task;
|
||||
|
||||
import cn.hutool.log.Log;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface BaseTask extends BiConsumer<String, AtomicBoolean> {
|
||||
|
||||
Log log = Log.get();
|
||||
|
||||
default void run(String threadName, AtomicBoolean loop) {
|
||||
Thread.currentThread().setName(threadName);
|
||||
|
||||
log.info("{} 任务正在运行", threadName);
|
||||
while (loop.get()) {
|
||||
accept(threadName, loop);
|
||||
}
|
||||
log.info("{} 任务已停止", threadName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param threadName 线程名
|
||||
* @param loop 原子化布尔 用以控制循环
|
||||
*/
|
||||
@Override
|
||||
void accept(String threadName, AtomicBoolean loop);
|
||||
}
|
||||
@@ -8,8 +8,9 @@ import ani.rss.util.other.AniUtil;
|
||||
import ani.rss.util.other.BgmUtil;
|
||||
import ani.rss.util.other.ConfigUtil;
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
@@ -20,62 +21,55 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
* 用于更新BGM评分
|
||||
*/
|
||||
@Slf4j
|
||||
public class BgmTask extends Thread {
|
||||
@Component
|
||||
public class BgmTask implements BaseTask {
|
||||
|
||||
private final AtomicBoolean loop;
|
||||
|
||||
public BgmTask(AtomicBoolean loop) {
|
||||
this.loop = loop;
|
||||
}
|
||||
@Resource
|
||||
private AniService aniService;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
super.setName("bgm-task-thread");
|
||||
log.info("{} 任务正在运行", getName());
|
||||
while (loop.get()) {
|
||||
public void accept(String threadName, AtomicBoolean loop) {
|
||||
try {
|
||||
BgmUtil.refreshToken();
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
|
||||
List<Ani> aniList = AniUtil.ANI_LIST;
|
||||
for (Ani ani : aniList) {
|
||||
if (!loop.get()) {
|
||||
return;
|
||||
}
|
||||
Boolean enable = ani.getEnable();
|
||||
if (!enable) {
|
||||
continue;
|
||||
}
|
||||
BgmInfo bgmInfo;
|
||||
try {
|
||||
BgmUtil.refreshToken();
|
||||
bgmInfo = BgmUtil.getBgmInfo(ani);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
continue;
|
||||
}
|
||||
|
||||
List<Ani> aniList = AniUtil.ANI_LIST;
|
||||
for (Ani ani : aniList) {
|
||||
if (!loop.get()) {
|
||||
return;
|
||||
}
|
||||
Boolean enable = ani.getEnable();
|
||||
if (!enable) {
|
||||
continue;
|
||||
}
|
||||
BgmInfo bgmInfo;
|
||||
try {
|
||||
bgmInfo = BgmUtil.getBgmInfo(ani);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
continue;
|
||||
}
|
||||
double score = Optional.ofNullable(bgmInfo.getRating())
|
||||
.map(BgmInfo.Rating::getScore)
|
||||
.orElse(0.0);
|
||||
ani.setScore(score);
|
||||
|
||||
double score = Optional.ofNullable(bgmInfo.getRating())
|
||||
.map(BgmInfo.Rating::getScore)
|
||||
.orElse(0.0);
|
||||
ani.setScore(score);
|
||||
Config config = ConfigUtil.CONFIG;
|
||||
Boolean updateTotalEpisodeNumber = config.getUpdateTotalEpisodeNumber();
|
||||
Boolean forceUpdateTotalEpisodeNumber = config.getForceUpdateTotalEpisodeNumber();
|
||||
|
||||
Config config = ConfigUtil.CONFIG;
|
||||
Boolean updateTotalEpisodeNumber = config.getUpdateTotalEpisodeNumber();
|
||||
Boolean forceUpdateTotalEpisodeNumber = config.getForceUpdateTotalEpisodeNumber();
|
||||
|
||||
if (!updateTotalEpisodeNumber) {
|
||||
// 未开启更新总集数
|
||||
continue;
|
||||
}
|
||||
|
||||
SpringUtil.getBean(AniService.class).updateTotalEpisodeNumber(ani, bgmInfo, forceUpdateTotalEpisodeNumber);
|
||||
if (!updateTotalEpisodeNumber) {
|
||||
// 未开启更新总集数
|
||||
continue;
|
||||
}
|
||||
AniUtil.sync();
|
||||
|
||||
ThreadUtil.sleep(12, TimeUnit.HOURS);
|
||||
aniService.updateTotalEpisodeNumber(ani, bgmInfo, forceUpdateTotalEpisodeNumber);
|
||||
}
|
||||
log.info("{} 任务已停止", getName());
|
||||
AniUtil.sync();
|
||||
|
||||
ThreadUtil.sleep(12, TimeUnit.HOURS);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,8 +7,9 @@ import ani.rss.service.DownloadService;
|
||||
import ani.rss.util.other.ConfigUtil;
|
||||
import ani.rss.util.other.TorrentUtil;
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
@@ -17,50 +18,42 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
* 重命名
|
||||
*/
|
||||
@Slf4j
|
||||
public class RenameTask extends Thread {
|
||||
|
||||
private final AtomicBoolean loop;
|
||||
|
||||
public RenameTask(AtomicBoolean loop) {
|
||||
this.loop = loop;
|
||||
}
|
||||
@Component
|
||||
public class RenameTask implements BaseTask {
|
||||
@Resource
|
||||
private DownloadService downloadService;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
super.setName("rename-task-thread");
|
||||
public void accept(String threadName, AtomicBoolean loop) {
|
||||
Config config = ConfigUtil.CONFIG;
|
||||
int renameSleepSeconds = config.getRenameSleepSeconds();
|
||||
|
||||
log.info("{} 当前设置间隔为 {} 秒", getName(), renameSleepSeconds);
|
||||
while (loop.get()) {
|
||||
if (!TorrentUtil.login()) {
|
||||
ThreadUtil.sleep(renameSleepSeconds * 1000L);
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
List<TorrentsInfo> torrentsInfos = TorrentUtil.getTorrentsInfos();
|
||||
for (TorrentsInfo torrentsInfo : torrentsInfos) {
|
||||
if (!loop.get()) {
|
||||
return;
|
||||
}
|
||||
Boolean deleteStandbyRSSOnly = config.getDeleteStandbyRSSOnly();
|
||||
try {
|
||||
TorrentUtil.rename(torrentsInfo);
|
||||
SpringUtil.getBean(DownloadService.class).notification(torrentsInfo);
|
||||
if (deleteStandbyRSSOnly) {
|
||||
continue;
|
||||
}
|
||||
TorrentUtil.delete(torrentsInfo);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
String message = ExceptionUtils.getMessage(e);
|
||||
log.error(message, e);
|
||||
}
|
||||
if (!TorrentUtil.login()) {
|
||||
ThreadUtil.sleep(renameSleepSeconds * 1000L);
|
||||
return;
|
||||
}
|
||||
log.info("{} 任务已停止", getName());
|
||||
try {
|
||||
List<TorrentsInfo> torrentsInfos = TorrentUtil.getTorrentsInfos();
|
||||
for (TorrentsInfo torrentsInfo : torrentsInfos) {
|
||||
if (!loop.get()) {
|
||||
return;
|
||||
}
|
||||
Boolean deleteStandbyRSSOnly = config.getDeleteStandbyRSSOnly();
|
||||
try {
|
||||
TorrentUtil.rename(torrentsInfo);
|
||||
downloadService.notification(torrentsInfo);
|
||||
if (deleteStandbyRSSOnly) {
|
||||
continue;
|
||||
}
|
||||
TorrentUtil.delete(torrentsInfo);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
String message = ExceptionUtils.getMessage(e);
|
||||
log.error(message, e);
|
||||
}
|
||||
ThreadUtil.sleep(renameSleepSeconds * 1000L);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import ani.rss.util.other.TorrentUtil;
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
@@ -18,15 +19,13 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
* RSS
|
||||
*/
|
||||
@Slf4j
|
||||
public class RssTask extends Thread {
|
||||
@Component
|
||||
public class RssTask implements BaseTask {
|
||||
public static final AtomicBoolean download = new AtomicBoolean(false);
|
||||
private final AtomicBoolean loop;
|
||||
|
||||
public RssTask(AtomicBoolean loop) {
|
||||
this.loop = loop;
|
||||
}
|
||||
|
||||
public static void download(AtomicBoolean loop) {
|
||||
DownloadService downloadService = SpringUtil.getBean(DownloadService.class);
|
||||
|
||||
try {
|
||||
if (!TorrentUtil.login()) {
|
||||
return;
|
||||
@@ -47,7 +46,7 @@ public class RssTask extends Thread {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
SpringUtil.getBean(DownloadService.class).downloadAni(ani);
|
||||
downloadService.downloadAni(ani);
|
||||
} catch (Exception e) {
|
||||
String message = ExceptionUtils.getMessage(e);
|
||||
log.error("{} {}", title, message);
|
||||
@@ -74,26 +73,23 @@ public class RssTask extends Thread {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
super.setName("rss-task-thread");
|
||||
public void accept(String threadName, AtomicBoolean loop) {
|
||||
Config config = ConfigUtil.CONFIG;
|
||||
Integer sleep = config.getRssSleepMinutes();
|
||||
log.info("{} 当前设置间隔为 {} 分钟", getName(), sleep);
|
||||
while (loop.get()) {
|
||||
if (!config.getRss()) {
|
||||
log.debug("rss未启用");
|
||||
ThreadUtil.sleep(sleep, TimeUnit.MINUTES);
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
sync();
|
||||
download(loop);
|
||||
} catch (Exception e) {
|
||||
String message = ExceptionUtils.getMessage(e);
|
||||
log.error(message, e);
|
||||
}
|
||||
|
||||
if (!config.getRss()) {
|
||||
log.debug("rss未启用");
|
||||
ThreadUtil.sleep(sleep, TimeUnit.MINUTES);
|
||||
return;
|
||||
}
|
||||
log.info("{} 任务已停止", getName());
|
||||
|
||||
try {
|
||||
sync();
|
||||
download(loop);
|
||||
} catch (Exception e) {
|
||||
String message = ExceptionUtils.getMessage(e);
|
||||
log.error(message, e);
|
||||
}
|
||||
ThreadUtil.sleep(sleep, TimeUnit.MINUTES);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user