def test_get_current_rhic_lookup_tasks(self): cfg = config.get_rhic_serve_config_info() # Create a valid, in_progress task task_a = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a11111111111", completed=False) task_a.save() # Create a completed task task_b = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a22222222222", completed=True, initiated=datetime.now(tzutc()), modified=datetime.now(tzutc())) task_b.save() # Create a timedout incomplete task timeout_in_minutes = cfg["single_rhic_lookup_timeout_in_minutes"] expired_time = datetime.now(tzutc()) - timedelta(minutes=timeout_in_minutes+1) task_c = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a333333333333", completed=False, initiated=expired_time) task_c.save() # Create a completed expired task expired_hours = cfg["single_rhic_lookup_cache_unknown_in_hours"] expired_time = datetime.now(tzutc()) - timedelta(hours=expired_hours+1) task_d = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a444444444444", completed=True, modified=expired_time) task_d.save() # Ensure all tasks where created and have been saved in mongo current_tasks = [x.uuid for x in RHICLookupTask.objects()] for t in [task_a, task_b, task_c, task_d]: self.assertTrue(t.uuid in current_tasks) # In-progress, valid task task = identity.get_current_rhic_lookup_tasks(task_a.uuid) self.assertIsNotNone(task) self.assertEquals(task.uuid, task_a.uuid) # Completed, valid task task = identity.get_current_rhic_lookup_tasks(task_b.uuid) self.assertIsNotNone(task) self.assertEquals(task.uuid, task_b.uuid) # In-progress, timed out task task = identity.get_current_rhic_lookup_tasks(task_c.uuid) self.assertIsNone(task) found = [x.uuid for x in RHICLookupTask.objects()] self.assertTrue(task_c.uuid not in found) # Completed, cache time expired task task = identity.get_current_rhic_lookup_tasks(task_d.uuid) self.assertIsNone(task) found = [x.uuid for x in RHICLookupTask.objects()] self.assertTrue(task_d.uuid not in found) # Be sure of the 4 tasks we created, the expired and timedout were removed # while the 2 good tasks remained self.assertEquals(len(found), 2) self.assertTrue(task_a.uuid in found) self.assertTrue(task_b.uuid in found)
def complete_rhic_lookup_task(uuid, status_code): """ @param uuid: RHIC uuid @param status_code: HTTP status code from RHIC lookup @return: None """ _LOG.info("complete_rhic_lookup_task(rhic_uuid='%s', status_code='%s') invoked" % (uuid, status_code)) current_task = identity.get_current_rhic_lookup_tasks(uuid) if not current_task: _LOG.warning("completed_rhic_lookup_task with status code '%s' called on uuid '%s' " "yet no task was found" % (status_code, uuid)) return None if status_code in [202, 404]: # 202 - in-progress tasks # 404 - lookups that received a definitive response of the RHIC not existing. current_task.task_id = None current_task.modified = datetime.now(tzutc()) current_task.status_code = status_code current_task.completed = True if status_code == 202: # Parent is still processing request, task will remain active. current_task.completed = False current_task.save() else: # Task will be killed, it either succeeded with a '200' or an unexpected error was seen. msg = "Received [%s] for lookup of RHIC [%s]" % (status_code, uuid) if status_code in [200]: # 200 - RHIC was found in parent _LOG.info(msg) else: _LOG.error(msg) identity.delete_rhic_lookup(current_task) return None
def get_cached_status_code(uuid): """ @param uuid: RHIC uuid @return: """ t = identity.get_current_rhic_lookup_tasks(uuid) if t and t.completed: return t.status_code return None
def update_rhic_lookup_task(uuid, task_id): _LOG.info("update_rhic_lookup_task(rhic_uuid='%s', task_id='%s')" % (uuid, task_id)) current_task = identity.get_current_rhic_lookup_tasks(uuid) if not current_task: current_task = RHICLookupTask(uuid=uuid, initiated=datetime.now(tzutc())) current_task.task_id = task_id current_task.modified = datetime.now(tzutc()) current_task.completed = False current_task.save() _LOG.info("update_rhic_lookup_task(%s, %s) updated task to %s" % (uuid, task_id, current_task)) return current_task
def handle_rhic_lookup(self, rhic_uuid): """ Will look up if an existing lookup is in progress for this RHIC. If a task is found, will return it's status code if completed, or 202 to signal in progress If a task is not found, will create a new task and return 202 to signal in progress @param rhic_uuid: @return: status code to return for request @rtype: int """ _LOG.info("Processing rhic_lookup for an unknown RHIC of UUID '%s' " % (rhic_uuid)) task = get_current_rhic_lookup_tasks(rhic_uuid) if task: if task.completed: ret_code = 404 if task.status_code: ret_code = task.status_code _LOG.info("Using cached value %s" % (task)) return ret_code else: _LOG.info("Lookup task in progress: %s" % (task)) return 202 task = identity_lookup.create_rhic_lookup_task(rhic_uuid) _LOG.info("Initiated new lookup task: %s" % (task)) return 202