Przeglądaj źródła

Reexecution, if request while running

master
Dirk Alders 2 lat temu
rodzic
commit
299917736d
3 zmienionych plików z 23 dodań i 32 usunięć
  1. 0
    3
      .gitmodules
  2. 23
    28
      exec_command.py
  3. 0
    1
      task

+ 0
- 3
.gitmodules Wyświetl plik

4
 [submodule "report"]
4
 [submodule "report"]
5
 	path = report
5
 	path = report
6
 	url = https://git.mount-mockery.de/pylib/report.git
6
 	url = https://git.mount-mockery.de/pylib/report.git
7
-[submodule "task"]
8
-	path = task
9
-	url = https://git.mount-mockery.de/pylib/task.git

+ 23
- 28
exec_command.py Wyświetl plik

2
 import json
2
 import json
3
 import logging
3
 import logging
4
 import mqtt
4
 import mqtt
5
-import os
6
 import report
5
 import report
7
-import task
6
+import subprocess
8
 import time
7
 import time
9
 
8
 
10
 try:
9
 try:
14
 logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
13
 logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
15
 
14
 
16
 
15
 
17
-class exec_command(mqtt.mqtt_client, task.threaded_queue):
16
+class exec_command(mqtt.mqtt_client):
18
     def __init__(self):
17
     def __init__(self):
19
         self.__block_execution__ = False
18
         self.__block_execution__ = False
20
-        task.threaded_queue.__init__(self)
21
         mqtt.mqtt_client.__init__(self, config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
19
         mqtt.mqtt_client.__init__(self, config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
22
         self.add_callback(config.TOPIC, self.mqtt_rx)
20
         self.add_callback(config.TOPIC, self.mqtt_rx)
21
+        # Start a pseudo process
22
+        self.process = subprocess.Popen(["sleep", "0"])
23
 
23
 
24
-    def exec_command(self, rt):
25
-        os.system(config.COMMAND)
26
-        self.__block_execution__ = False
27
-    
24
+    def exec_command(self):
25
+        self.process = subprocess.Popen(config.COMMAND.split(" "))
28
 
26
 
29
     def mqtt_rx(self, client, userdate, message):
27
     def mqtt_rx(self, client, userdate, message):
30
-        if not self.__block_execution__:
31
-            data = None
32
-            if config.PAYLOAD_KEY is None:
33
-                try:
34
-                    data = message.payload.decode('utf-8')
35
-                except:
36
-                    logger.exception("Error decoding mqtt message")
37
-            else:
38
-                try:
39
-                    payload = json.loads(message.payload)
40
-                except:
41
-                    logger.exception("Error decoding json mqtt message")
42
-                else:
43
-                    data = payload.get(config.PAYLOAD_KEY)
44
-            if config.PAYLOAD_DATA is None or data == config.PAYLOAD_DATA:
45
-                logger.debug("Starting execution in background...")
46
-                self.__block_execution__ = True
47
-                self.enqueue(5, self.exec_command)
28
+        data = None
29
+        if config.PAYLOAD_KEY is None:
30
+            try:
31
+                data = message.payload.decode('utf-8')
32
+            except:
33
+                logger.exception("Error decoding mqtt message")
48
         else:
34
         else:
49
-            logger.debug("Execution in progress. Ignoring request...")
35
+            try:
36
+                payload = json.loads(message.payload)
37
+            except:
38
+                logger.exception("Error decoding json mqtt message")
39
+            else:
40
+                data = payload.get(config.PAYLOAD_KEY)
41
+        if config.PAYLOAD_DATA is None or data == config.PAYLOAD_DATA:
42
+            if self.process.poll() is None:
43
+                self.process.kill()
44
+            logger.debug("Starting execution in background...")
45
+            self.exec_command()
50
 
46
 
51
 
47
 
52
 if __name__ == '__main__': 
48
 if __name__ == '__main__': 
53
     report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
49
     report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
54
     #
50
     #
55
     ec = exec_command()
51
     ec = exec_command()
56
-    ec.run()
57
     #
52
     #
58
     while True:
53
     while True:
59
         time.sleep(30)
54
         time.sleep(30)

+ 0
- 1
task

1
-Subproject commit 7583bb5f3bd2420c901374ba95b678af6ce88433

Ładowanie…
Anuluj
Zapisz