-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattempt_logging.py
75 lines (67 loc) · 1.79 KB
/
attempt_logging.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
73
74
75
import time
import uuid
def fun(attempt_number):
print('in fun()')
if (attempt_number < 3):
raise Exception('My code is failing!')
return True
def attempt(timeout=60, wait_seconds=10, max_attempts=10):
meta = {
'function_call_id': str(uuid.uuid4()),
}
error = None
result = None
for attempt_number in (range(1, max_attempts+1)):
error = None
result = None
retry = False
start_time = time.perf_counter()
pre_attempt_meta = {
**meta,
'attempt_stage': 'start',
'attempt': attempt_number,
'start_time': start_time,
}
if __debug__:
print(pre_attempt_meta)
post_attempt_meta = {
**meta,
'attempt': attempt_number,
'attempt_stage': 'end',
}
try:
result = fun(attempt_number)
except Exception as e:
error = e
if isinstance(e, Exception):
retry = True
end_time = time.perf_counter()
post_attempt_meta.update({
'outcome': repr(e),
})
if result:
if result is False:
retry = True
post_attempt_meta.update({
'outcome': result,
})
end_time = time.perf_counter()
post_attempt_meta.update({
'end_time': end_time,
'elapsed_time': end_time - start_time,
})
if __debug__:
print(post_attempt_meta)
if retry:
time.sleep(wait_seconds)
else:
break
if error:
raise error
else:
return result
try:
result = attempt()
print('outcome:', result)
except Exception as e:
print('outcome:', repr(e))