Skip to content

Commit 8bf5996

Browse files
committed
add retry function when http post get error response
1 parent ef298b1 commit 8bf5996

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

reyun.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
_ENDPOINT_ = 'http://192.168.2.25:8080/receive/rest/'
1717
_UNKNOWN_ = 'unknown'
1818
_BUFFER_NAME_ = 'reyun-buffer'
19+
_RETRY_TIMES = 3
1920

2021
def _http_call(appid,method,data):
2122
postdata = {}
@@ -28,15 +29,29 @@ def _http_call(appid,method,data):
2829
data = dict((key,str(value)) for key, value in data.iteritems() if key not in postdata.keys())
2930

3031
postdata['context'] = data
32+
jsondata = json.dumps(postdata)
3133
logging.debug(_ENDPOINT_+method)
32-
logging.debug(json.dumps(postdata))
34+
logging.debug(jsondata)
3335

34-
req = urllib2.Request(_ENDPOINT_+method, json.dumps(postdata))
36+
req = urllib2.Request(_ENDPOINT_+method, jsondata)
3537
req.add_header('Content-Type', 'application/json')
36-
rsp = urllib2.urlopen(req)
38+
rsp = None
39+
try:
40+
rsp = urllib2.urlopen(req)
41+
except :
42+
pass
3743

44+
status = 1
45+
content = ''
3846

39-
logging.debug('%s[%s]%s' % (method,rsp.getcode(),rsp.read()))
47+
if rsp != None :
48+
content = rsp.readlines()
49+
status = json.loads(content[0])['status']
50+
if status != 0:
51+
logging.error('[args error] %s %s' % (jsondata,content))
52+
logging.debug('%s[%s]%s' % (method,status,content))
53+
54+
return status
4055

4156
class Consumer(threading.Thread):
4257
def __init__(self,appid, queue):
@@ -50,9 +65,13 @@ def run(self):
5065
if self._queue:
5166
msg = self._queue.blpop(_BUFFER_NAME_)[1]
5267
data = eval(msg)
53-
_http_call(self._appid,data['method'],data['data'])
54-
55-
68+
if data['retry'] < _RETRY_TIMES:
69+
data['retry'] += 1
70+
status = _http_call(self._appid,data['method'],data['data'])
71+
if status == 1:
72+
self._queue.rpush(_BUFFER_NAME_,json.dumps(data))
73+
else:
74+
logging.error('[retry 3 times] %s' % msg)
5675

5776
class Producer():
5877
def __init__(self,appid,queue=None):
@@ -62,7 +81,7 @@ def __init__(self,appid,queue=None):
6281
def produce(self,method,data):
6382
del data['self']
6483
if self._queue:
65-
self._queue.rpush(_BUFFER_NAME_,{'method':method,'data':data})
84+
self._queue.rpush(_BUFFER_NAME_,{'method':method,'data':data,'retry':0})
6685
else:
6786
_http_call(self._appid,method,data)
6887

@@ -71,7 +90,7 @@ def __init__(self,appid,buffer=False,host='localhost',port=6379,db=0):
7190
self._appid=appid
7291
self._queue = None
7392
self.buffer = buffer
74-
if buffer:
93+
if self.buffer:
7594
import redis
7695
self._queue = redis.StrictRedis(host,port,db)
7796
self._consumer = Consumer(appid,self._queue)
@@ -227,9 +246,9 @@ def heartbeat(self,deviceid,who,serverid=_UNKNOWN_,channelid=_UNKNOWN_,level=-1)
227246
channelid = 'appstore'
228247

229248

230-
api = API("appkey",buffer=False,host='x00',port=6379,db=0)
249+
api = API("appkey",buffer=True,host='x00',port=6379,db=0)
231250

232-
api.install(deviceid="xxxxx",serverid=serverid,channelid=channelid)
251+
api.install(deviceid="xxxxxx",serverid=serverid,channelid=channelid)
233252

234253
# api.startup(deviceid="xxxxx",serverid=serverid,channelid=channelid,tz="+8",devicetype="ios",\
235254
# op="cmcc",network="3g",os="ios",resolution="400*600")

0 commit comments

Comments
 (0)