[Change] 调整 runUpdateTimer 命令为 runUpdateTask 并将命令移到 CQBotAdminProcess 中;

[Fix] 修复 RankingUpdateTimer 时间错误的问题;
This commit is contained in:
LamGC 2020-04-02 00:33:22 +08:00
parent 9a2a69ea41
commit ce93c7372e
3 changed files with 42 additions and 22 deletions

View File

@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Date;
public class CQBotAdminProcess { public class CQBotAdminProcess {
@ -55,4 +56,15 @@ public class CQBotAdminProcess {
} }
} }
@Command
public String runUpdateTask(@Argument(force = false, name = "date") Date queryTime) {
try {
CQProcess.runUpdateTimer(queryTime);
} catch (Exception e) {
log.error("执行更新任务时发生异常", e);
return "操作执行时发生错误!";
}
return "操作已完成.";
}
} }

View File

@ -88,8 +88,7 @@ public class CQProcess {
log.info("初始化完成."); log.info("初始化完成.");
} }
@Command public static String runUpdateTimer(Date queryTime) {
public static String runUpdateTimer(@Argument(force = false, name = "date") Date queryTime) {
log.info("正在手动触发排行榜更新任务..."); log.info("正在手动触发排行榜更新任务...");
updateTimer.now(queryTime); updateTimer.now(queryTime);
log.info("任务执行结束."); log.info("任务执行结束.");
@ -169,7 +168,7 @@ public class CQProcess {
log.warn("配置项 {} 的参数值格式有误!", imageLimitPropertyKey); log.warn("配置项 {} 的参数值格式有误!", imageLimitPropertyKey);
} }
for (JsonObject rankInfo : getRankingInfoByCache(type, mode, queryDate, 1, itemLimit)) { for (JsonObject rankInfo : getRankingInfoByCache(type, mode, queryDate, 1, itemLimit, false)) {
index++; index++;
int rank = rankInfo.get("rank").getAsInt(); int rank = rankInfo.get("rank").getAsInt();
int illustId = rankInfo.get("illust_id").getAsInt(); int illustId = rankInfo.get("illust_id").getAsInt();
@ -306,8 +305,8 @@ public class CQProcess {
illustsArray.forEach(illustsList::add); illustsArray.forEach(illustsList::add);
illustsList.sort((o1, o2) -> { illustsList.sort((o1, o2) -> {
try { try {
int illustLikeCount1 = getIllustPreLoadData(o1.getAsJsonObject().get("illustId").getAsInt()).get("likeCount").getAsInt(); int illustLikeCount1 = getIllustPreLoadData(o1.getAsJsonObject().get("illustId").getAsInt(), false).get("likeCount").getAsInt();
int illustLikeCount2 = getIllustPreLoadData(o2.getAsJsonObject().get("illustId").getAsInt()).get("likeCount").getAsInt(); int illustLikeCount2 = getIllustPreLoadData(o2.getAsJsonObject().get("illustId").getAsInt(), false).get("likeCount").getAsInt();
return Integer.compare(illustLikeCount2, illustLikeCount1); return Integer.compare(illustLikeCount2, illustLikeCount1);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -401,7 +400,7 @@ public class CQProcess {
log.info("IllustId: {}, Quality: {}, PageIndex: {}", illustId, quality.name(), pageIndex); log.info("IllustId: {}, Quality: {}, PageIndex: {}", illustId, quality.name(), pageIndex);
List<String> pagesList; List<String> pagesList;
try { try {
pagesList = getIllustPages(illustId, quality); pagesList = getIllustPages(illustId, quality, false);
} catch (IOException e) { } catch (IOException e) {
log.error("获取下载链接列表时发生异常", e); log.error("获取下载链接列表时发生异常", e);
return "发生网络异常,无法获取图片!"; return "发生网络异常,无法获取图片!";
@ -503,15 +502,15 @@ public class CQProcess {
*/ */
private static boolean isNoSafe(int illustId, Properties settingProp, boolean returnRaw) throws IOException { private static boolean isNoSafe(int illustId, Properties settingProp, boolean returnRaw) throws IOException {
boolean rawValue = getIllustInfo(illustId).getAsJsonArray("tags").contains(new JsonPrimitive("R-18")); boolean rawValue = getIllustInfo(illustId, false).getAsJsonArray("tags").contains(new JsonPrimitive("R-18"));
return returnRaw || settingProp == null ? rawValue : rawValue && !settingProp.getProperty("image.allowR18", "false").equalsIgnoreCase("true"); return returnRaw || settingProp == null ? rawValue : rawValue && !settingProp.getProperty("image.allowR18", "false").equalsIgnoreCase("true");
} }
private static JsonObject getIllustInfo(int illustId) throws IOException { private static JsonObject getIllustInfo(int illustId, boolean flushCache) throws IOException {
String illustIdStr = buildSyncKey(Integer.toString(illustId)); String illustIdStr = buildSyncKey(Integer.toString(illustId));
if (!illustInfoCache.exists(illustIdStr)) { if (!illustInfoCache.exists(illustIdStr) || flushCache) {
synchronized (illustIdStr) { synchronized (illustIdStr) {
if (!illustInfoCache.exists(illustIdStr)) { if (!illustInfoCache.exists(illustIdStr) || flushCache) {
JsonObject illustInfoObj = pixivDownload.getIllustInfoByIllustId(illustId); JsonObject illustInfoObj = pixivDownload.getIllustInfoByIllustId(illustId);
illustInfoCache.update(illustIdStr, illustInfoObj, null); illustInfoCache.update(illustIdStr, illustInfoObj, null);
} }
@ -520,11 +519,11 @@ public class CQProcess {
return illustInfoCache.getCache(illustIdStr).getAsJsonObject(); return illustInfoCache.getCache(illustIdStr).getAsJsonObject();
} }
public static JsonObject getIllustPreLoadData(int illustId) throws IOException { public static JsonObject getIllustPreLoadData(int illustId, boolean flushCache) throws IOException {
String illustIdStr = buildSyncKey(Integer.toString(illustId)); String illustIdStr = buildSyncKey(Integer.toString(illustId));
if (!illustPreLoadDataCache.exists(illustIdStr)) { if (!illustPreLoadDataCache.exists(illustIdStr) || flushCache) {
synchronized (illustIdStr) { synchronized (illustIdStr) {
if (!illustPreLoadDataCache.exists(illustIdStr)) { if (!illustPreLoadDataCache.exists(illustIdStr) || flushCache) {
log.info("缓存失效, 正在更新..."); log.info("缓存失效, 正在更新...");
JsonObject preLoadDataObj = pixivDownload.getIllustPreLoadDataById(illustId) JsonObject preLoadDataObj = pixivDownload.getIllustPreLoadDataById(illustId)
.getAsJsonObject("illust") .getAsJsonObject("illust")
@ -549,11 +548,11 @@ public class CQProcess {
return illustPreLoadDataCache.getCache(illustIdStr).getAsJsonObject(); return illustPreLoadDataCache.getCache(illustIdStr).getAsJsonObject();
} }
public static List<String> getIllustPages(int illustId, PixivDownload.PageQuality quality) throws IOException { public static List<String> getIllustPages(int illustId, PixivDownload.PageQuality quality, boolean flushCache) throws IOException {
String pagesSign = buildSyncKey(Integer.toString(illustId), ".", quality.name()); String pagesSign = buildSyncKey(Integer.toString(illustId), ".", quality.name());
if (!pagesCache.exists(pagesSign)) { if (!pagesCache.exists(pagesSign) || flushCache) {
synchronized (pagesSign) { synchronized (pagesSign) {
if (!pagesCache.exists(pagesSign)) { if (!pagesCache.exists(pagesSign) || flushCache) {
List<String> linkList = PixivDownload.getIllustAllPageDownload(pixivDownload.getHttpClient(), pixivDownload.getCookieStore(), illustId, quality); List<String> linkList = PixivDownload.getIllustAllPageDownload(pixivDownload.getHttpClient(), pixivDownload.getCookieStore(), illustId, quality);
pagesCache.update(pagesSign, linkList, null); pagesCache.update(pagesSign, linkList, null);
} }
@ -578,10 +577,11 @@ public class CQProcess {
* @param queryDate 查询时间 * @param queryDate 查询时间
* @param start 开始排名, 从1开始 * @param start 开始排名, 从1开始
* @param range 取范围 * @param range 取范围
* @param flushCache
* @return 成功返回有值List, 失败且无异常返回空 * @return 成功返回有值List, 失败且无异常返回空
* @throws IOException 获取异常时抛出 * @throws IOException 获取异常时抛出
*/ */
public static List<JsonObject> getRankingInfoByCache(PixivURL.RankingContentType contentType, PixivURL.RankingMode mode, Date queryDate, int start, int range) throws IOException { public static List<JsonObject> getRankingInfoByCache(PixivURL.RankingContentType contentType, PixivURL.RankingMode mode, Date queryDate, int start, int range, boolean flushCache) throws IOException {
if(!contentType.isSupportedMode(mode)) { if(!contentType.isSupportedMode(mode)) {
log.warn("试图获取不支持的排行榜类型已拒绝.(ContentType: {}, RankingMode: {})", contentType.name(), mode.name()); log.warn("试图获取不支持的排行榜类型已拒绝.(ContentType: {}, RankingMode: {})", contentType.name(), mode.name());
if(log.isDebugEnabled()) { if(log.isDebugEnabled()) {
@ -597,9 +597,9 @@ public class CQProcess {
String date = new SimpleDateFormat("yyyyMMdd").format(queryDate); String date = new SimpleDateFormat("yyyyMMdd").format(queryDate);
//int requestSign = ("Ranking." + contentType.name() + "." + mode.name() + "." + date).hashCode(); //int requestSign = ("Ranking." + contentType.name() + "." + mode.name() + "." + date).hashCode();
String requestSign = buildSyncKey(contentType.name(), ".", mode.name(), ".", date); String requestSign = buildSyncKey(contentType.name(), ".", mode.name(), ".", date);
if(!rankingCache.exists(requestSign)) { if(!rankingCache.exists(requestSign) || flushCache) {
synchronized(requestSign) { synchronized(requestSign) {
if(!rankingCache.exists(requestSign)) { if(!rankingCache.exists(requestSign) || flushCache) {
log.info("Ranking缓存失效, 正在更新...(RequestSign: {})", requestSign); log.info("Ranking缓存失效, 正在更新...(RequestSign: {})", requestSign);
List<JsonObject> rankingResult = pixivDownload.getRanking(contentType, mode, queryDate, 1, 500); List<JsonObject> rankingResult = pixivDownload.getRanking(contentType, mode, queryDate, 1, 500);
JsonArray rankingArr = new JsonArray(rankingResult.size()); JsonArray rankingArr = new JsonArray(rankingResult.size());

View File

@ -46,9 +46,17 @@ public class RankingUpdateTimer {
log.info("当前时间 {}, 定时任务开始执行...", new Date()); log.info("当前时间 {}, 定时任务开始执行...", new Date());
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
calendar.setTime(queryDate == null ? new Date() : queryDate); calendar.setTime(queryDate == null ? new Date() : queryDate);
if(queryDate == null) {
calendar.add(Calendar.DATE, -1); LocalDate currentLocalDate = LocalDate.now();
if(calendar.get(Calendar.DAY_OF_YEAR) == currentLocalDate.getDayOfYear() ||
calendar.get(Calendar.DAY_OF_YEAR) == currentLocalDate.getDayOfYear() - 1) {
if(calendar.get(Calendar.HOUR_OF_DAY) < 12) {
calendar.add(Calendar.DAY_OF_YEAR, -2);
} else {
calendar.add(Calendar.DAY_OF_YEAR, -1);
}
} }
log.info("正在获取 {} 期排行榜数据...", calendar.getTime()); log.info("正在获取 {} 期排行榜数据...", calendar.getTime());
for (PixivURL.RankingMode rankingMode : PixivURL.RankingMode.values()) { for (PixivURL.RankingMode rankingMode : PixivURL.RankingMode.values()) {
for (PixivURL.RankingContentType contentType : PixivURL.RankingContentType.values()) { for (PixivURL.RankingContentType contentType : PixivURL.RankingContentType.values()) {
@ -57,7 +65,7 @@ public class RankingUpdateTimer {
} }
log.info("当前排行榜类型: {}.{}, 正在更新...", rankingMode.name(), contentType.name()); log.info("当前排行榜类型: {}.{}, 正在更新...", rankingMode.name(), contentType.name());
try { try {
CQProcess.getRankingInfoByCache(contentType, rankingMode, calendar.getTime(), 1, 0); CQProcess.getRankingInfoByCache(contentType, rankingMode, calendar.getTime(), 1, 0, true);
log.info("排行榜 {}.{} 更新完成.", rankingMode.name(), contentType.name()); log.info("排行榜 {}.{} 更新完成.", rankingMode.name(), contentType.name());
} catch (IOException e) { } catch (IOException e) {
log.error("排行榜 {}.{} 更新时发生异常", rankingMode.name(), contentType.name()); log.error("排行榜 {}.{} 更新时发生异常", rankingMode.name(), contentType.name());