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 name="MAIN_CLASS_NAME" value="cc.sukazyo.cono.morny.core.ServerMain" />
<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" />
<extension name="coverage">
<pattern>

View File

@ -5,7 +5,7 @@
</option>
<option name="MAIN_CLASS_NAME" value="cc.sukazyo.cono.morny.core.ServerMain" />
<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="WORKING_DIRECTORY" value="$ProjectFileDir$/run" />
<extension name="coverage">

View File

@ -5,7 +5,7 @@
</option>
<option name="MAIN_CLASS_NAME" value="cc.sukazyo.cono.morny.core.ServerMain" />
<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" />
<extension name="coverage">
<pattern>

View File

@ -187,7 +187,7 @@ class MornyCoeur (modules: List[MornyModule])(using val config: MornyConfig)(tes
private var _httpServerContext: MornyHttpServerContext = MornyHttpServerContextImpl()
// Coeur Initializing Pre Event
modules.foreach(it => it.onInitializingPre(OnInitializingPreContext(
tryCallModulesEvent("onInitializingPre", _.onInitializingPre(OnInitializingPreContext(
externalContext,
coeurStartTimestamp, account, username, userid, tasks, trusted,
eventManager, commands, queries,
@ -238,7 +238,7 @@ class MornyCoeur (modules: List[MornyModule])(using val config: MornyConfig)(tes
}
// Coeur Initializing Event
modules.foreach(it => it.onInitializing(OnInitializingContext(
tryCallModulesEvent("onInitializing", _.onInitializing(OnInitializingContext(
externalContext,
coeurStartTimestamp, account, username, userid, tasks, trusted,
eventManager, commands, queries,
@ -255,7 +255,7 @@ class MornyCoeur (modules: List[MornyModule])(using val config: MornyConfig)(tes
initializeContext / this << watchDog
// Coeur Initializing Post Event
modules.foreach(it => it.onInitializingPost(OnInitializingPostContext(
tryCallModulesEvent("onInitializingPost", _.onInitializingPost(OnInitializingPostContext(
externalContext,
coeurStartTimestamp, account, username, userid, tasks, trusted,
eventManager, commands, queries,
@ -273,7 +273,7 @@ class MornyCoeur (modules: List[MornyModule])(using val config: MornyConfig)(tes
configure_exitCleanup()
// Coeur Starting Event
modules.foreach(it => it.onStarting(OnStartingContext(
tryCallModulesEvent("onStarting", _.onStarting(OnStartingContext(
initializeContext)))
logger `info` "start http server"
@ -328,7 +328,7 @@ class MornyCoeur (modules: List[MornyModule])(using val config: MornyConfig)(tes
})
// Coeur Starting Post Event
modules.foreach(it => it.onStartingPost(OnStartingPostContext(
tryCallModulesEvent("onStartingPost", _.onStartingPost(OnStartingPostContext(
initializeContext)))
if config.commandLoginRefresh then
@ -381,7 +381,7 @@ class MornyCoeur (modules: List[MornyModule])(using val config: MornyConfig)(tes
}
def saveDataAll(): Unit = {
modules.foreach(it => it.onRoutineSavingData)
tryCallModulesEvent("onRoutineSaveData", _.onRoutineSavingData)
logger `notice` "done all save action."
}
@ -394,7 +394,7 @@ class MornyCoeur (modules: List[MornyModule])(using val config: MornyConfig)(tes
private def exitCleanup (): Unit = {
// Morny Exiting
modules.foreach(it => it.onExiting)
tryCallModulesEvent("onExiting", _.onExiting)
account.removeGetUpdatesListener()
logger `info` "stopped bot update listener"
@ -404,12 +404,12 @@ class MornyCoeur (modules: List[MornyModule])(using val config: MornyConfig)(tes
// Morny Exiting Post
if config.commandLogoutClear then
commands.automaticTGListRemove()
modules.foreach(it => it.onExitingPost)
tryCallModulesEvent("onExitingPost", _.onExitingPost)
account.shutdown()
logger `info` "stopped bot account"
// Morny Exited
modules.foreach(it => it.onExited)
tryCallModulesEvent("onExited", _.onExited)
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 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] = {
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 isScheduleTaskRegistered = false
private val scheduleTask: RoutineTask = new RoutineTask {
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"
return;
coeur.tasks ++ scheduleTask
isScheduleTaskRegistered = true
logger `notice` "Medication Timer started."
def stop(): Unit =
if isScheduleTaskRegistered then
coeur.tasks % scheduleTask
logger `notice` "Medication Timer stopped."
else
logger `notice` "Medication Timer have not run, skipped stop it."
private def sendNotification(): Unit = {
val sendResponse: SendResponse = SendMessage(notify_toChat, NOTIFY_MESSAGE).unsafeExecute
@ -82,7 +87,7 @@ class MedicationTimer (using coeur: MornyCoeur) {
notify_toChat,
edited.messageId,
edited.text + s"\n-- $editTime --"
).entities((entities toArray)*).unsafeExecute
).entities(entities.toArray*).unsafeExecute
lastNotify_messageId = None
true
}