[Fix] BotEventHandler 修复事件处理线程非预期设置的问题;

[Change] BotEventHandler 设置线程超时时间;
[Fix] CacheStoreCentral 整理'InterruptedException'在'getImageById'的传递路径;
This commit is contained in:
LamGC 2020-06-12 20:00:37 +08:00
parent d9b08f8ad9
commit abcd26f21b
4 changed files with 12 additions and 10 deletions

View File

@ -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);
}

View File

@ -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 "(错误: 图片获取出错)";

View File

@ -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));

View File

@ -107,6 +107,7 @@ public class TimeLimitThreadPoolExecutor extends ThreadPoolExecutor {
while (true) {
try {
long interval = this.timeoutCheckInterval.get();
//noinspection BusyWait 用于等待超时
Thread.sleep(interval);
// 检查是否存在超时的任务