Skip to content

Commit

Permalink
Retry on Companion server error.
Browse files Browse the repository at this point in the history
Don't complain if errors occur during shipyard retry.
  • Loading branch information
Marginal committed Oct 7, 2015
1 parent 06fbb66 commit 8416720
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions EDMarketConnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
l10n.Translations().install()
EDDB = eddb.EDDB()

SHIPYARD_RETRY = 5 # retry pause for shipyard data [s]
SERVER_RETRY = 5 # retry pause for Companion servers [s]
EDSM_POLL = 0.1


Expand Down Expand Up @@ -252,11 +252,11 @@ def verify(self, code):
else:
return self.getandsend() # try again

def getandsend(self, event=None, retrying_for_shipyard=False):
def getandsend(self, event=None, retrying=False):

play_sound = event and event.type=='35' and not config.getint('hotkey_mute')

if not retrying_for_shipyard:
if not retrying:
if time() < self.holdofftime: # Was invoked by key while in cooldown
self.status['text'] = ''
if play_sound and (self.holdofftime-time()) < companion.holdoff*0.75:
Expand Down Expand Up @@ -287,13 +287,6 @@ def getandsend(self, event=None, retrying_for_shipyard=False):
self.status['text'] = _("What are you flying?!") # Shouldn't happen
if play_sound: hotkeymgr.play_bad()

elif retrying_for_shipyard:
if __debug__:
print 'Retry for shipyard - ' + (data['commander'].get('docked') and (data['lastStarport'].get('ships') and 'Success' or 'Failure') or 'Undocked!')
if data['commander'].get('docked'): # might have undocked while we were waiting for retry in which case station data is unreliable
eddn.export_shipyard(data)
self.status['text'] = strftime(_('Last updated at {HH}:{MM}:{SS}').format(HH='%H', MM='%M', SS='%S').encode('utf-8'), localtime(querytime)).decode('utf-8')

else:
if __debug__: # Recording
with open('%s%s.%s.json' % (data['lastSystem']['name'], data['commander'].get('docked') and '.'+data['lastStarport']['name'] or '', strftime('%Y-%m-%dT%H.%M.%S', localtime())), 'wt') as h:
Expand Down Expand Up @@ -359,9 +352,8 @@ def getandsend(self, event=None, retrying_for_shipyard=False):
if data['lastStarport'].get('ships'):
eddn.export_shipyard(data)
else:
# API is flakey about shipyard info - retry if missing (<1s is usually sufficient - 5s for margin).
self.w.after(int(SHIPYARD_RETRY * 1000), lambda:self.getandsend(event, retrying_for_shipyard=True))
return # early exit to avoid starting cooldown count
# API is flakey about shipyard info - silently retry if missing (<1s is usually sufficient - 5s for margin).
self.w.after(int(SERVER_RETRY * 1000), self.retry_for_shipyard)
elif __debug__ and data['lastStarport'].get('ships'):
print 'Spurious shipyard!'

Expand All @@ -372,8 +364,13 @@ def getandsend(self, event=None, retrying_for_shipyard=False):

# Companion API problem
except companion.ServerError as e:
self.status['text'] = unicode(e)
if play_sound: hotkeymgr.play_bad()
if retrying:
self.status['text'] = unicode(e)
if play_sound: hotkeymgr.play_bad()
else:
# Retry once if Companion server is unresponsive
self.w.after(int(SERVER_RETRY * 1000), lambda:self.getandsend(event, True))
return # early exit to avoid starting cooldown count

except requests.exceptions.ConnectionError as e:
if __debug__: print_exc()
Expand All @@ -393,6 +390,17 @@ def getandsend(self, event=None, retrying_for_shipyard=False):
self.holdofftime = querytime + companion.holdoff
self.cooldown()

def retry_for_shipyard(self):
# Try again to get shipyard data and send to EDDN. Don't report errors if can't get or send the data.
try:
data = self.session.query()
if __debug__:
print 'Retry for shipyard - ' + (data['commander'].get('docked') and (data['lastStarport'].get('ships') and 'Success' or 'Failure') or 'Undocked!')
if data['commander'].get('docked'): # might have undocked while we were waiting for retry in which case station data is unreliable
eddn.export_shipyard(data)
except:
pass

def edsmpoll(self):
result = self.edsm.result
if result['done']:
Expand Down

0 comments on commit 8416720

Please sign in to comment.