From f963c76d9d0ed0a241c32e25cbb3162254210047 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Fri, 20 May 2022 19:10:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20daemon=20=E5=8C=85?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E5=90=83=E8=8D=AF=E6=8F=90=E9=86=92?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=20#35?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit > merged from major/0.7-sp/medication-time - 添加 annie 的吃药提醒功能 #35 - 添加了 MornyDaemons 用于管理 morny 常驻任务 - 将 TrackerDataManager 移入 daemon 部分 - 一些 log 的更改 - 启动提示的 "System" 改为 "Coeur" - 为 tracker daemon 的启动关闭也添加了log --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../cc/sukazyo/cono/morny/MornyCoeur.java | 14 +++--- .../morny/bot/event/OnActivityRecord.java | 2 +- .../cono/morny/daemon/MedicationTimer.java | 47 +++++++++++++++++++ .../cono/morny/daemon/MornyDaemons.java | 31 ++++++++++++ .../TrackerDataManager.java | 4 +- .../cono/morny/data/TelegramStickers.java | 1 + 8 files changed, 93 insertions(+), 12 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/daemon/MornyDaemons.java rename src/main/java/cc/sukazyo/cono/morny/{data/tracker => daemon}/TrackerDataManager.java (97%) diff --git a/gradle.properties b/gradle.properties index ad8c396..d9dfb9e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.7.0.6 +VERSION = 0.7.0.7 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index c4542ff..f97e70e 100644 --- a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java +++ b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java @@ -4,6 +4,6 @@ package cc.sukazyo.cono.morny; * the final field that will be updated by gradle automatically. */ public class GradleProjectConfigures { - public static final String VERSION = "0.7.0.6"; - public static final long COMPILE_TIMESTAMP = 1652946142762L; + public static final String VERSION = "0.7.0.7"; + public static final long COMPILE_TIMESTAMP = 1653044177443L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java index 23c7539..e61cbbb 100644 --- a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java +++ b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java @@ -4,7 +4,8 @@ import cc.sukazyo.cono.morny.bot.api.OnUpdate; import cc.sukazyo.cono.morny.bot.command.MornyCommands; import cc.sukazyo.cono.morny.bot.event.EventListeners; import cc.sukazyo.cono.morny.bot.query.MornyQueries; -import cc.sukazyo.cono.morny.data.tracker.TrackerDataManager; +import cc.sukazyo.cono.morny.daemon.MornyDaemons; +import cc.sukazyo.cono.morny.daemon.TrackerDataManager; import cc.sukazyo.untitled.telegram.api.extra.ExtraAction; import com.pengrad.telegrambot.TelegramBot; @@ -133,24 +134,24 @@ public class MornyCoeur { boolean isAutomaticResetCommandList, boolean isRemoveCommandListWhenExit ) { if (INSTANCE == null) { - logger.info("System Starting"); + logger.info("Coeur Starting"); INSTANCE = new MornyCoeur( botKey, botUsername, master, trustedChat, latestEventTimestamp, isRemoveCommandListWhenExit ); - TrackerDataManager.init(); + MornyDaemons.start(); EventListeners.registerAllListeners(); INSTANCE.account.setUpdatesListener(OnUpdate::onNormalUpdate); if (isAutomaticResetCommandList) { logger.info("resetting telegram command list"); commandManager().automaticUpdateList(); } - logger.info("System start complete"); + logger.info("Coeur start complete"); return; } - logger.error("System already started coeur!!!"); + logger.error("Coeur already started!!!"); } /** @@ -166,8 +167,7 @@ public class MornyCoeur { */ private void exitCleanup () { logger.info("clean:save tracker data."); - TrackerDataManager.DAEMON.interrupt(); - TrackerDataManager.trackingLock.lock(); + MornyDaemons.stop(); if (isRemoveCommandListWhenExit) { commandManager.automaticRemoveList(); } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnActivityRecord.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnActivityRecord.java index 9c3ed6e..6b26436 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnActivityRecord.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnActivityRecord.java @@ -1,7 +1,7 @@ package cc.sukazyo.cono.morny.bot.event; import cc.sukazyo.cono.morny.bot.api.EventListener; -import cc.sukazyo.cono.morny.data.tracker.TrackerDataManager; +import cc.sukazyo.cono.morny.daemon.TrackerDataManager; import com.pengrad.telegrambot.model.Chat; import com.pengrad.telegrambot.model.Update; diff --git a/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java b/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java new file mode 100644 index 0000000..f081bf2 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java @@ -0,0 +1,47 @@ +package cc.sukazyo.cono.morny.daemon; + +import cc.sukazyo.cono.morny.MornyCoeur; +import cc.sukazyo.cono.morny.data.TelegramStickers; +import com.pengrad.telegrambot.request.SendSticker; + +import static cc.sukazyo.cono.morny.Log.logger; + +public class MedicationTimer extends Thread { + + public static final long NOTIFY_RECEIVE_CHAT = 5028252995L; + + MedicationTimer () { + super("TIMER_Medication"); + } + + @Override + public void run () { + logger.info("MedicationTimer started"); + while (!interrupted()) { + try { + waitToNextRoutine(); + sendNotification(); + } catch (InterruptedException e) { + interrupt(); + logger.info("MedicationTimer was interrupted, will be exit now"); + } catch (Exception e) { + logger.error("Unexpected error occurred"); + e.printStackTrace(System.out); + } + } + logger.info("MedicationTimer stopped"); + } + + private static void sendNotification () { + MornyCoeur.extra().exec(new SendSticker(NOTIFY_RECEIVE_CHAT, TelegramStickers.ID_PROGYNOVA)); + } + + private static long calcNextRoutineTimestamp () { + return ((System.currentTimeMillis()+8*60*60*1000) / (12*60*60*1000) + 1) * 12*60*60*1000 - 8*60*60*1000; + } + + private void waitToNextRoutine () throws InterruptedException { + sleep(calcNextRoutineTimestamp() - System.currentTimeMillis()); + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/daemon/MornyDaemons.java b/src/main/java/cc/sukazyo/cono/morny/daemon/MornyDaemons.java new file mode 100644 index 0000000..7fe2678 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/daemon/MornyDaemons.java @@ -0,0 +1,31 @@ +package cc.sukazyo.cono.morny.daemon; + +import static cc.sukazyo.cono.morny.Log.logger; + +public class MornyDaemons { + + static MedicationTimer medicationTimerInstance; + + public static void start () { + logger.info("ALL Morny Daemons starting..."); + TrackerDataManager.init(); + medicationTimerInstance = new MedicationTimer(); + medicationTimerInstance.start(); + logger.info("Morny Daemons started."); + } + + public static void stop () { + + logger.info("ALL Morny Daemons stopping..."); + + TrackerDataManager.DAEMON.interrupt(); + medicationTimerInstance.interrupt(); + + TrackerDataManager.trackingLock.lock(); + try { medicationTimerInstance.join(); } catch (InterruptedException e) { e.printStackTrace(System.out); } + + logger.info("ALL Morny Daemons STOPPED."); + + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/data/tracker/TrackerDataManager.java b/src/main/java/cc/sukazyo/cono/morny/daemon/TrackerDataManager.java similarity index 97% rename from src/main/java/cc/sukazyo/cono/morny/data/tracker/TrackerDataManager.java rename to src/main/java/cc/sukazyo/cono/morny/daemon/TrackerDataManager.java index 7b2372f..d0f8ab0 100644 --- a/src/main/java/cc/sukazyo/cono/morny/data/tracker/TrackerDataManager.java +++ b/src/main/java/cc/sukazyo/cono/morny/daemon/TrackerDataManager.java @@ -1,4 +1,4 @@ -package cc.sukazyo.cono.morny.data.tracker; +package cc.sukazyo.cono.morny.daemon; import java.io.File; import java.io.FileOutputStream; @@ -27,6 +27,7 @@ public class TrackerDataManager { @Override public void run () { trackingLock.lock(); + logger.info("Tracker started."); long lastWaitTimestamp = System.currentTimeMillis(); boolean postProcess = false; do { @@ -48,6 +49,7 @@ public class TrackerDataManager { else logger.info("nothing to do yet"); } while (!postProcess); trackingLock.unlock(); + logger.info("Tracker exited."); } } diff --git a/src/main/java/cc/sukazyo/cono/morny/data/TelegramStickers.java b/src/main/java/cc/sukazyo/cono/morny/data/TelegramStickers.java index c53024c..ecf4921 100644 --- a/src/main/java/cc/sukazyo/cono/morny/data/TelegramStickers.java +++ b/src/main/java/cc/sukazyo/cono/morny/data/TelegramStickers.java @@ -14,5 +14,6 @@ public class TelegramStickers { public static final String ID_WAITING = "CAACAgEAAx0CSQh32gABA-8DYbh7W2VhJ490ucfZMUMrgMR2FW4AAm4nAAJ4_MYFjx6zpxJPWsQjBA"; public static final String ID_SENT = "CAACAgEAAx0CSQh32gABA--zYbiyU_wOijEitp-0tSl_k7W6l3gAAgMmAAJ4_MYF4GrompjXPx4jBA"; public static final String ID_SAVED = "CAACAgEAAx0CSQh32gABBExuYdB_G0srfhQldRWkBYxWzCOv4-IAApooAAJ4_MYFcjuNZszfQcQjBA"; + public static final String ID_PROGYNOVA = "CAACAgUAAxkBAAICm2KEuL7UQqNP7vSPCg2DHJIND6UsAAKLAwACH4WSBszIo722aQ3jJAQ"; }