- """
- We never raise an exception here. Log and keep moving
- Bugs will eventually be caught be examining logs.
- """
- try:
- # First, send out all messages waiting for us
- while not _SEND_QUEUE.empty():
- work_item = _SEND_QUEUE.get(block=False, timeout=None)
- _send(mrc, payload=work_item["payload"], message_type=work_item["msg type"])
-
- # Next, update all statuses waiting in a1s mailbox
- _update_all_statuses(mrc)
-
- # TODO: next body of work is to try to clean up the database for any updated statuses
-
- except Exception as e:
- logger.debug("Polling thread encountered an unexpected exception, but it will continue:")
- logger.exception(e)
-
- time.sleep(1)
+ # send out all messages waiting for us
+ while not _SEND_QUEUE.empty():
+ work_item = _SEND_QUEUE.get(block=False, timeout=None)
+ _send(mrc, payload=work_item["payload"], message_type=work_item["msg type"])
+
+ # read our mailbox and update statuses
+ updated_instances = set()
+ for msg in self._rcv_func():
+ try:
+ pay = json.loads(msg["payload"])
+ pti = pay["policy_type_id"]
+ pii = pay["policy_instance_id"]
+ data.set_status(pti, pii, pay["handler_id"], pay["status"])
+ updated_instances.add((pti, pii))
+ except (PolicyTypeNotFound, PolicyInstanceNotFound, KeyError, json.decoder.JSONDecodeError):
+ # TODO: in the future we may also have to catch SDL errors
+ logger.debug(("Dropping malformed or non applicable message", msg))
+
+ # for all updated instances, see if we can trigger a delete
+ # should be no catch needed here, since the status update would have failed if it was a bad pair
+ for ut in updated_instances:
+ data.clean_up_instance(ut[0], ut[1])
+
+ # TODO: what's a reasonable sleep time? we don't want to hammer redis too much, and a1 isn't a real time component
+ time.sleep(1)
+
+
+def start_rmr_thread(init_func_override=None, rcv_func_override=None):
+ """
+ Start a1s rmr thread
+ Also called during unit testing
+ """
+ rmr_loop = RmrLoop(init_func_override, rcv_func_override)
+ thread = Thread(target=rmr_loop.loop)
+ thread.start()
+ while not rmr_loop.rmr_is_ready():
+ time.sleep(0.5)
+ return rmr_loop # return the handle; useful during unit testing