def update(self, lease_time=600): """Update lease time for a formerly enqueued message.""" msg = self.message if msg is None: raise Exception('No message to update.') for _repeat in range(6): try: self.message.update(lease_time, client=self.client) break except IOError as e: sleep(_repeat * 2 + 1) if e.errno == errno.EPIPE: self.client = Client() except GCloudError: sleep(_repeat * 2 + 5)
def _get_message(self, lease_time): """Get one message with lease_time.""" for _repeat in range(6): try: for task in self.handle.lease(lease_time=lease_time, num_tasks=1, client=self.client): return task return None except IOError as e: sleep(_repeat * 2 + 1) if e.errno == errno.EPIPE: self.client = Client() except GCloudError: sleep(_repeat * 2 + 5)
def task_done(self): """Acknowledge that a formerly enqueued task is complete. Note that this method MUST be called for each item. See the class docstring for details. """ if self.message is None: raise Exception('No message to acknowledge.') for _repeat in range(6): try: self.message.delete(client=self.client) self.message = None break except IOError as e: sleep(_repeat * 2 + 1) if e.errno == errno.EPIPE: self.client = Client() except GCloudError: sleep(_repeat * 2 + 5)
def put(self, item, block=True, timeout=None, delay=None): """Put item into the queue. Note that GTQ doesn't implement non-blocking or timeouts for writes, so both 'block' and 'timeout' must have their default values only. """ if not (block and timeout is None): raise Exception('block and timeout must have default values') for _repeat in range(6): try: self.handle.insert_task(description=json.dumps( item, separators=(',', ':')), client=self.client) break except IOError as e: sleep(_repeat * 2 + 1) if e.errno == errno.EPIPE: self.client = Client() except GCloudError: sleep(_repeat * 2 + 5)
def setUp(self): json_credentials_path = os.environ['JSON_CREDENTIALS_PATH'] with open(json_credentials_path) as fp: project_id = json.loads(fp.read()).get("project_id") self.client = Client.from_service_account_json(json_credentials_path, project=project_id)
def __init__(self): self.client = Client()