-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.py
executable file
·68 lines (51 loc) · 2.08 KB
/
updater.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
#!/usr/bin/env python
import logging
import signal
from datetime import datetime
from time import sleep, ctime
from metahosting.common import argument_parsing, config_manager, logging_setup
class Updater(object):
def __init__(self, config):
persistence_class = \
config_manager.get_backend_class(config['persistence'])
messaging_class = \
config_manager.get_backend_class(config['messaging'])
self.messaging = messaging_class(config=config['messaging'],
queue='status')
self.datapoints = persistence_class(config=config['persistence'])
self.running = False
def start(self):
self.running = True
logging.info('Starting status updater %s', ctime())
self.messaging.subscribe('status', self.store_datapoint)
while self.running:
logging.info('Heartbeat status updater %s', ctime())
sleep(15)
def stop(self, signal, stack):
logging.info('Stopping status updater with signal %s', signal)
self.running = False
def store_datapoint(self, item):
timestamp = item.pop('ts')
item['ts'] = datetime.fromtimestamp(timestamp)
logging.debug(item)
self.datapoints.insert(item)
def cleanup_old_entries(self, days=14):
old = datetime.datetime.now() - datetime.timedelta(days=days)
self.datapoints.delete_many({"ts": {"$lt": old}})
def run():
arguments = argument_parsing()
logging_setup(arguments=arguments)
if arguments.config:
config_manager._CONFIG_FILE = arguments.config
if arguments.envfile:
config_manager._VARIABLES_FILE = arguments.envfile
config = dict()
config['persistence'] = config_manager.get_configuration('persistence')
config['messaging'] = config_manager.get_configuration('messaging')
updater = Updater(config=config)
signal.signal(signal.SIGTERM, updater.stop)
signal.signal(signal.SIGHUP, updater.stop)
signal.signal(signal.SIGINT, updater.stop)
updater.start()
if __name__ == "__main__":
run()