[Change] Main 调整参数的接收形式;

[Fix] Dockerfile.sample 修复Java应用无法接收stop命令发送信号的问题;
[Change] RankingUpdateTimer 调整更新投递形式, 将同步更新调整为异步更新;
[Change] BotEventHandler 实装TimeLimitThreadPoolExecutor;
This commit is contained in:
LamGC 2020-05-04 02:07:46 +08:00
parent f279d99fda
commit fe213deecb
4 changed files with 64 additions and 31 deletions

View File

@ -1,6 +1,8 @@
FROM openjdk:8u252-jre FROM openjdk:8u252-jre
ENV jarFileName=ContentGrabbingJi-exec.jar ENV jarFileName=ContentGrabbingJi-exec.jar
ENV CGJ_REDIS_URI="127.0.0.1:6379"
ENV CGJ_PROXY=""
COPY ${jarFileName} /CGJ.jar COPY ${jarFileName} /CGJ.jar
RUN mkdir /data/ RUN mkdir /data/

View File

@ -55,8 +55,15 @@ public class Main {
log.debug("Args: {}, LogsPath: {}", Arrays.toString(args), System.getProperty("cgj.logsPath")); log.debug("Args: {}, LogsPath: {}", Arrays.toString(args), System.getProperty("cgj.logsPath"));
log.debug("运行目录: {}", System.getProperty("user.dir")); log.debug("运行目录: {}", System.getProperty("user.dir"));
ArgumentsProperties argsProp = new ArgumentsProperties(args); ArgumentsProperties argsProp = new ArgumentsProperties(args);
if(argsProp.containsKey("proxy")) {
URL proxyUrl = new URL(argsProp.getValue("proxy"));
if(!getSettingToSysProp(argsProp, "proxy", null)) {
getEnvSettingToSysProp("CGJ_PROXY", "proxy", null);
}
String proxyAddress = System.getProperty("cgj.proxy");
if(!Strings.isNullOrEmpty(proxyAddress)) {
URL proxyUrl = new URL(proxyAddress);
proxy = new HttpHost(proxyUrl.getHost(), proxyUrl.getPort()); proxy = new HttpHost(proxyUrl.getHost(), proxyUrl.getPort());
log.info("已启用Http协议代理{}", proxy.toHostString()); log.info("已启用Http协议代理{}", proxy.toHostString());
} else { } else {
@ -67,27 +74,20 @@ public class Main {
log.error("创建文件夹失败!"); log.error("创建文件夹失败!");
} }
if(argsProp.containsKey("botDataDir")) { if(!getSettingToSysProp(argsProp, "botDataDir", "./") &&
log.info("botDataDir: {}", argsProp.getValue("botDataDir")); !getEnvSettingToSysProp("CGJ_BOT_DATA_DIR", "botDataDir", "./")) {
System.setProperty("cgj.botDataDir", argsProp.getValue("botDataDir"));
} else {
log.warn("未设置botDataDir, 当前运行目录将作为酷Q机器人所在目录."); log.warn("未设置botDataDir, 当前运行目录将作为酷Q机器人所在目录.");
System.setProperty("cgj.botDataDir", "./");
} }
if(!getSettingToSysProp(argsProp, "redisAddress", "127.0.0.1") &&
if(argsProp.containsKey("redisAddr")) { !getEnvSettingToSysProp("CGJ_REDIS_URI", "redisAddress", "127.0.0.1")) {
log.info("redisAddress: {}", argsProp.getValue("redisAddr")); log.warn("未设置RedisAddress, 将使用默认值连接Redis服务器(127.0.0.1:6379)");
System.setProperty("cgj.redisAddress", argsProp.getValue("redisAddr"));
} else {
log.info("未设置RedisAddress, 将使用默认值连接Redis服务器(127.0.0.1:6379)");
System.setProperty("cgj.redisAddress", "127.0.0.1");
} }
File cookieStoreFile = new File(System.getProperty("cgj.botDataDir"), "cookies.store"); File cookieStoreFile = new File(System.getProperty("cgj.botDataDir"), "cookies.store");
if(!cookieStoreFile.exists()) { if(!cookieStoreFile.exists()) {
log.warn("未找到cookies.store文件, 是否启动PixivLoginProxyServer? (yes/no)"); log.warn("未找到cookies.store文件, 是否启动PixivLoginProxyServer? (yes/no)");
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
if(scanner.nextLine().equalsIgnoreCase("yes")) { if(scanner.nextLine().trim().equalsIgnoreCase("yes")) {
startPixivLoginProxyServer(); startPixivLoginProxyServer();
} else { } else {
System.exit(1); System.exit(1);
@ -104,6 +104,37 @@ public class Main {
ArgumentsRunner.run(Main.class, args); ArgumentsRunner.run(Main.class, args);
} }
/**
* 从ArgumentsProperties获取设置项到System Properties
* @param prop ArgumentsProperties对象
* @param key 设置项key
* @param defaultValue 默认值
* @return 如果成功从ArgumentsProperties获得设置项, 返回true, 如未找到(使用了defaultValue或null), 返回false;
*/
private static boolean getSettingToSysProp(ArgumentsProperties prop, String key, String defaultValue) {
if(prop.containsKey(key)) {
log.info("{}: {}", key, prop.getValue(key));
System.setProperty("cgj." + key, prop.getValue(key));
return true;
} else {
if(defaultValue != null) {
System.setProperty("cgj." + key, defaultValue);
}
return false;
}
}
private static boolean getEnvSettingToSysProp(String envKey, String sysPropKey, String defaultValue) {
String env = System.getenv(envKey);
if(env != null) {
System.setProperty("cgj." + sysPropKey, env);
return true;
} else {
System.setProperty("cgj." + sysPropKey, defaultValue);
return false;
}
}
@Command @Command
public static void botMode(@Argument(name = "args", force = false) String argsStr) { public static void botMode(@Argument(name = "args", force = false) String argsStr) {
new MiraiMain().init(); new MiraiMain().init();

View File

@ -1,6 +1,5 @@
package net.lamgc.cgj.bot; package net.lamgc.cgj.bot;
import com.google.common.base.Throwables;
import net.lamgc.cgj.bot.event.BotEventHandler; import net.lamgc.cgj.bot.event.BotEventHandler;
import net.lamgc.cgj.bot.event.VirtualLoadMessageEvent; import net.lamgc.cgj.bot.event.VirtualLoadMessageEvent;
import net.lamgc.cgj.pixiv.PixivURL; import net.lamgc.cgj.pixiv.PixivURL;
@ -28,10 +27,10 @@ public class RankingUpdateTimer {
cal.setTime(firstRunDate == null ? new Date() : firstRunDate); cal.setTime(firstRunDate == null ? new Date() : firstRunDate);
LocalDate currentLocalDate = LocalDate.now(); LocalDate currentLocalDate = LocalDate.now();
if(cal.get(Calendar.DAY_OF_YEAR) <= currentLocalDate.getDayOfYear() && cal.get(Calendar.HOUR_OF_DAY) >= 12) { if(cal.get(Calendar.DAY_OF_YEAR) <= currentLocalDate.getDayOfYear() && cal.get(Calendar.HOUR_OF_DAY) >= 12) {
cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) + 1); cal.set(Calendar.DAY_OF_YEAR, currentLocalDate.getDayOfYear() + 1);
} }
cal.set(Calendar.HOUR_OF_DAY, 12); cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.MINUTE, 30); cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0); cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0); cal.set(Calendar.MILLISECOND, 0);
@ -66,15 +65,10 @@ public class RankingUpdateTimer {
log.debug("不支持的类型, 填空值跳过...(类型: {}.{})", rankingMode.name(), contentType.name()); log.debug("不支持的类型, 填空值跳过...(类型: {}.{})", rankingMode.name(), contentType.name());
} }
log.info("当前排行榜类型: {}.{}, 正在更新...", rankingMode.name(), contentType.name()); log.info("当前排行榜类型: {}.{}, 正在更新...", rankingMode.name(), contentType.name());
try { BotEventHandler.executor.executor(
//BotCommandProcess.getRankingInfoByCache(contentType, rankingMode, calendar.getTime(), 1, 0, true);
BotEventHandler.executor.executorSync(
new VirtualLoadMessageEvent(0,0, new VirtualLoadMessageEvent(0,0,
".cgj ranking -type=" + contentType.name() + " -mode=" + rankingMode.name())); ".cgj ranking -type=" + contentType.name() + " -mode=" + rankingMode.name()));
log.info("排行榜 {}.{} 更新完成.", rankingMode.name(), contentType.name()); log.info("排行榜 {}.{} 负载指令已投递.", rankingMode.name(), contentType.name());
} catch (InterruptedException e) {
log.error("排行榜 {}.{} 更新时发生异常. \n{}", rankingMode.name(), contentType.name(), Throwables.getStackTraceAsString(e));
}
} }
} }
log.warn("定时任务更新完成."); log.warn("定时任务更新完成.");

