diff --git a/Dockerfile.sample b/Dockerfile.sample index 3b199b5..4aabdc4 100644 --- a/Dockerfile.sample +++ b/Dockerfile.sample @@ -1,6 +1,8 @@ FROM openjdk:8u252-jre ENV jarFileName=ContentGrabbingJi-exec.jar +ENV CGJ_REDIS_URI="127.0.0.1:6379" +ENV CGJ_PROXY="" COPY ${jarFileName} /CGJ.jar RUN mkdir /data/ diff --git a/src/main/java/net/lamgc/cgj/Main.java b/src/main/java/net/lamgc/cgj/Main.java index 407ce17..5b76fb6 100644 --- a/src/main/java/net/lamgc/cgj/Main.java +++ b/src/main/java/net/lamgc/cgj/Main.java @@ -55,8 +55,15 @@ public class Main { log.debug("Args: {}, LogsPath: {}", Arrays.toString(args), System.getProperty("cgj.logsPath")); log.debug("运行目录: {}", System.getProperty("user.dir")); 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()); log.info("已启用Http协议代理:{}", proxy.toHostString()); } else { @@ -67,27 +74,20 @@ public class Main { log.error("创建文件夹失败!"); } - if(argsProp.containsKey("botDataDir")) { - log.info("botDataDir: {}", argsProp.getValue("botDataDir")); - System.setProperty("cgj.botDataDir", argsProp.getValue("botDataDir")); - } else { + if(!getSettingToSysProp(argsProp, "botDataDir", "./") && + !getEnvSettingToSysProp("CGJ_BOT_DATA_DIR", "botDataDir", "./")) { log.warn("未设置botDataDir, 当前运行目录将作为酷Q机器人所在目录."); - System.setProperty("cgj.botDataDir", "./"); } - - if(argsProp.containsKey("redisAddr")) { - log.info("redisAddress: {}", argsProp.getValue("redisAddr")); - 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"); + if(!getSettingToSysProp(argsProp, "redisAddress", "127.0.0.1") && + !getEnvSettingToSysProp("CGJ_REDIS_URI", "redisAddress", "127.0.0.1")) { + log.warn("未设置RedisAddress, 将使用默认值连接Redis服务器(127.0.0.1:6379)"); } File cookieStoreFile = new File(System.getProperty("cgj.botDataDir"), "cookies.store"); if(!cookieStoreFile.exists()) { log.warn("未找到cookies.store文件, 是否启动PixivLoginProxyServer? (yes/no)"); Scanner scanner = new Scanner(System.in); - if(scanner.nextLine().equalsIgnoreCase("yes")) { + if(scanner.nextLine().trim().equalsIgnoreCase("yes")) { startPixivLoginProxyServer(); } else { System.exit(1); @@ -104,6 +104,37 @@ public class Main { 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 public static void botMode(@Argument(name = "args", force = false) String argsStr) { new MiraiMain().init(); diff --git a/src/main/java/net/lamgc/cgj/bot/RankingUpdateTimer.java b/src/main/java/net/lamgc/cgj/bot/RankingUpdateTimer.java index bb4a836..7c7e0ec 100644 --- a/src/main/java/net/lamgc/cgj/bot/RankingUpdateTimer.java +++ b/src/main/java/net/lamgc/cgj/bot/RankingUpdateTimer.java @@ -1,6 +1,5 @@ package net.lamgc.cgj.bot; -import com.google.common.base.Throwables; import net.lamgc.cgj.bot.event.BotEventHandler; import net.lamgc.cgj.bot.event.VirtualLoadMessageEvent; import net.lamgc.cgj.pixiv.PixivURL; @@ -28,10 +27,10 @@ public class RankingUpdateTimer { cal.setTime(firstRunDate == null ? new Date() : firstRunDate); LocalDate currentLocalDate = LocalDate.now(); 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.MINUTE, 30); + cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); @@ -66,15 +65,10 @@ public class RankingUpdateTimer { log.debug("不支持的类型, 填空值跳过...(类型: {}.{})", rankingMode.name(), contentType.name()); } log.info("当前排行榜类型: {}.{}, 正在更新...", rankingMode.name(), contentType.name()); - try { - //BotCommandProcess.getRankingInfoByCache(contentType, rankingMode, calendar.getTime(), 1, 0, true); - BotEventHandler.executor.executorSync( - new VirtualLoadMessageEvent(0,0, - ".cgj ranking -type=" + contentType.name() + " -mode=" + rankingMode.name())); - log.info("排行榜 {}.{} 更新完成.", rankingMode.name(), contentType.name()); - } catch (InterruptedException e) { - log.error("排行榜 {}.{} 更新时发生异常. \n{}", rankingMode.name(), contentType.name(), Throwables.getStackTraceAsString(e)); - } + BotEventHandler.executor.executor( + new VirtualLoadMessageEvent(0,0, + ".cgj ranking -type=" + contentType.name() + " -mode=" + rankingMode.name())); + log.info("排行榜 {}.{} 负载指令已投递.", rankingMode.name(), contentType.name()); } } log.warn("定时任务更新完成."); 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 5e22dd2..a74eaa0 100644 --- a/src/main/java/net/lamgc/cgj/bot/event/BotEventHandler.java +++ b/src/main/java/net/lamgc/cgj/bot/event/BotEventHandler.java @@ -7,6 +7,7 @@ import net.lamgc.cgj.bot.BotAdminCommandProcess; import net.lamgc.cgj.bot.BotCommandProcess; import net.lamgc.cgj.util.DateParser; 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.ArgumentsRunnerConfig; import net.lamgc.utils.base.runner.exception.DeveloperRunnerException; @@ -25,7 +26,6 @@ import java.net.URI; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; 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), Runtime.getRuntime().availableProcessors(), 30L, @@ -185,8 +186,13 @@ public class BotEventHandler implements EventHandler { } catch(ParameterNoFoundException e) { result = "命令缺少参数: " + e.getParameterName(); } catch(DeveloperRunnerException e) { - log.error("执行命令时发生异常", e); - result = "命令执行时发生错误,无法完成!"; + if (!(e.getCause() instanceof InterruptedException)) { + log.error("执行命令时发生异常", e); + result = "色图姬在执行命令时遇到了一个错误!"; + } else { + log.error("命令执行超时, 终止执行."); + result = "色图姬发现这个命令的处理时间太久了!所以打断了这个命令。"; + } } long processTime = System.currentTimeMillis() - time; if(Objects.requireNonNull(result) instanceof String) {