improve event loop to end gracefully

This commit is contained in:
falsycat 2023-02-12 02:14:45 +09:00
parent c41810994a
commit 29cba6e079

16
main.py
View File

@ -1,6 +1,7 @@
import logging
import json
import os
import signal
import time
from datetime import datetime, timezone
@ -54,6 +55,10 @@ class App:
self.mods = {}
self.alive = True
signal.signal(signal.SIGTERM, lambda a,b: self.exit())
signal.signal(signal.SIGINT, lambda a,b: self.exit())
def authenticate(self):
if os.path.exists("auth.json"):
with open("auth.json", "r") as f:
@ -92,7 +97,7 @@ class App:
def run(self):
self.install("elicense", mods.elicense)
while True:
while self.alive:
try:
self.cycle()
self.logging.debug("cycle done")
@ -101,7 +106,10 @@ class App:
for mod in self.mods:
self.mods[mod].cycle()
time.sleep(self.CONFIG["interval"])
for i in range(self.CONFIG["interval"]):
if not self.alive: break
time.sleep(1)
def cycle(self):
items = self.M.notifications(
@ -117,6 +125,10 @@ class App:
# TODO: call mod cycles
def exit(self):
self.alive = False
self.logging.info("exit requested")
def reply(self, st, msg):
try:
self.logging.debug("reply:", msg)