-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathreloader.py
49 lines (36 loc) · 1.38 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
41
42
43
44
45
46
47
48
49
import importlib
import os
import threading
import importlib.util
from server import app
_config_mtime = None
_reload_lock = threading.RLock()
if "CONFIG" in os.environ:
config_path = os.environ["CONFIG"]
else:
config_path = os.path.join(app.root_path, 'config.py')
config_spec = importlib.util.spec_from_file_location("server.config", config_path)
config_module = importlib.util.module_from_spec(config_spec)
config_spec.loader.exec_module(config_module)
_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(config_path).st_mtime_ns
if cur_mtime != _config_mtime:
with _reload_lock:
if cur_mtime != _config_mtime:
try:
config_spec.loader.exec_module(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