浏览代码

Merge branch 'systemd-integration' of blezz-tech/sharix-open-webservice-running into unstable

+ 3 - 0
handlers/conf/open_handler@.service

@@ -10,5 +10,8 @@ ExecStart=/root/sharix-open-webapp-base/webservice_running/handlers/.venv/bin/py
 Restart=always
 RestartSec=60s
 
+TimeoutStopSec=30
+KillSignal=SIGTERM
+
 [Install]
 WantedBy=multi-user.target

+ 6 - 3
handlers/handlers/open_access_request_pending.py

@@ -2,7 +2,7 @@ import hashlib
 from datetime import datetime
 import config
 import logging
-from EchoBot import JabberBot
+from utils.JabberBot import JabberBot
 from slixmpp.stanza import Message
 import jsonAndRequest as jsreq
 from time import sleep
@@ -10,6 +10,7 @@ from lib import run_process
 from classes.Ticket import Ticket
 from typing import List
 from concurrent.futures import ThreadPoolExecutor
+from utils.SystemdKiller import SystemdKiller
 
 botname = "open_access_request_pending"
 operating_status = 320
@@ -56,7 +57,9 @@ def start_handler():
     tickets: List[Ticket]  = get_fake_data()
 
     with ThreadPoolExecutor() as executor:
-        while True:
+        killer = SystemdKiller()
+
+        while not killer.exit_event.is_set():
             # Параллельная обработка всех тикетов
             processed = list(executor.map(run_process(interval_attempts_unlimited, processing), tickets))
             
@@ -65,7 +68,7 @@ def start_handler():
             
             logging.debug(tickets)
             logging.debug("Sleep")
-            sleep(INTERVAL_TIME)
+            killer.exit_event.wait(timeout=INTERVAL_TIME)
 
 
 # START CUSTOM FUNCTIONS

+ 1 - 1
handlers/handlers/open_basic_bot.py

@@ -2,7 +2,7 @@ import hashlib
 from datetime import datetime
 import config
 import logging
-from EchoBot import JabberBot
+from utils.JabberBot import JabberBot
 from slixmpp.stanza import Message
 
 botname = "open_basic_bot"

+ 2 - 4
handlers/main.py

@@ -1,9 +1,8 @@
 import config
-from EchoBot import JabberBot
+from utils.JabberBot import JabberBot
 import logging
 from argparse import ArgumentParser
 import importlib
-import asyncio
 
 if __name__ == '__main__':
     # Setup the command line arguments.
@@ -54,8 +53,7 @@ if __name__ == '__main__':
         # Ctrl+C, Ctrl+Z
         # systemctl stop с последующей отправкой кодов и корректной остановкой программы
 
-        # bot.stop()
-        asyncio.get_event_loop().run_forever()
+        bot.stop()
     else:
         print("Select using -n flag for select availible bot from botlist:\n")
         for botname in config.BOTLIST:

+ 0 - 0
handlers/EchoBot.py → handlers/utils/JabberBot.py


+ 11 - 0
handlers/utils/SystemdKiller.py

@@ -0,0 +1,11 @@
+import signal
+import threading
+
+class SystemdKiller:
+    def __init__(self):
+        self.exit_event = threading.Event()
+        signal.signal(signal.SIGINT, self.exit_handler)
+        signal.signal(signal.SIGTERM, self.exit_handler)
+
+    def exit_handler(self, signum, frame):
+        self.exit_event.set()  # Устанавливаем флаг прерывания