-
Notifications
You must be signed in to change notification settings - Fork 381
/
Copy pathwait_for_entity.py
executable file
·72 lines (64 loc) · 2.12 KB
/
wait_for_entity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Amazon Software License (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://aws.amazon.com/asl/
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express
# or implied. See the License for the specific language governing permissions
# and limitations under the License.
"""
Waits for an entity to reach a terminal state.
Type of entity can be inferred from the initial two letters of the id,
or specified manually with the convention:
ds = data source
ml = ML Model
ev = evaluation
bp = batch prediction
Useage:
python wait_for_entity.py entity_id [entity_type]
"""
import boto
import datetime
import json
import random
import sys
import time
def poll_until_completed(entity_id, entity_type_str):
ml = boto.connect_machinelearning()
polling_function = {
'ds': ml.get_data_source,
'ml': ml.get_ml_model,
'ev': ml.get_evaluation,
'bp': ml.get_batch_prediction,
}[entity_type_str]
delay = 2
while True:
results = polling_function(entity_id)
status = results['Status']
message = results.get('Message', '')
now = str(datetime.datetime.now().time())
print("Object %s is %s (%s) at %s" % (entity_id, status, message, now))
if status in ['COMPLETED', 'FAILED', 'INVALID']:
break
# exponential backoff with jitter
delay *= random.uniform(1.1, 2.0)
time.sleep(delay)
print(json.dumps(results, indent=2))
if __name__ == "__main__":
try:
entity_id = sys.argv[1]
if len(sys.argv) > 2:
entity_type_str = sys.argv[2]
else:
entity_type_str = entity_id[:2]
if entity_type_str not in ['ds', 'ml', 'ev', 'bp']:
raise RuntimeError("Unknown entity type")
except:
print(__doc__)
sys.exit(-1)
poll_until_completed(entity_id, entity_type_str)