From abcd26f21b9eef26af5c11a04be9b60897872107 Mon Sep 17 00:00:00 2001 From: LamGC Date: Fri, 12 Jun 2020 20:00:37 +0800 Subject: [PATCH] =?UTF-8?q?[Fix]=20BotEventHandler=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E5=A4=84=E7=90=86=E7=BA=BF=E7=A8=8B=E9=9D=9E?= =?UTF-8?q?=E9=A2=84=E6=9C=9F=E8=AE=BE=E7=BD=AE=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?;=20[Change]=20BotEventHandler=20=E8=AE=BE=E7=BD=AE=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B=E8=B6=85=E6=97=B6=E6=97=B6=E9=97=B4;=20[Fix]=20CacheS?= =?UTF-8?q?toreCentral=20=E6=95=B4=E7=90=86'InterruptedException'=E5=9C=A8?= =?UTF-8?q?'getImageById'=E7=9A=84=E4=BC=A0=E9=80=92=E8=B7=AF=E5=BE=84;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java | 9 +++++---- .../java/net/lamgc/cgj/bot/cache/CacheStoreCentral.java | 4 ++-- .../java/net/lamgc/cgj/bot/event/BotEventHandler.java | 8 ++++---- .../net/lamgc/cgj/util/TimeLimitThreadPoolExecutor.java | 1 + 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java b/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java index 9914fc7..2af2b3f 100644 --- a/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java +++ b/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java @@ -91,7 +91,8 @@ public class BotCommandProcess { */ @Command(commandName = "info") public static String artworkInfo(@Argument(name = "$fromGroup") long fromGroup, - @Argument(name = "id") int illustId) { + @Argument(name = "id") int illustId) + throws InterruptedException { if(illustId <= 0) { return "这个作品Id是错误的!"; } @@ -142,7 +143,7 @@ public class BotCommandProcess { @Argument(force = false, name = "force") boolean force, @Argument(force = false, name = "mode", defaultValue = "DAILY") String contentMode, @Argument(force = false, name = "type", defaultValue = "ALL") String contentType - ) { + ) throws InterruptedException { Date queryDate = queryTime; if (queryDate == null) { queryDate = new Date(); @@ -311,7 +312,7 @@ public class BotCommandProcess { @Argument(name = "ex", force = false) String excludeKeywords, @Argument(name = "option", force = false) String contentOption, @Argument(name = "p", force = false, defaultValue = "1") int pagesIndex - ) throws IOException { + ) throws IOException, InterruptedException { log.debug("正在执行搜索..."); JsonObject resultBody = CacheStoreCentral.getCentral() .getSearchBody(content, type, area, includeKeywords, excludeKeywords, contentOption); @@ -494,7 +495,7 @@ public class BotCommandProcess { @Argument(name = "id") int illustId, @Argument(name = "quality", force = false) PixivDownload.PageQuality quality, @Argument(name = "p", force = false, defaultValue = "1") int pageIndex - ) { + ) throws InterruptedException { return CacheStoreCentral.getCentral().getImageById(fromGroup, illustId, quality, pageIndex); } diff --git a/src/main/java/net/lamgc/cgj/bot/cache/CacheStoreCentral.java b/src/main/java/net/lamgc/cgj/bot/cache/CacheStoreCentral.java index 4499c4e..bf85b38 100644 --- a/src/main/java/net/lamgc/cgj/bot/cache/CacheStoreCentral.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/CacheStoreCentral.java @@ -113,7 +113,7 @@ public final class CacheStoreCentral { * @param pageIndex 指定页面索引, 从1开始 * @return 如果成功, 返回BotCode, 否则返回错误信息. */ - public String getImageById(long fromGroup, int illustId, PixivDownload.PageQuality quality, int pageIndex) { + public String getImageById(long fromGroup, int illustId, PixivDownload.PageQuality quality, int pageIndex) throws InterruptedException { log.debug("IllustId: {}, Quality: {}, PageIndex: {}", illustId, quality.name(), pageIndex); if(pageIndex <= 0) { log.warn("指定的页数不能小于或等于0: {}", pageIndex); @@ -188,7 +188,7 @@ public final class CacheStoreCentral { } } catch (InterruptedException e) { log.warn("图片缓存被中断", e); - return "(错误:图片获取超时)"; + throw e; } catch (Throwable e) { log.error("图片 {} 获取失败:\n{}", illustId + "p" + pageIndex, Throwables.getStackTraceAsString(e)); return "(错误: 图片获取出错)"; diff --git a/src/main/java/net/lamgc/cgj/bot/event/BotEventHandler.java b/src/main/java/net/lamgc/cgj/bot/event/BotEventHandler.java index 6902669..867ba27 100644 --- a/src/main/java/net/lamgc/cgj/bot/event/BotEventHandler.java +++ b/src/main/java/net/lamgc/cgj/bot/event/BotEventHandler.java @@ -46,9 +46,9 @@ public class BotEventHandler implements EventHandler { * 消息事件执行器 */ private final static EventExecutor executor = new EventExecutor(new TimeLimitThreadPoolExecutor( - 0, + 180000, // 3min Math.max(Runtime.getRuntime().availableProcessors(), 4), - Math.max(Math.max(Runtime.getRuntime().availableProcessors() * 2, 4), 32), + Math.min(Runtime.getRuntime().availableProcessors() * 2, 32), 30L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1536), @@ -219,8 +219,8 @@ public class BotEventHandler implements EventHandler { } catch(DeveloperRunnerException e) { Throwable cause = e.getCause(); if (cause instanceof InterruptedException) { - log.error("命令执行超时, 终止执行."); - result = "色图姬发现这个命令的处理时间太久了!所以打断了这个命令。"; + log.error("命令执行超时, 终止执行.", cause); + result = "色图姬查阅图库太久,被赶出来了!"; } else if(cause instanceof NoSuchElementException && cause.getMessage().startsWith("No work found: ")) { String message = cause.getMessage(); log.error("指定作品不存在.(Id: {})", message.substring(message.lastIndexOf(": ") + 2)); diff --git a/src/main/java/net/lamgc/cgj/util/TimeLimitThreadPoolExecutor.java b/src/main/java/net/lamgc/cgj/util/TimeLimitThreadPoolExecutor.java index e3b9d42..64d1224 100644 --- a/src/main/java/net/lamgc/cgj/util/TimeLimitThreadPoolExecutor.java +++ b/src/main/java/net/lamgc/cgj/util/TimeLimitThreadPoolExecutor.java @@ -107,6 +107,7 @@ public class TimeLimitThreadPoolExecutor extends ThreadPoolExecutor { while (true) { try { long interval = this.timeoutCheckInterval.get(); + //noinspection BusyWait 用于等待超时 Thread.sleep(interval); // 检查是否存在超时的任务