diff options
author | Donald Stufft <donald@stufft.io> | 2013-07-24 22:54:05 -0400 |
---|---|---|
committer | Donald Stufft <donald@stufft.io> | 2013-07-24 22:54:05 -0400 |
commit | ea1ea42752e5e04ff0f22f152284ccd9518afcc8 (patch) | |
tree | 6694b2b9d56e6a5c51ec0b401ab75334fb5abf87 /tasks.py | |
parent | 3317ec0b3cee60db516909ba407289b0eaf1cb92 (diff) | |
download | decorator-ea1ea42752e5e04ff0f22f152284ccd9518afcc8.tar.gz |
Use RQ to handle purging asynchronously
Diffstat (limited to 'tasks.py')
-rw-r--r-- | tasks.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/tasks.py b/tasks.py new file mode 100644 index 0000000..dab4427 --- /dev/null +++ b/tasks.py @@ -0,0 +1,41 @@ +import urlparse + +import requests + + +def purge_fastly_tags(domain, api_key, service_id, *tags, max_tries=25): + session = requests.session() + headers = {"X-Fastly-Key": api_key, "Accept": "application/json"} + + all_tags = set(tags) + purges = {} + + count = 0 + + while all_tags and not count > max_tries: + try: + for tag in set(all_tags): + # Build the URL + url_path = "/service/%s/purge/%s" % (service_id, tag) + url = urlparse.urljoin(domain, url_path) + + # Issue the Purge + resp = session.post(url, headers=headers) + resp.raise_for_status() + + # Store the Purge ID so we can track it later + purges[tag] = resp.json()["id"] + + for tag, purge_id in purges.iteritems(): + # Ensure that the purge completed successfully + url = urlparse.urljoin(domain, "/purge") + status = session.get(url, params={"id": purge_id}) + status.raise_for_status() + + # If the purge completely successfully remove the tag from + # our list. + if status.json().get("results", {}).get("complete", None): + all_tags.remove(tag) + except Exception: + if count > max_tries: + raise |