convert Daemon._delete_lbry_file to inlineCallbacks

This commit is contained in:
Jack Robison 2017-02-09 13:59:21 -05:00
parent e292abceee
commit d137528f67

View file

@ -830,33 +830,24 @@ class Daemon(AuthJSONRPCServer):
helper = _ResolveNameHelper(self, name, force_refresh) helper = _ResolveNameHelper(self, name, force_refresh)
return helper.get_deferred() return helper.get_deferred()
@defer.inlineCallbacks
def _delete_lbry_file(self, lbry_file, delete_file=True): def _delete_lbry_file(self, lbry_file, delete_file=True):
d = self.lbry_file_manager.delete_lbry_file(lbry_file) stream_hash = lbry_file.stream_hash
filename = os.path.join(self.download_directory, lbry_file.file_name)
def finish_deletion(lbry_file): yield self.lbry_file_manager.delete_lbry_file(lbry_file)
d = lbry_file.delete_data() yield lbry_file.delete_data()
d.addCallback(lambda _: _delete_stream_data(lbry_file)) stream_count = yield self.lbry_file_manager.get_count_for_stream_hash(stream_hash)
return d if stream_count == 0:
yield self.stream_info_manager.delete_stream(stream_hash)
def _delete_stream_data(lbry_file): else:
s_h = lbry_file.stream_hash log.warning("Can't delete stream info for %s", stream_hash)
d = self.lbry_file_manager.get_count_for_stream_hash(s_h) if delete_file:
# TODO: could possibly be a timing issue here if os.path.isfile(filename):
d.addCallback(lambda c: self.stream_info_manager.delete_stream(s_h) if c == 0 else True) os.remove(filename)
if delete_file: log.info("Deleted file %s", filename)
def remove_if_file(): log.info("Deleted stream %s", stream_hash)
filename = os.path.join(self.download_directory, lbry_file.file_name) defer.returnValue(True)
if os.path.isfile(filename):
os.remove(filename)
else:
return defer.succeed(None)
d.addCallback(lambda _: remove_if_file)
return d
d.addCallback(lambda _: finish_deletion(lbry_file))
d.addCallback(lambda _: log.info("Delete lbry file"))
return d
def _get_or_download_sd_blob(self, blob, sd_hash): def _get_or_download_sd_blob(self, blob, sd_hash):
if blob: if blob: