improve event loop to end gracefully
This commit is contained in:
parent
c41810994a
commit
29cba6e079
16
main.py
16
main.py
@ -1,6 +1,7 @@
|
|||||||
import logging
|
import logging
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import signal
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
@ -54,6 +55,10 @@ class App:
|
|||||||
|
|
||||||
self.mods = {}
|
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):
|
def authenticate(self):
|
||||||
if os.path.exists("auth.json"):
|
if os.path.exists("auth.json"):
|
||||||
with open("auth.json", "r") as f:
|
with open("auth.json", "r") as f:
|
||||||
@ -92,7 +97,7 @@ class App:
|
|||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.install("elicense", mods.elicense)
|
self.install("elicense", mods.elicense)
|
||||||
while True:
|
while self.alive:
|
||||||
try:
|
try:
|
||||||
self.cycle()
|
self.cycle()
|
||||||
self.logging.debug("cycle done")
|
self.logging.debug("cycle done")
|
||||||
@ -101,7 +106,10 @@ class App:
|
|||||||
|
|
||||||
for mod in self.mods:
|
for mod in self.mods:
|
||||||
self.mods[mod].cycle()
|
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):
|
def cycle(self):
|
||||||
items = self.M.notifications(
|
items = self.M.notifications(
|
||||||
@ -117,6 +125,10 @@ class App:
|
|||||||
|
|
||||||
# TODO: call mod cycles
|
# TODO: call mod cycles
|
||||||
|
|
||||||
|
def exit(self):
|
||||||
|
self.alive = False
|
||||||
|
self.logging.info("exit requested")
|
||||||
|
|
||||||
def reply(self, st, msg):
|
def reply(self, st, msg):
|
||||||
try:
|
try:
|
||||||
self.logging.debug("reply:", msg)
|
self.logging.debug("reply:", msg)
|
||||||
|
Loading…
Reference in New Issue
Block a user