diff --git a/src/core.py b/src/core.py index a836e3e..a051c92 100644 --- a/src/core.py +++ b/src/core.py @@ -168,42 +168,35 @@ def recent_blobs(self): def _listen(self, last_object, object_type, blocking=True, interval=15, query=None): if last_object is None: - last_object = next(self._recent(object_type, query=query)) + last_object = next(self._recent(object_type, query=query), None) # If there are no elements (even first element): just get new samples from now on - if last_object is not None: - last_id = last_object.id - last_time = last_object.upload_time elif isinstance(last_object, MWDBObject): # If we are requesting for typed objects, we should additionally check the object type if object_type is not MWDBObject and not isinstance(last_object, object_type): raise TypeError("latest_object type must be 'str' or '{}'".format(object_type.__name__)) # If object instance provided: get ID from instance - last_id = last_object.id - last_time = last_object.upload_time else: # If not: first check whether object exists in repository - last_obj = self._query(object_type, last_object, raise_not_found=True) - last_id = last_obj.id - last_time = last_obj.upload_time + last_object = self._query(object_type, last_object, raise_not_found=True) while True: objects = [] for obj in self._recent(object_type, query=query): - if obj.id == last_id: - break - - if obj.upload_time < last_time: - raise RuntimeError( - "Newly fetched object [{}] is older than the pivot [{}]".format( - obj.id, last_id + if last_object: + if obj.id == last_object.id: + break + + if obj.upload_time < last_object.upload_time: + raise RuntimeError( + "Newly fetched object [{}] is older than the pivot [{}]".format( + obj.id, last_object.id + ) ) - ) objects.append(obj) # Return fetched objects in reversed order (from oldest to latest) for obj in objects[::-1]: - last_id = obj.id - last_time = obj.upload_time + last_object = obj yield obj if blocking: time.sleep(interval)