Reexecution, if request while running

This commit is contained in:
Dirk Alders 2022-09-22 10:19:04 +01:00
parent 79b444272f
commit 299917736d
3 changed files with 23 additions and 32 deletions

3
.gitmodules vendored
View File

@ -4,6 +4,3 @@
[submodule "report"] [submodule "report"]
path = report path = report
url = https://git.mount-mockery.de/pylib/report.git url = https://git.mount-mockery.de/pylib/report.git
[submodule "task"]
path = task
url = https://git.mount-mockery.de/pylib/task.git

View File

@ -2,9 +2,8 @@ import config
import json import json
import logging import logging
import mqtt import mqtt
import os
import report import report
import task import subprocess
import time import time
try: try:
@ -14,46 +13,42 @@ except ImportError:
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main') logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
class exec_command(mqtt.mqtt_client, task.threaded_queue): class exec_command(mqtt.mqtt_client):
def __init__(self): def __init__(self):
self.__block_execution__ = False self.__block_execution__ = False
task.threaded_queue.__init__(self)
mqtt.mqtt_client.__init__(self, config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS) mqtt.mqtt_client.__init__(self, config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
self.add_callback(config.TOPIC, self.mqtt_rx) self.add_callback(config.TOPIC, self.mqtt_rx)
# Start a pseudo process
self.process = subprocess.Popen(["sleep", "0"])
def exec_command(self, rt): def exec_command(self):
os.system(config.COMMAND) self.process = subprocess.Popen(config.COMMAND.split(" "))
self.__block_execution__ = False
def mqtt_rx(self, client, userdate, message): def mqtt_rx(self, client, userdate, message):
if not self.__block_execution__: data = None
data = None if config.PAYLOAD_KEY is None:
if config.PAYLOAD_KEY is None: try:
try: data = message.payload.decode('utf-8')
data = message.payload.decode('utf-8') except:
except: logger.exception("Error decoding mqtt message")
logger.exception("Error decoding mqtt message")
else:
try:
payload = json.loads(message.payload)
except:
logger.exception("Error decoding json mqtt message")
else:
data = payload.get(config.PAYLOAD_KEY)
if config.PAYLOAD_DATA is None or data == config.PAYLOAD_DATA:
logger.debug("Starting execution in background...")
self.__block_execution__ = True
self.enqueue(5, self.exec_command)
else: else:
logger.debug("Execution in progress. Ignoring request...") try:
payload = json.loads(message.payload)
except:
logger.exception("Error decoding json mqtt message")
else:
data = payload.get(config.PAYLOAD_KEY)
if config.PAYLOAD_DATA is None or data == config.PAYLOAD_DATA:
if self.process.poll() is None:
self.process.kill()
logger.debug("Starting execution in background...")
self.exec_command()
if __name__ == '__main__': if __name__ == '__main__':
report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT) report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
# #
ec = exec_command() ec = exec_command()
ec.run()
# #
while True: while True:
time.sleep(30) time.sleep(30)

1
task

@ -1 +0,0 @@
Subproject commit 7583bb5f3bd2420c901374ba95b678af6ce88433