add exception catch for Morny Coeur Lifecycle Event calls, fix MedicationTimer onExit event failed when no timer runs.

This commit is contained in:
A.C.Sukazyo Eyre 2024-06-15 14:26:50 +08:00
parent d40dae4453
commit 11542377d5
Signed by: Eyre_S
GPG Key ID: C17CE40291207874
5 changed files with 30 additions and 16 deletions

View File

@ -5,7 +5,7 @@
</option> </option>
<option name="MAIN_CLASS_NAME" value="cc.sukazyo.cono.morny.core.ServerMain" /> <option name="MAIN_CLASS_NAME" value="cc.sukazyo.cono.morny.core.ServerMain" />
<module name="Coeur Morny Cono" /> <module name="Coeur Morny Cono" />
<option name="PROGRAM_PARAMETERS" value="--quiet --debug -t --username sukazyo_deving_bot --master 793274677 --trusted-chat -1 --auto-cmd --outdated-block --trusted-reader-dinner 1040613596 --report-to 793274677 -medc 793274677 -medt 0,2,18,19 -medtz 8 --dinner-chat -1001670950261" /> <option name="PROGRAM_PARAMETERS" value="--quiet --debug -t --username sukazyo_deving_bot --master 793274677 --trusted-chat -1 --auto-cmd --outdated-ignore --trusted-reader-dinner 1040613596 --report-to 793274677 -medc 793274677 -medt 0,2,18,19 -medtz 8 --dinner-chat -1001670950261" />
<option name="WORKING_DIRECTORY" value="$ProjectFileDir$/run" /> <option name="WORKING_DIRECTORY" value="$ProjectFileDir$/run" />
<extension name="coverage"> <extension name="coverage">
<pattern> <pattern>

View File

@ -5,7 +5,7 @@
</option> </option>
<option name="MAIN_CLASS_NAME" value="cc.sukazyo.cono.morny.core.ServerMain" /> <option name="MAIN_CLASS_NAME" value="cc.sukazyo.cono.morny.core.ServerMain" />
<module name="Coeur Morny Cono" /> <module name="Coeur Morny Cono" />
<option name="PROGRAM_PARAMETERS" value="--quiet --debug --username sukazyo_deving_bot --master 793274677 --trusted-chat -1 --outdated-block --trusted-reader-dinner 1040613596 -medc 793274677 -medt 0,2,18,19 -medtz 8 --dinner-chat -1001670950261" /> <option name="PROGRAM_PARAMETERS" value="--quiet --debug --username sukazyo_deving_bot --master 793274677 --trusted-chat -1 --outdated-ignore --trusted-reader-dinner 1040613596 -medc 793274677 -medtz 8 --dinner-chat -1001670950261" />
<option name="VM_PARAMETERS" value="-Djava.net.useSystemProxies=true" /> <option name="VM_PARAMETERS" value="-Djava.net.useSystemProxies=true" />
<option name="WORKING_DIRECTORY" value="$ProjectFileDir$/run" /> <option name="WORKING_DIRECTORY" value="$ProjectFileDir$/run" />
<extension name="coverage"> <extension name="coverage">

View File

@ -5,7 +5,7 @@
</option> </option>
<option name="MAIN_CLASS_NAME" value="cc.sukazyo.cono.morny.core.ServerMain" /> <option name="MAIN_CLASS_NAME" value="cc.sukazyo.cono.morny.core.ServerMain" />
<module name="Coeur Morny Cono" /> <module name="Coeur Morny Cono" />
<option name="PROGRAM_PARAMETERS" value="--quiet --debug --username sukazyo_deving_bot --master 793274677 --trusted-chat -1 --auto-cmd --outdated-block --trusted-reader-dinner 1040613596 --report-to 793274677 -medc 793274677 -medt 0,2,18,19 -medtz 8 --dinner-chat -1001670950261" /> <option name="PROGRAM_PARAMETERS" value="--quiet --debug --username sukazyo_deving_bot --master 793274677 --trusted-chat -1 --auto-cmd --outdated-ignore --trusted-reader-dinner 1040613596 --report-to 793274677 -medc 793274677 -medt 0,2,18,19 -medtz 8 --dinner-chat -1001670950261" />
<option name="WORKING_DIRECTORY" value="$ProjectFileDir$/run" /> <option name="WORKING_DIRECTORY" value="$ProjectFileDir$/run" />
<extension name="coverage"> <extension name="coverage">
<pattern> <pattern>
@ -17,4 +17,4 @@
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
</method> </method>
</configuration> </configuration>
</component> </component>

