Skip to content

Commit

Permalink
Merge pull request #264 from Cal-CS-61A-Staff/client-update
Browse files Browse the repository at this point in the history
Terminate server requests when software update succeeds
  • Loading branch information
albert12132 committed Oct 19, 2014
2 parents d0fd1a2 + 912bde6 commit 25836cd
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions client/utils/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ def send_to_server(access_token, messages, name, server, version, log,
log.warning('Server error message: %s', response_json['message'])
try:
if ex.code == 403:
software_update(response_json['data']['download_link'], log)
if software_update(response_json['data']['download_link'], log):
raise SoftwareUpdated
return {}
except Exception as e:
log.warn('Could not connect to %s', server)
log.warning('Could not connect to %s', server)

def dump_to_server(access_token, msg_queue, name, server, insecure, staging_queue,
version, log):
Expand All @@ -47,6 +48,10 @@ def dump_to_server(access_token, msg_queue, name, server, insecure, staging_queu
staging_queue.get() #throw away successful message
else:
msg_queue.put(staging_queue.get())
except SoftwareUpdated:
log.info('ok was updated. Abort now; messages will be sent '
'to server on next invocation')
return
except error.URLError as ex:
log.warning('URLError: %s', str(ex))
return
Expand All @@ -59,8 +64,16 @@ def server_timer():
# Software Updating #
#####################

class SoftwareUpdated(BaseException):
pass

def software_update(download_link, log):
"""Check for the latest version of ok and update this file accordingly."""
"""Check for the latest version of ok and update this file accordingly.
RETURN:
bool; True if the newest version of ok was written to the filesystem, False
otherwise.
"""
log.info('Retrieving latest version from %s', download_link)

file_destination = 'ok'
Expand All @@ -72,10 +85,11 @@ def software_update(download_link, log):
zip_binary = response.read()
log.info('Writing new version to %s', file_destination)
with open(file_destination, 'wb') as f:
f.write(zip_binary)
os.fsync(f)
log.info('Successfully wrote to %s', file_destination)
return True
except error.HTTPError as e:
log.warn('Error when downloading new version of ok: %s', str(e))
log.warning('Error when downloading new version of ok: %s', str(e))
except IOError as e:
log.warn('Error writing to %s: %s', file_destination, str(e))

log.warning('Error writing to %s: %s', file_destination, str(e))
return False

0 comments on commit 25836cd

Please sign in to comment.