Versie 0.2, met ondersteuning om het uitzetten te signaleren

This commit is contained in:
Chris Josten 2020-05-20 16:54:25 +02:00
parent d550b4944f
commit 5273c53852
2 changed files with 25 additions and 4 deletions

View file

@ -4,6 +4,7 @@ import argparse
import asyncio
import logging
import os
import signal
import subprocess
import time
@ -13,6 +14,7 @@ TIMEOUT: float = 30 * 60
last_checked: float = time.time()
last_checked_value: bool = False
cur_timeout: float = TIMEOUT
client = None
def count_users() -> int:
@ -51,11 +53,16 @@ def trusty_sleep(amount: float):
LOGGER.debug("Woke up early")
time.sleep(amount - (time.time() - last_checked))
async def sigint_handler(*_, **__):
LOGGER.debug("Notifying of shutdown")
await client.notify("NotifyShutdown")
async def main() -> None:
global TIMEOUT
global CLIENT_IP
global last_checked
global last_checked_value
global client
logging.basicConfig(format='[%(asctime)s %(levelname)s] %(name)s: %(message)s', level=logging.DEBUG)
argparser = argparse.ArgumentParser(description="AIUO - Automatisch In- en Uitschakelen Ober (Oberkant)")
@ -77,6 +84,10 @@ async def main() -> None:
await asyncio.sleep(10)
LOGGER.debug(await client.call("GetBootReason"))
# Alleen de klant op de hoogte stellen nadat we hebben laten weten dat we er zijn.
LOGGER.debug("SIGINT verwerken")
loop = asyncio.get_running_loop()
#loop.add_signal_handler(signal.SIGINT, lambda: asyncio.ensure_future(sigint_handler()))
final_verdict = False
@ -92,12 +103,22 @@ async def main() -> None:
last_checked_value = True
else:
last_checked_value = False
await sigint_handler()
os.system("shutdown +1 The system is going to shut down because no active sessions were found and no one is on the server")
def start_main():
asyncio.run(main())
#asyncio.run(main())
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(main())
except KeyboardInterrupt as e:
LOGGER.debug("Keyboard interrupt")
finally:
LOGGER.debug("Notify shutdown")
loop.run_forever(sigint_handler())
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()
if __name__ == "__main__":
start_main()

View file

@ -2,7 +2,7 @@
from setuptools import setup
setup(name="AIUO-ober",
version="0.1",
version="0.2",
description="Automatisch In- en Uitschakelen Ober Ober",
author="Chris Josten",
author_email="chris@netsoj.nl",