View File

@ -187,7 +187,7 @@ class MornyCoeur (modules: List[MornyModule])(using val config: MornyConfig)(tes
private var _httpServerContext: MornyHttpServerContext = MornyHttpServerContextImpl() private var _httpServerContext: MornyHttpServerContext = MornyHttpServerContextImpl()
// Coeur Initializing Pre Event // Coeur Initializing Pre Event
modules.foreach(it => it.onInitializingPre(OnInitializingPreContext( tryCallModulesEvent("onInitializingPre", _.onInitializingPre(OnInitializingPreContext(
externalContext, externalContext,
coeurStartTimestamp, account, username, userid, tasks, trusted, coeurStartTimestamp, account, username, userid, tasks, trusted,
eventManager, commands, queries, eventManager, commands, queries,
@ -238,7 +238,7 @@ class MornyCoeur (modules: List[MornyModule])(using val config: MornyConfig)(tes
} }
// Coeur Initializing Event // Coeur Initializing Event
modules.foreach(it => it.onInitializing(OnInitializingContext( tryCallModulesEvent("onInitializing", _.onInitializing(OnInitializingContext(
externalContext, externalContext,
coeurStartTimestamp, account, username, userid, tasks, trusted, coeurStartTimestamp, account, username, userid, tasks, trusted,
eventManager, commands, queries, eventManager, commands, queries,
@ -255,7 +255,7 @@ class MornyCoeur (modules: List[MornyModule])(using val config: MornyConfig)(tes
initializeContext / this << watchDog initializeContext / this << watchDog
// Coeur Initializing Post Event // Coeur Initializing Post Event
modules.foreach(it => it.onInitializingPost(OnInitializingPostContext( tryCallModulesEvent("onInitializingPost", _.onInitializingPost(OnInitializingPostContext(
externalContext, externalContext,
coeurStartTimestamp, account, username, userid, tasks, trusted, coeurStartTimestamp, account, username, userid, tasks, trusted,
eventManager, commands, queries, eventManager, commands, queries,
@ -273,7 +273,7 @@ class MornyCoeur (modules: List[MornyModule])(using val config: MornyConfig)(tes
configure_exitCleanup() configure_exitCleanup()
// Coeur Starting Event // Coeur Starting Event
modules.foreach(it => it.onStarting(OnStartingContext( tryCallModulesEvent("onStarting", _.onStarting(OnStartingContext(
initializeContext))) initializeContext)))
logger `info` "start http server" logger `info` "start http server"
@ -328,7 +328,7 @@ class MornyCoeur (modules: List[MornyModule])(using val config: MornyConfig)(tes
}) })
// Coeur Starting Post Event // Coeur Starting Post Event
modules.foreach(it => it.onStartingPost(OnStartingPostContext( tryCallModulesEvent("onStartingPost", _.onStartingPost(OnStartingPostContext(
initializeContext))) initializeContext)))
if config.commandLoginRefresh then if config.commandLoginRefresh then
@ -381,7 +381,7 @@ class MornyCoeur (modules: List[MornyModule])(using val config: MornyConfig)(tes
} }
def saveDataAll(): Unit = { def saveDataAll(): Unit = {
modules.foreach(it => it.onRoutineSavingData) tryCallModulesEvent("onRoutineSaveData", _.onRoutineSavingData)
logger `notice` "done all save action." logger `notice` "done all save action."
} }
@ -394,7 +394,7 @@ class MornyCoeur (modules: List[MornyModule])(using val config: MornyConfig)(tes
private def exitCleanup (): Unit = { private def exitCleanup (): Unit = {
// Morny Exiting // Morny Exiting
modules.foreach(it => it.onExiting) tryCallModulesEvent("onExiting", _.onExiting)
account.removeGetUpdatesListener() account.removeGetUpdatesListener()
logger `info` "stopped bot update listener" logger `info` "stopped bot update listener"
@ -404,12 +404,12 @@ class MornyCoeur (modules: List[MornyModule])(using val config: MornyConfig)(tes
// Morny Exiting Post // Morny Exiting Post
if config.commandLogoutClear then if config.commandLogoutClear then
commands.automaticTGListRemove() commands.automaticTGListRemove()
modules.foreach(it => it.onExitingPost) tryCallModulesEvent("onExitingPost", _.onExitingPost)
account.shutdown() account.shutdown()
logger `info` "stopped bot account" logger `info` "stopped bot account"
// Morny Exited // Morny Exited
modules.foreach(it => it.onExited) tryCallModulesEvent("onExited", _.onExited)
logger `info` "done exit cleanup\nMorny will EXIT now" logger `info` "done exit cleanup\nMorny will EXIT now"
} }
@ -424,6 +424,15 @@ class MornyCoeur (modules: List[MornyModule])(using val config: MornyConfig)(tes
private case class LoginResult(account: TelegramBot, username: String, userid: Long) private case class LoginResult(account: TelegramBot, username: String, userid: Long)
private def tryCallModulesEvent (eventName: String, calls: MornyModule=>Unit): Unit =
modules.foreach { mod =>
try { calls(mod) }
catch case e: Exception =>
logger `error`
s"""Morny Coeur Lifecycle Event `$eventName` execution failed on module ${mod.id}
|${e.toLogString}""".stripMargin
}
private def login (skip_login: Boolean = false): Option[LoginResult] = { private def login (skip_login: Boolean = false): Option[LoginResult] = {
val builder = TelegramBot.Builder(config.telegramBotKey) val builder = TelegramBot.Builder(config.telegramBotKey)

View File

@ -32,6 +32,7 @@ class MedicationTimer (using coeur: MornyCoeur) {
private var lastNotify_messageId: Option[Int] = None private var lastNotify_messageId: Option[Int] = None
private var isScheduleTaskRegistered = false
private val scheduleTask: RoutineTask = new RoutineTask { private val scheduleTask: RoutineTask = new RoutineTask {
override def name: String = DAEMON_THREAD_NAME_DEF override def name: String = DAEMON_THREAD_NAME_DEF
@ -59,11 +60,15 @@ class MedicationTimer (using coeur: MornyCoeur) {
logger `notice` "Medication Timer disabled : related param is not complete set" logger `notice` "Medication Timer disabled : related param is not complete set"
return; return;
coeur.tasks ++ scheduleTask coeur.tasks ++ scheduleTask
isScheduleTaskRegistered = true
logger `notice` "Medication Timer started." logger `notice` "Medication Timer started."
def stop(): Unit = def stop(): Unit =
coeur.tasks % scheduleTask if isScheduleTaskRegistered then
logger `notice` "Medication Timer stopped." coeur.tasks % scheduleTask
logger `notice` "Medication Timer stopped."
else
logger `notice` "Medication Timer have not run, skipped stop it."
private def sendNotification(): Unit = { private def sendNotification(): Unit = {
val sendResponse: SendResponse = SendMessage(notify_toChat, NOTIFY_MESSAGE).unsafeExecute val sendResponse: SendResponse = SendMessage(notify_toChat, NOTIFY_MESSAGE).unsafeExecute
@ -82,7 +87,7 @@ class MedicationTimer (using coeur: MornyCoeur) {
notify_toChat, notify_toChat,
edited.messageId, edited.messageId,
edited.text + s"\n-- $editTime --" edited.text + s"\n-- $editTime --"
).entities((entities toArray)*).unsafeExecute ).entities(entities.toArray*).unsafeExecute
lastNotify_messageId = None lastNotify_messageId = None
true true
} }