]> git.treefish.org Git - mtxbot.git/blobdiff - src/mtxbot.py
Fix presence shutdown
[mtxbot.git] / src / mtxbot.py
index 0843f7d2184439e5e2a42df31bdff46ed7da4ed2..011341f85b7204a6674a886112729c1da503d16f 100755 (executable)
@@ -16,10 +16,10 @@ assert sys.version_info >= (3, 5)
 from presence import Presence
 
 def handleInterrupt(sig, frame):
 from presence import Presence
 
 def handleInterrupt(sig, frame):
-    global stopped
+    global stop
     if os.getpid() == mainPid:
         logging.info( "Got stop signal." )
     if os.getpid() == mainPid:
         logging.info( "Got stop signal." )
-        stopped = True
+        stop = True
         for presence in presences:
             presence.stop()
 
         for presence in presences:
             presence.stop()
 
@@ -36,6 +36,9 @@ async def main():
         done, pending = await asyncio.wait( presence_tasks,
                                             return_when = asyncio.FIRST_EXCEPTION )
 
         done, pending = await asyncio.wait( presence_tasks,
                                             return_when = asyncio.FIRST_EXCEPTION )
 
+        for presence in presences:
+            presence.stop()
+
         for task in done:
             try:
                 task.result()
         for task in done:
             try:
                 task.result()
@@ -43,6 +46,7 @@ async def main():
                 logging.error( "Error running task: %s" % str(e) )
 
     finally:
                 logging.error( "Error running task: %s" % str(e) )
 
     finally:
+        logging.info("Closing client...")
         await client.close()
 
 logging.basicConfig(format='[%(asctime)s] %(levelname)s: %(message)s',
         await client.close()
 
 logging.basicConfig(format='[%(asctime)s] %(levelname)s: %(message)s',
@@ -51,8 +55,9 @@ logging.basicConfig(format='[%(asctime)s] %(levelname)s: %(message)s',
 
 mainPid = os.getpid()
 signal.signal(signal.SIGINT, handleInterrupt)
 
 mainPid = os.getpid()
 signal.signal(signal.SIGINT, handleInterrupt)
+signal.signal(signal.SIGTERM, handleInterrupt)
 
 
-stopped = False
+stop = False
 
 if len(sys.argv) != 2:
     print("Usage: %s <config json>" % sys.argv[0])
 
 if len(sys.argv) != 2:
     print("Usage: %s <config json>" % sys.argv[0])
@@ -61,18 +66,20 @@ if len(sys.argv) != 2:
 with open(sys.argv[1]) as configFile:
     config = json.load(configFile)
 
 with open(sys.argv[1]) as configFile:
     config = json.load(configFile)
 
-while not stopped:
+fifo_dir = os.getenv('MTXBOT_FIFO_DIR', '/run/mtxbot')
+
+while not stop:
     presences = []
     presences = []
-    for entry in os.listdir(config['paths']['fifodir']):
-        if stat.S_ISFIFO(os.stat(entry).st_mode):
+    for entry in os.listdir(fifo_dir):
+        fullpath = "%s/%s" % (fifo_dir, entry)
+        if stat.S_ISFIFO(os.stat(fullpath).st_mode):
             logging.info("Creating presence for %s..." % entry)
             logging.info("Creating presence for %s..." % entry)
-            presences.append( Presence( entry, "%s/%s" %
-                                        (config['paths']['fifodir'], entry) ) )
+            presences.append( Presence(entry, fullpath) )
     if len(presences) == 0:
         logging.error("No fifos could be found!")
         break
     asyncio.run(main())
     if len(presences) == 0:
         logging.error("No fifos could be found!")
         break
     asyncio.run(main())
-    if not stopped:
+    if not stop:
         logging.warning("Main loop exited!")
         logging.info("Restarting after grace period...")
         time.sleep(3.0)
         logging.warning("Main loop exited!")
         logging.info("Restarting after grace period...")
         time.sleep(3.0)