Skip to content
This repository has been archived by the owner on Oct 15, 2019. It is now read-only.

Commit

Permalink
fix(taskqueue): fix double logging, handle 429s silently
Browse files Browse the repository at this point in the history
  • Loading branch information
TheKevJames committed Aug 9, 2018
1 parent ce9eaa7 commit 2938dfd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 26 deletions.
5 changes: 3 additions & 2 deletions gcloud/rest/taskqueue/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ def find_and_process_work(self):
task_lease = self.tq.lease(num_tasks=self.batch_size,
lease_duration=self.lease_seconds)
except requests.exceptions.HTTPError as e:
log.error('got error attempting to lease tasks, retrying',
exc_info=e)
if e.response.status_code != 429:
log.error('got error attempting to lease tasks, retrying',
exc_info=e)
return True

if not task_lease:
Expand Down
31 changes: 8 additions & 23 deletions gcloud/rest/taskqueue/queue.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
import logging
import threading

import requests

from gcloud.rest.auth import Token


log = logging.getLogger(__name__)

API_ROOT = 'https://cloudtasks.googleapis.com/v2beta2'
LOCATION = 'us-central1'
SCOPES = [
'https://www.googleapis.com/auth/cloud-tasks',
]


def raise_for_status(resp):
try:
resp.raise_for_status()
except requests.exceptions.HTTPError as e:
try:
log.error(e.response.json())
except ValueError:
log.error(e.response.text)

raise


class TaskQueue(object):
def __init__(self, project, taskqueue, creds=None, google_api_lock=None,
location=LOCATION):
Expand Down Expand Up @@ -64,7 +49,7 @@ def ack(self, task):
with self.google_api_lock:
resp = requests.post(url, headers=self.headers(), json=body)

raise_for_status(resp)
resp.raise_for_status()
return resp.json()

# https://cloud.google.com/cloud-tasks/docs/reference/rest/v2beta2/projects.locations.queues.tasks/cancelLease
Expand All @@ -79,7 +64,7 @@ def cancel(self, task):
with self.google_api_lock:
resp = requests.post(url, headers=self.headers(), json=body)

raise_for_status(resp)
resp.raise_for_status()
return resp.json()

# https://cloud.google.com/cloud-tasks/docs/reference/rest/v2beta2/projects.locations.queues.tasks/delete
Expand All @@ -89,7 +74,7 @@ def delete(self, tname):
with self.google_api_lock:
resp = requests.delete(url, headers=self.headers())

raise_for_status(resp)
resp.raise_for_status()
return resp.json()

def drain(self):
Expand All @@ -114,7 +99,7 @@ def get(self, tname, full=False):
with self.google_api_lock:
resp = requests.get(url, headers=self.headers(), params=params)

raise_for_status(resp)
resp.raise_for_status()
return resp.json()

# https://cloud.google.com/cloud-tasks/docs/reference/rest/v2beta2/projects.locations.queues.tasks/create
Expand All @@ -134,7 +119,7 @@ def insert(self, payload, tag=None):
with self.google_api_lock:
resp = requests.post(url, headers=self.headers(), json=body)

raise_for_status(resp)
resp.raise_for_status()
return resp.json()

# https://cloud.google.com/cloud-tasks/docs/reference/rest/v2beta2/projects.locations.queues.tasks/lease
Expand All @@ -152,7 +137,7 @@ def lease(self, num_tasks=1, lease_duration=10, task_filter=None):
with self.google_api_lock:
resp = requests.post(url, headers=self.headers(), json=body)

raise_for_status(resp)
resp.raise_for_status()
return resp.json()

# https://cloud.google.com/cloud-tasks/docs/reference/rest/v2beta2/projects.locations.queues.tasks/list
Expand All @@ -167,7 +152,7 @@ def list(self, full=False, page_size=1000, page_token=''):
with self.google_api_lock:
resp = requests.get(url, headers=self.headers(), params=params)

raise_for_status(resp)
resp.raise_for_status()
return resp.json()

# https://cloud.google.com/cloud-tasks/docs/reference/rest/v2beta2/projects.locations.queues.tasks/renewLease
Expand All @@ -183,5 +168,5 @@ def renew(self, task, lease_duration=10):
with self.google_api_lock:
resp = requests.post(url, headers=self.headers(), json=body)

raise_for_status(resp)
resp.raise_for_status()
return resp.json()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

setuptools.setup(
name='gcloud-rest',
version='1.5.1',
version='1.5.2',
description='RESTful Python Client for Google Cloud',
long_description=README,
namespace_packages=[
Expand Down

0 comments on commit 2938dfd

Please sign in to comment.