Package intermine :: Module idresolution
[hide private]
[frames] | no frames]

Source Code for Module intermine.idresolution

 1  import weakref 
 2  import time 
 3   
 4  # Use core json for 2.6+, simplejson for <=2.5 
 5  try: 
 6      import json 
 7  except ImportError: 
 8      import simplejson as json 
 9   
10 -def get_json(service, path, key):
11 sock = None 12 try: 13 sock = service.opener.open(service.root + path) 14 text = sock.read() 15 finally: 16 if sock: sock.close() 17 data = json.loads(text) 18 if data['error'] is not None: 19 raise Exception(data['error']) 20 if key not in data: 21 raise Exception(key + " not returned from " + path) 22 return data[key]
23 24 ONE_MINUTE = 60 25 26 COMPLETED = set(["SUCCESS", "ERROR"]) 27
28 -class Job(object):
29 """ 30 A Representation of an Identifier Resolution Job 31 ================================================ 32 33 Users can submit requests to resolve sets of IDs to 34 objects in the data-store. These jobs begin in a PENDING 35 state, and transition through RUNNING to either SUCCESS 36 or ERROR. 37 38 Upon completion, the results of the job may be fetched, and the 39 job may be deleted on the server. 40 """ 41 42 INITIAL_DECAY = 1.25 43 INITIAL_BACKOFF = 0.05 44 MAX_BACKOFF = ONE_MINUTE 45
46 - def __init__(self, service, uid):
47 self.service = weakref.proxy(service) 48 self.uid = uid 49 self.status = None 50 self.backoff = Job.INITIAL_BACKOFF 51 self.decay = Job.INITIAL_DECAY 52 self.max_backoff = Job.MAX_BACKOFF 53 if self.uid is None: 54 raise Exception("No uid found")
55
56 - def poll(self):
57 """ 58 Check to see if the job has been completed, updating the 59 status of the job in the process. 60 61 @return: Boolean Whether or not the job is complete. 62 """ 63 if self.status not in COMPLETED: 64 backoff = self.backoff 65 self.backoff = min(self.max_backoff, backoff * self.decay) 66 time.sleep(backoff) 67 self.status = self.fetch_status() 68 return self.status in COMPLETED
69
70 - def fetch_status(self):
71 """ 72 Retrieve the results of this completed job from the server. 73 74 @rtype: dict 75 """ 76 return get_json(self.service, "/ids/{0}/status".format(self.uid), "status")
77
78 - def delete(self):
79 """ 80 Delete the job from the server. 81 82 The job should not be used again once this method has been invoked. 83 """ 84 path = "/ids/" + self.uid 85 response = self.service.opener.delete(self.service.root + path) 86 response_data = json.loads(response) 87 if response_data['error'] is not None: 88 raise Exception(response_data['error'])
89
90 - def fetch_results(self):
91 """ 92 Retrieve the current status of this job from the server. 93 94 @rtype String 95 """ 96 return get_json(self.service, "/ids/{0}/result".format(self.uid), "results")
97