1 from __future__ import print_function
7 from crunchstat_summary import logger
10 class CollectionReader(object):
11 def __init__(self, collection_id):
12 logger.debug('load collection %s', collection_id)
13 collection = arvados.collection.CollectionReader(collection_id)
14 filenames = [filename for filename in collection]
15 if len(filenames) != 1:
17 "collection {} has {} files; need exactly one".format(
18 collection_id, len(filenames)))
19 self._reader = collection.open(filenames[0])
22 return iter(self._reader)
25 class LiveLogReader(object):
26 def __init__(self, job_uuid):
27 logger.debug('load stderr events for job %s', job_uuid)
29 ['object_uuid', '=', job_uuid],
30 ['event_type', '=', 'stderr']]
31 self._buffer = collections.deque()
33 self._label = job_uuid
35 self._start_getting_next_page()
37 def _start_getting_next_page(self):
38 self._thread = threading.Thread(target=self._get_next_page)
39 self._thread.daemon = True
42 def _get_next_page(self):
43 page = arvados.api().logs().index(
46 filters=self._filters + [['id','>',str(self._last_id)]],
48 self._got += len(page['items'])
50 '%s: received %d of %d log events',
51 self._label, self._got,
52 self._got + page['items_available'] - len(page['items']))
55 def _buffer_page(self):
56 """Wait for current worker, copy results to _buffer, start next worker.
58 Return True if anything was added to the buffer."""
59 if self._thread is None:
64 if len(page['items']) == 0:
66 if len(page['items']) < page['items_available']:
67 self._start_getting_next_page()
68 for i in page['items']:
69 for line in i['properties']['text'].split('\n'):
70 self._buffer.append(line)
71 self._last_id = i['id']
78 if len(self._buffer) == 0:
79 if not self._buffer_page():
81 return self._buffer.popleft() + '\n'