Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 6.7k
Update compute metadata hanging get example to use "wait_for_change".#332
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
8f2b9a007b6edff5d952b7216f494eee7afFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -26,44 +26,52 @@ | ||
| import requests | ||
| METADATA_URL = "http://metadata.google.internal/computeMetadata/v1/" | ||
| METADATA_URL = 'http://metadata.google.internal/computeMetadata/v1/' | ||
| METADATA_HEADERS = {'Metadata-Flavor': 'Google'} | ||
| 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: | ||
| ||
| 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': | ||
Contributor 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.