fix bug where last blob in a stream was not saved to blob manager

This commit is contained in:
Kay Kurokawa 2017-10-12 11:25:52 -04:00
parent 574660cc20
commit 551eab9a42

View file

@ -69,17 +69,23 @@ class CryptStreamCreator(object):
self.stopped = True self.stopped = True
self.producer = None self.producer = None
def _close_current_blob(self):
# close the blob that was being written to
# and save it to blob manager
should_announce = self.blob_count == 0
d = self.current_blob.close()
d.addCallback(self._blob_finished)
d.addCallback(lambda blob_info: self.blob_manager.creator_finished(blob_info,
should_announce))
self.finished_deferreds.append(d)
self.current_blob = None
def stop(self): def stop(self):
"""Stop creating the stream. Create the terminating zero-length blob.""" """Stop creating the stream. Create the terminating zero-length blob."""
log.debug("stop has been called for StreamCreator") log.debug("stop has been called for StreamCreator")
self.stopped = True self.stopped = True
if self.current_blob is not None: if self.current_blob is not None:
current_blob = self.current_blob self._close_current_blob()
d = current_blob.close()
d.addCallback(self._blob_finished)
d.addErrback(self._error)
self.finished_deferreds.append(d)
self.current_blob = None
self._finalize() self._finalize()
dl = defer.DeferredList(self.finished_deferreds) dl = defer.DeferredList(self.finished_deferreds)
dl.addCallback(lambda _: self._finished()) dl.addCallback(lambda _: self._finished())
@ -123,6 +129,7 @@ class CryptStreamCreator(object):
d.addCallback(self._blob_finished) d.addCallback(self._blob_finished)
self.finished_deferreds.append(d) self.finished_deferreds.append(d)
def _write(self, data): def _write(self, data):
while len(data) > 0: while len(data) > 0:
if self.current_blob is None: if self.current_blob is None:
@ -133,13 +140,7 @@ class CryptStreamCreator(object):
done, num_bytes_written = self.current_blob.write(data) done, num_bytes_written = self.current_blob.write(data)
data = data[num_bytes_written:] data = data[num_bytes_written:]
if done is True: if done is True:
should_announce = self.blob_count == 0 self._close_current_blob()
d = self.current_blob.close()
d.addCallback(self._blob_finished)
d.addCallback(lambda blob_info: self.blob_manager.creator_finished(blob_info,
should_announce))
self.finished_deferreds.append(d)
self.current_blob = None
def _get_blob_maker(self, iv, blob_creator): def _get_blob_maker(self, iv, blob_creator):
return CryptStreamBlobMaker(self.key, iv, self.blob_count, blob_creator) return CryptStreamBlobMaker(self.key, iv, self.blob_count, blob_creator)