Created
October 16, 2015 13:07
-
-
Save sjmf/b250a101cff14520283b to your computer and use it in GitHub Desktop.
Stomp logger using lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from stompy.simple import Client | |
from flask import Flask | |
app = Flask(__name__) | |
lock = Lock() | |
@app.route("/") | |
def route(): | |
yield from lock | |
try: | |
with open(LOGFILE, 'r') as f: | |
return f.read() | |
finally: | |
lock.release() | |
# stomp_creds connection credentials | |
stomp_creds = { | |
"host" : "sjmf.in", | |
"port" : 61613, | |
"queue": "/topic/fridgeprint", | |
"user" : "fridgeprint", | |
"pass" : "Where's my fucking Milk?" | |
} | |
LOGFILE = "./fridgeprint_msgs.log" | |
conn = Client(host=stomp_creds['host'], port=stomp_creds['port']) | |
def main(): | |
conn.connect(username=stomp_creds['user'], password=stomp_creds['pass']) | |
conn.subscribe(stomp_creds['queue']) | |
msg = conn.get() | |
while msg: | |
yield from lock | |
try: | |
with open(LOGFILE, 'a+') as f: | |
f.write(str(json.loads(msg.body)) + "\n") | |
finally: | |
lock.release() | |
msg = conn.get() | |
if __name__ == "__main__": | |
try: | |
app.run() | |
main() | |
except KeyboardInterrupt: | |
conn.disconnect() | |
print "Exiting :)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment