Skip to content

Commit

Permalink
Fix listen_* methods without valid last_object provided (#24)
Browse files Browse the repository at this point in the history
* Fix listen_* methods without valid last_object provided

* Reorganized last_object method
  • Loading branch information
psrok1 authored Jan 5, 2021
1 parent 70b4a22 commit f7948cd
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions src/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f7948cd

Please sign in to comment.