-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update compute metadata hanging get example to use "wait_for_change". #332
Changes from 1 commit
8f2b9a0
07b6edf
f5d952b
7216f49
4eee7af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,38 +32,46 @@ | |
|
||
def wait_for_maintenance(callback): | ||
url = METADATA_URL + 'instance/maintenance-event' | ||
last_in_maintenance = False | ||
last_maintenance_event = None | ||
# [START hanging_get] | ||
last_etag = 0 | ||
last_etag = "0" | ||
|
||
while True: | ||
r = requests.get( | ||
url, | ||
params={'last_etag': last_etag}, | ||
params={'last_etag': last_etag, 'wait_for_change': True}, | ||
headers=METADATA_HEADERS) | ||
|
||
# During maintenance the service can return a 503, so these should | ||
# be retried. | ||
if r.status_code == 503: | ||
time.sleep(1) | ||
continue | ||
elif r.status_code != requests.codes.ok: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only status the docs mention that we should explicitly check for is 503. Any other error is better surfaced through the standard exception infrastructure rather than just sleeping and trying again. Why not remove this and just use |
||
print('Unexpected HTTP return code from metadata server: {}:{}.' | ||
.format(r.status_code, r.reason)) | ||
time.sleep(1) | ||
continue | ||
|
||
last_etag = r.headers['etag'] | ||
# [END hanging_get] | ||
|
||
if r.text == 'MIGRATE_ON_HOST_MAINTENANCE': | ||
in_maintenance = True | ||
if r.text != 'NONE': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you've added an else, please switch the logic here. |
||
# Possible events: | ||
# MIGRATE_ON_HOST_MAINTENANCE: instance will be migrated | ||
# SHUTDOWN_ON_HOST_MAINTENANCE: instance will be shut down | ||
maintenance_event = r.text | ||
else: | ||
in_maintenance = False | ||
maintenance_event = None | ||
|
||
if in_maintenance != last_in_maintenance: | ||
last_in_maintenance = in_maintenance | ||
callback(in_maintenance) | ||
if maintenance_event != last_maintenance_event: | ||
last_maintenance_event = maintenance_event | ||
callback(maintenance_event) | ||
|
||
|
||
def maintenance_callback(status): | ||
if status: | ||
print('Undergoing host maintenance') | ||
def maintenance_callback(event): | ||
if event: | ||
print('Undergoing host maintenance: {}'.format(event)) | ||
else: | ||
print('Finished host maintenance') | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use single quotes everywhere, please.