View File

@ -7,6 +7,7 @@ import net.lamgc.cgj.bot.BotAdminCommandProcess;
import net.lamgc.cgj.bot.BotCommandProcess; import net.lamgc.cgj.bot.BotCommandProcess;
import net.lamgc.cgj.util.DateParser; import net.lamgc.cgj.util.DateParser;
import net.lamgc.cgj.util.PagesQualityParser; import net.lamgc.cgj.util.PagesQualityParser;
import net.lamgc.cgj.util.TimeLimitThreadPoolExecutor;
import net.lamgc.utils.base.runner.ArgumentsRunner; import net.lamgc.utils.base.runner.ArgumentsRunner;
import net.lamgc.utils.base.runner.ArgumentsRunnerConfig; import net.lamgc.utils.base.runner.ArgumentsRunnerConfig;
import net.lamgc.utils.base.runner.exception.DeveloperRunnerException; import net.lamgc.utils.base.runner.exception.DeveloperRunnerException;
@ -25,7 +26,6 @@ import java.net.URI;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -50,7 +50,8 @@ public class BotEventHandler implements EventHandler {
/** /**
* 消息事件执行器 * 消息事件执行器
*/ */
public final static EventExecutor executor = new EventExecutor(new ThreadPoolExecutor( public final static EventExecutor executor = new EventExecutor(new TimeLimitThreadPoolExecutor(
60 * 1000,
(int) Math.ceil(Runtime.getRuntime().availableProcessors() / 2F), (int) Math.ceil(Runtime.getRuntime().availableProcessors() / 2F),
Runtime.getRuntime().availableProcessors(), Runtime.getRuntime().availableProcessors(),
30L, 30L,
@ -185,8 +186,13 @@ public class BotEventHandler implements EventHandler {
} catch(ParameterNoFoundException e) { } catch(ParameterNoFoundException e) {
result = "命令缺少参数: " + e.getParameterName(); result = "命令缺少参数: " + e.getParameterName();
} catch(DeveloperRunnerException e) { } catch(DeveloperRunnerException e) {
if (!(e.getCause() instanceof InterruptedException)) {
log.error("执行命令时发生异常", e); log.error("执行命令时发生异常", e);
result = "命令执行时发生错误,无法完成!"; result = "色图姬在执行命令时遇到了一个错误!";
} else {
log.error("命令执行超时, 终止执行.");
result = "色图姬发现这个命令的处理时间太久了!所以打断了这个命令。";
}
} }
long processTime = System.currentTimeMillis() - time; long processTime = System.currentTimeMillis() - time;
if(Objects.requireNonNull(result) instanceof String) { if(Objects.requireNonNull(result) instanceof String) {