GithubHelp home page GithubHelp logo

mqtt-recorder's People

Contributors

jschlyter avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mqtt-recorder's Issues

Graceful SIGTERM handling

Tried running script from bash, killing it after the session was over but found empty files.

Copied some of the code from here and while I git it working, gracefull is not the word i'd use.; but this may help others who try something similar

https://stackoverflow.com/questions/23313720/asyncio-how-can-coroutines-be-used-in-signal-handlers

stil ugly, but slightly better:

$ git diff
diff --git a/mqtt_recorder.py b/mqtt_recorder.py
index 0d30b26..8945456 100644
--- a/mqtt_recorder.py
+++ b/mqtt_recorder.py
@@ -8,13 +8,13 @@ import sys
 import base64
 import time
 from hbmqtt.client import MQTTClient, QOS_0, QOS_1
-
+import signal
+from concurrent.futures import CancelledError

 TOPICS = [("#", QOS_1)]

 logger = logging.getLogger('mqtt_recorder')

-
 async def mqtt_record(server: str, output: str = None) -> None:
     """Record MQTT messages"""
     mqtt = MQTTClient()
@@ -36,6 +36,9 @@ async def mqtt_record(server: str, output: str = None) -> None:
         print(json.dumps(record), file=output_file)


+
+
+
 async def mqtt_replay(server: str, input: str = None, delay: int = 0, realtime: bool = False, scale: float = 1) -> None:
     """Replay MQTT messages"""
     mqtt = MQTTClient()
@@ -73,6 +76,10 @@ async def mqtt_replay(server: str, input: str = None, delay: int = 0, realtime:
             await asyncio.sleep(delay_s)


+async def shutdown(sig, loop):
+    loop.stop()
+
+
 def main():
     """ Main function"""
     parser = argparse.ArgumentParser(description='MQTT recorder')
@@ -124,13 +131,22 @@ def main():
     else:
         logging.basicConfig(level=logging.INFO)

+
     if args.mode == 'replay':
         process = mqtt_replay(server=args.server, input=args.input, delay=args.delay, realtime=args.realtime, scale=1/args.speed)
     else:
         process = mqtt_record(server=args.server, output=args.output)
+    for signame in (signal.SIGINT, signal.SIGTERM):
+        asyncio.get_event_loop().add_signal_handler(signame,
+                            lambda: asyncio.ensure_future(shutdown(signame, asyncio.get_event_loop())))
+
+    try:
+        asyncio.get_event_loop().run_until_complete(process)
+    except:
+        pass

-    asyncio.get_event_loop().run_until_complete(process)


 if __name__ == "__main__":
     main()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.