refactor async calls

This commit is contained in:
j 2014-07-09 20:59:24 +02:00
parent e5a75b41a2
commit 2ce541d211

View file

@ -596,21 +596,13 @@ class Client(object):
post['files'] = files['files']
post['volume'] = name
print 'sending list of files in %s (%s total)' % (name, len(post['files']))
r = self.api.async('update', post)
#send empty list to get updated list of requested info/files/data
post = {'info': {}}
r = self.api.update(post)
if r['status']['code'] == 200:
#backend works on update request asyncronously, wait for it to finish
if 'taskId' in r['data']:
t = self.api.taskStatus(task_id=r['data']['taskId'])
print 'waiting for server ...'
while t['data']['status'] == 'PENDING':
time.sleep(5)
t = self.api.taskStatus(task_id=r['data']['taskId'])
#send empty list to get updated list of requested info/files/data
post = {'info': {}}
r = self.api.update(post)
if r['data']['info']:
r = self.update_info(r['data']['info'], prefix)
if r['data']['info']:
r = self.update_info(r['data']['info'], prefix)
if not 'data' in r:
print r
@ -718,13 +710,7 @@ class Client(object):
print 'sending info for %d files' % len(info)
post = {'info': {}, 'upload': True}
post['info'] = self.get_info_for_ids(info, prefix)
r = self.api.update(post)
if 'taskId' in r['data']:
t = self.api.taskStatus(task_id=r['data']['taskId'])
print 'waiting for server ...'
while t['data']['status'] == 'PENDING':
time.sleep(5)
t = self.api.taskStatus(task_id=r['data']['taskId'])
r = self.api.async('update', post)
#send empty list to get updated list of requested info/files/data
post = {'info': {}}
r = self.api.update(post)
@ -908,6 +894,18 @@ class API(ox.API):
netloc = urlparse(self.url).netloc
self._resume_file = '/tmp/pandora_client.%s.%s.json' % (os.environ.get('USER'), netloc)
def async(self, action, data, interval=5):
t = r = getattr(self, action)(data)
if r['status']['code'] == 200:
# wait for async task to finish
if 'taskId' in r['data']:
t = self.taskStatus(task_id=r['data']['taskId'])
print 'waiting for server ...'
while t['data'].get('status') == 'PENDING':
time.sleep(interval)
t = self.taskStatus(task_id=r['data']['taskId'])
return t
def uploadFrames(self, i, data):
#upload frames
if self.site['media'].get('importFrames') and i['frames']: