Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
Merge pull request #163 from opdemand/feature/chef-api-retries
Browse files Browse the repository at this point in the history
LGTM.
  • Loading branch information
mboersma committed Sep 5, 2013
2 parents b22cf85 + 9433bf2 commit 242ac17
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions celerytasks/chef.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import httplib
import json
import re
import time
import urlparse

from chef_rsa import Key
Expand All @@ -22,7 +23,7 @@ def ruby_b64encode(value):
"""
b64 = base64.b64encode(value)
for i in xrange(0, len(b64), 60):
yield b64[i:i+60]
yield b64[i:i + 60]


class UTC(datetime.tzinfo):
Expand Down Expand Up @@ -116,12 +117,20 @@ def __init__(self, server_url, client_name, client_key):
self.conn = httplib.HTTPSConnection(self.hostname)
self.conn.connect()

def request(self, verb, path, body=''):
def request(self, verb, path, body='', attempts=5, interval=5):
url = self.path + path
headers = create_authorization(
self.headers, verb, url, self.client_key, self.client_name, body)
self.conn.request(verb, url, body=body, headers=headers)
resp = self.conn.getresponse()
# retry all chef api requests
for _ in range(attempts):
self.conn.request(verb, url, body=body, headers=headers)
resp = self.conn.getresponse()
if resp.status != 500:
break
time.sleep(interval)
else:
errmsg = 'Chef API requests failed: {}'.format(path)
raise RuntimeError(errmsg)
return resp.read(), resp.status

def create_databag(self, name):
Expand Down

0 comments on commit 242ac17

Please sign in to comment.