|
| 1 | +import uwsgi |
| 2 | +import traceback |
| 3 | +import oxenmq |
| 4 | +from oxenc import bt_deserialize |
| 5 | +import time |
| 6 | +from datetime import timedelta |
| 7 | +import functools |
| 8 | + |
| 9 | +from .web import app |
| 10 | +from . import cleanup |
| 11 | +from . import config |
| 12 | +from . import crypto |
| 13 | +from .omq import omq |
| 14 | + |
| 15 | +# This is the uwsgi "mule" that handles things not related to serving HTTP requests: |
| 16 | +# - it holds the oxenmq instance (with its own interface into sogs) |
| 17 | +# - it handles cleanup jobs (e.g. periodic deletions) |
| 18 | + |
| 19 | + |
| 20 | +def run(): |
| 21 | + try: |
| 22 | + app.logger.info("OxenMQ mule started.") |
| 23 | + |
| 24 | + while True: |
| 25 | + time.sleep(1) |
| 26 | + |
| 27 | + except Exception: |
| 28 | + app.logger.error("mule died via exception:\n{}".format(traceback.format_exc())) |
| 29 | + |
| 30 | + |
| 31 | +def allow_conn(addr, pk, sn): |
| 32 | + # TODO: user recognition auth |
| 33 | + return oxenmq.AuthLevel.basic |
| 34 | + |
| 35 | + |
| 36 | +def admin_conn(addr, pk, sn): |
| 37 | + return oxenmq.AuthLevel.admin |
| 38 | + |
| 39 | + |
| 40 | +def inproc_fail(connid, reason): |
| 41 | + raise RuntimeError(f"Couldn't connect mule to itself: {reason}") |
| 42 | + |
| 43 | + |
| 44 | +def setup_omq(): |
| 45 | + global omq, mule_conn |
| 46 | + |
| 47 | + app.logger.debug("Mule setting up omq") |
| 48 | + if isinstance(config.OMQ_LISTEN, list): |
| 49 | + listen = config.OMQ_LISTEN |
| 50 | + elif config.OMQ_LISTEN is None: |
| 51 | + listen = [] |
| 52 | + else: |
| 53 | + listen = [config.OMQ_LISTEN] |
| 54 | + for addr in listen: |
| 55 | + omq.listen(addr, curve=True, allow_connection=allow_conn) |
| 56 | + app.logger.info(f"OxenMQ listening on {addr}") |
| 57 | + |
| 58 | + # Internal socket for workers to talk to us: |
| 59 | + omq.listen(config.OMQ_INTERNAL, curve=False, allow_connection=admin_conn) |
| 60 | + |
| 61 | + # Periodic database cleanup timer: |
| 62 | + omq.add_timer(cleanup.cleanup, timedelta(seconds=cleanup.INTERVAL)) |
| 63 | + |
| 64 | + # Commands other workers can send to us, e.g. for notifications of activity for us to know about |
| 65 | + worker = omq.add_category("worker", access_level=oxenmq.AuthLevel.admin) |
| 66 | + worker.add_command("message_posted", message_posted) |
| 67 | + worker.add_command("messages_deleted", messages_deleted) |
| 68 | + worker.add_command("message_edited", message_edited) |
| 69 | + |
| 70 | + app.logger.debug("Mule starting omq") |
| 71 | + omq.start() |
| 72 | + |
| 73 | + # Connect mule to itself so that if something the mule does wants to send something to the mule |
| 74 | + # it will work. (And so be careful not to recurse!) |
| 75 | + app.logger.debug("Mule connecting to self") |
| 76 | + mule_conn = omq.connect_inproc(on_success=None, on_failure=inproc_fail) |
| 77 | + |
| 78 | + |
| 79 | +def log_exceptions(f): |
| 80 | + @functools.wraps(f) |
| 81 | + def wrapper(*args, **kwargs): |
| 82 | + try: |
| 83 | + return f(*args, **kwargs) |
| 84 | + except Exception as e: |
| 85 | + app.logger.error(f"{f.__name__} raised exception: {e}") |
| 86 | + raise |
| 87 | + |
| 88 | + return wrapper |
| 89 | + |
| 90 | + |
| 91 | +@log_exceptions |
| 92 | +def message_posted(m: oxenmq.Message): |
| 93 | + id = bt_deserialize(m.data()[0]) |
| 94 | + app.logger.warning(f"FIXME: mule -- message posted stub, id={id}") |
| 95 | + |
| 96 | + |
| 97 | +@log_exceptions |
| 98 | +def messages_deleted(m: oxenmq.Message): |
| 99 | + ids = bt_deserialize(m.data()[0]) |
| 100 | + app.logger.warning(f"FIXME: mule -- message delete stub, deleted messages: {ids}") |
| 101 | + |
| 102 | + |
| 103 | +@log_exceptions |
| 104 | +def message_edited(m: oxenmq.Message): |
| 105 | + app.logger.warning("FIXME: mule -- message edited stub") |
0 commit comments