handle signals for better app shutdown
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
bd3a00c2d9
commit
573765958f
|
@ -2,6 +2,6 @@
|
||||||
Pylokid. From Mail to Lodur - all automated.
|
Pylokid. From Mail to Lodur - all automated.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__version__ = "3.0.2"
|
__version__ = "3.0.3"
|
||||||
__git_version__ = "0"
|
__git_version__ = "0"
|
||||||
__url__ = "https://github.com/tobru/pylokid"
|
__url__ = "https://github.com/tobru/pylokid"
|
||||||
|
|
|
@ -4,7 +4,4 @@ Helper module to run not-installed version (via ``python3 -m pylokid``)
|
||||||
from pylokid.main import main
|
from pylokid.main import main
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
try:
|
main()
|
||||||
main()
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
print("Byebye")
|
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
|
import signal
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from importlib.metadata import version
|
from importlib.metadata import version
|
||||||
|
@ -37,6 +38,20 @@ PUSHOVER_API_TOKEN = os.getenv("PUSHOVER_API_TOKEN")
|
||||||
PUSHOVER_USER_KEY = os.getenv("PUSHOVER_USER_KEY")
|
PUSHOVER_USER_KEY = os.getenv("PUSHOVER_USER_KEY")
|
||||||
|
|
||||||
|
|
||||||
|
class GracefulKiller:
|
||||||
|
kill_now = False
|
||||||
|
signals = {signal.SIGINT: "SIGINT", signal.SIGTERM: "SIGTERM"}
|
||||||
|
|
||||||
|
def __init__(self, logger):
|
||||||
|
signal.signal(signal.SIGINT, self.exit_gracefully)
|
||||||
|
signal.signal(signal.SIGTERM, self.exit_gracefully)
|
||||||
|
self.logger = logger
|
||||||
|
|
||||||
|
def exit_gracefully(self, signum, frame):
|
||||||
|
self.logger.info("Received signal %s", self.signals[signum])
|
||||||
|
self.kill_now = True
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
""" main """
|
""" main """
|
||||||
|
|
||||||
|
@ -48,6 +63,8 @@ def main():
|
||||||
logger = logging.getLogger("pylokid")
|
logger = logging.getLogger("pylokid")
|
||||||
logger.info("Starting pylokid version %s", version("pylokid"))
|
logger.info("Starting pylokid version %s", version("pylokid"))
|
||||||
|
|
||||||
|
killer = GracefulKiller(logger)
|
||||||
|
|
||||||
# Initialize IMAP Session
|
# Initialize IMAP Session
|
||||||
imap_client = EmailHandling(
|
imap_client = EmailHandling(
|
||||||
IMAP_SERVER,
|
IMAP_SERVER,
|
||||||
|
@ -244,6 +261,11 @@ def main():
|
||||||
|
|
||||||
# send heartbeat
|
# send heartbeat
|
||||||
requests.get(HEARTBEAT_URL)
|
requests.get(HEARTBEAT_URL)
|
||||||
# repeat every
|
|
||||||
logger.info("Waiting %s seconds until next check", IMAP_CHECK_INTERVAL)
|
while not killer.kill_now:
|
||||||
time.sleep(int(IMAP_CHECK_INTERVAL))
|
# repeat every
|
||||||
|
logger.info("Waiting %s seconds until next check", IMAP_CHECK_INTERVAL)
|
||||||
|
time.sleep(int(IMAP_CHECK_INTERVAL))
|
||||||
|
|
||||||
|
logger.info("Pylokid waves bye bye")
|
||||||
|
exit()
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[tool.poetry]
|
[tool.poetry]
|
||||||
name = "pylokid"
|
name = "pylokid"
|
||||||
version = "3.0.2"
|
version = "3.0.3"
|
||||||
description = ""
|
description = ""
|
||||||
authors = ["Tobias Brunner <tobias@tobru.ch>"]
|
authors = ["Tobias Brunner <tobias@tobru.ch>"]
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
Loading…
Reference in New Issue