forked from DestructiveVoice/DestructiveFarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreloader.py
40 lines (28 loc) · 1.08 KB
/
reloader.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
import importlib
import os
import threading
from server import app, config as config_module
_config_mtime = None
_reload_lock = threading.RLock()
_cur_config = config_module.CONFIG
def get_config():
"""
Returns CONFIG dictionary from config.py module.
If config.py file was updated since the last call, get_config() reloads
the dictionary. If an error happens during reloading, get_config() returns
the old dictionary.
:returns: the newest valid version of the CONFIG dictionary
"""
global _config_mtime, _cur_config
cur_mtime = os.stat(os.path.join(app.root_path, 'config.py')).st_mtime_ns
if cur_mtime != _config_mtime:
with _reload_lock:
if cur_mtime != _config_mtime:
try:
importlib.reload(config_module)
_cur_config = config_module.CONFIG
app.logger.info('New config loaded')
except Exception as e:
app.logger.error('Failed to reload config: %s', e)
_config_mtime = cur_mtime
return _cur_config