Commit a0b8ade5 by Guba Sándor

disk: added 2 level info and abortable function

parent 5350131f
...@@ -105,7 +105,7 @@ class Disk(object): ...@@ -105,7 +105,7 @@ class Disk(object):
# Call subprocess # Call subprocess
subprocess.check_output(cmdline) subprocess.check_output(cmdline)
def download(self, task, url): def download(self, task, url, parent_id=None):
''' Download image from url. ''' ''' Download image from url. '''
disk_path = self.get_path() disk_path = self.get_path()
logger.info("Downloading image from %s to %s", url, disk_path) logger.info("Downloading image from %s to %s", url, disk_path)
...@@ -113,13 +113,27 @@ class Disk(object): ...@@ -113,13 +113,27 @@ class Disk(object):
if r.status_code == 200: if r.status_code == 200:
class AbortException(Exception): class AbortException(Exception):
pass pass
if parent_id is None:
parent_id = task.request.id
percent_size = float(r.headers['content-length']) / 100
percent = 0
actual_size = 0
chunk_size = 256 * 1024
try: try:
with open(disk_path, 'wb') as f: with open(disk_path, 'wb') as f:
for chunk in r.iter_content(chunk_size=4096): for chunk in r.iter_content(chunk_size=chunk_size):
if task.is_aborted(): if task.is_aborted():
raise AbortException() raise AbortException()
if chunk: if chunk:
f.write(chunk) f.write(chunk)
actual_size += chunk_size
if actual_size > (percent_size * percent):
percent += 1
task.update_state(
task_id=parent_id,
state=task.AsyncResult(parent_id).state,
meta={'size': actual_size,
'percent': percent})
f.flush() f.flush()
self.size = os.path.getsize(disk_path) self.size = os.path.getsize(disk_path)
logger.debug("Download finished %s (%s bytes)", logger.debug("Download finished %s (%s bytes)",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment