-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcentralHub.py
More file actions
executable file
·105 lines (87 loc) · 3.12 KB
/
centralHub.py
File metadata and controls
executable file
·105 lines (87 loc) · 3.12 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
To use this code in your program:
import Pyro4
hub = Pyro4.Proxy('PYRONAME:central.hub')
# For example in the rain sensor program:
hub.report_in('rain_sensor')
"""
import argparse
import threading
import time
import Pyro4
Pyro4.config.REQUIRE_EXPOSE = True
class CentralHub(object):
"""
Central hub object.
This is exposed through Pyro and monitors the status of the
individual helper processes running.
"""
def __init__(self):
self._ntimes = {'rain_sensor': 10,
'alcor': 10,
'microphones': 10,
'cloud_watcher': 10,
'transparency': 10,
'data_transfer': 10,
'sge_running': 10,
'sge_queued': 10,
'free_gb': 4320,
'uncopied_gb': 4320}
self.sleeptime = 30
self.monitors = sorted(list(self._ntimes.keys()))
self.status = {monitor: False for monitor in self.monitors}
self.connections = {monitor: 0 for monitor in self.monitors}
self.print_thread = threading.Thread(target=self.print_status)
self.print_thread.daemon = True
self.print_thread.start()
def single_report_in(self, name, arg):
lower_name = name.lower()
if lower_name not in self.monitors:
return {'ok': False,
'reason': 'No monitor for {}. Available monitors: {}'.format(
name, list(self.monitors))}
old_connections = self.connections[lower_name]
self.connections[lower_name] = self._ntimes[lower_name]
old_status = self.status[lower_name]
if arg is None:
return_status = True
else:
return_status = arg
self.status[lower_name] = return_status
return {'ok': return_status, 'name': lower_name, 'previous': {
'connections': old_connections, 'status': old_status,
}}
@Pyro4.expose
def report_in(self, name, arg=None):
out = self.single_report_in(name, arg)
return out
def print_status(self):
while True:
print(self.status)
print(self.connections)
self.update_connections()
time.sleep(self.sleeptime)
def update_connections(self):
for monitor in self.monitors:
if self.connections[monitor] > 0:
self.connections[monitor] -= 1
if self.connections[monitor] <= 0:
self.connections[monitor] = 0
self.status[monitor] = False
@Pyro4.expose
def get_status(self):
return self.status
def main(args):
hub = CentralHub()
daemon = Pyro4.Daemon(args.daemon_host)
ns = Pyro4.locateNS()
uri = daemon.register(hub)
ns.register('central.hub', uri)
daemon.requestLoop()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--daemon-host', required=False, default='10.2.5.32',
help='Host to publish daemon to')
main(parser.parse_args())