forked from interuss/dss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracer_poll.py
executable file
·112 lines (87 loc) · 3.66 KB
/
tracer_poll.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!env/bin/python3
import argparse
import datetime
import os
import sys
import time
from typing import List
from monitoring.monitorlib import versioning
from monitoring.tracer import diff, polling
from monitoring.tracer.resources import ResourceSet
def parseArgs() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Poll for changes in DSS-tracked Entity status")
ResourceSet.add_arguments(parser)
# Feature arguments
parser.add_argument('--rid-isa-poll-interval', type=float, default=0, help='Seconds beteween each poll of the DSS for ISAs, 0 to disable DSS polling for ISAs')
parser.add_argument('--scd-operation-poll-interval', type=float, default=0, help='Seconds between each poll of the DSS for Operations, 0 to disable DSS polling for Operations')
parser.add_argument('--scd-constraint-poll-interval', type=float, default=0, help='Seconds between each poll of the DSS for Constraints, 0 to disable DSS polling for Constraints')
return parser.parse_args()
def print_no_newline(s):
sys.stdout.write(s)
sys.stdout.flush()
def main() -> int:
args = parseArgs()
# Required resources
resources = ResourceSet.from_arguments(args)
config = vars(args)
config['code_version'] = versioning.get_code_version()
resources.logger.log_new('poll_start', config)
# Prepare pollers
pollers: List[polling.Poller] = []
if args.rid_isa_poll_interval > 0:
pollers.append(polling.Poller(
name='poll_isas',
object_diff_text=diff.isa_diff_text,
interval=datetime.timedelta(seconds=args.rid_isa_poll_interval),
poll=lambda: polling.poll_rid_isas(resources, resources.area)))
if args.scd_operation_poll_interval > 0:
pollers.append(polling.Poller(
name='poll_ops',
object_diff_text=diff.entity_diff_text,
interval=datetime.timedelta(seconds=args.scd_operation_poll_interval),
poll=lambda: polling.poll_scd_operations(resources)))
if args.scd_constraint_poll_interval > 0:
pollers.append(polling.Poller(
name='poll_constraints',
object_diff_text=diff.entity_diff_text,
interval=datetime.timedelta(seconds=args.scd_constraint_poll_interval),
poll=lambda: polling.poll_scd_constraints(resources)))
if len(pollers) == 0:
sys.stderr.write('Bad arguments: No data types had polling requests')
return os.EX_USAGE
# Execute the polling loop
abort = False
need_line_break = False
while not abort:
try:
most_urgent_dt = datetime.timedelta(days=999999999)
most_urgent_poller = None
for poller in pollers:
dt = poller.time_to_next_poll()
if dt < most_urgent_dt:
most_urgent_poller = poller
most_urgent_dt = dt
if most_urgent_dt.total_seconds() > 0:
time.sleep(most_urgent_dt.total_seconds())
t0 = datetime.datetime.utcnow()
result = most_urgent_poller.poll()
t1 = datetime.datetime.utcnow()
if result.has_different_content_than(most_urgent_poller.last_result):
resources.logger.log_new(most_urgent_poller.name, result)
if need_line_break:
print()
print(most_urgent_poller.diff_text(result))
need_line_break = False
most_urgent_poller.last_result = result
else:
resources.logger.log_same(t0, t1, most_urgent_poller.name)
print_no_newline('.')
need_line_break = True
except KeyboardInterrupt:
abort = True
resources.logger.log_new('poll_stop', {
'timestamp': datetime.datetime.utcnow().isoformat(),
})
return os.EX_OK
if __name__ == "__main__":
sys.exit(main())