+def delete_policy_instance_if_applicable(policy_type_id, policy_instance_id):
+ """
+ delete a policy instance if all known statuses are DELETED
+
+ pops a1s waiting mailbox
+ """
+ # pop through a1s mailbox, updating a1s db of all policy statuses
+ for msg in a1rmr.dequeue_all_waiting_messages(21024):
+ # try to parse the messages as responses. Drop those that are malformed
+ # NOTE: we don't use the parameters "policy_type_id, policy_instance" from above here,
+ # because we are popping the whole mailbox, which might include other statuses
+ pay = json.loads(msg["payload"])
+ if "policy_type_id" in pay and "policy_instance_id" in pay and "handler_id" in pay and "status" in pay:
+ set_policy_instance_status(pay["policy_type_id"], pay["policy_instance_id"], pay["handler_id"], pay["status"])
+ else:
+ logger.debug("Dropping message")
+ logger.debug(pay)
+
+ # raise if not valid
+ instance_is_valid(policy_type_id, policy_instance_id)
+
+ # see if we can delete
+ vector = get_policy_instance_statuses(policy_type_id, policy_instance_id)
+ if vector != []:
+ all_deleted = True
+ for i in vector:
+ if i != "DELETED":
+ all_deleted = False
+ break # have at least one not DELETED, do nothing
+
+ # blow away from a1 db
+ if all_deleted:
+ del POLICY_DATA[policy_type_id][I][policy_instance_id]
+
+