11
11
12
12
logger = logging .getLogger (__name__ )
13
13
14
+ FilesToWatch = Dict [Path , Optional [str ]]
14
15
15
- def tls_cert_files_config (config : KresConfig ) -> List [Any ]:
16
+ _config_store : Optional [ConfigStore ] = None
17
+
18
+
19
+ def watched_files_config (config : KresConfig ) -> List [Any ]:
16
20
return [
17
21
config .network .tls .files_watchdog ,
18
22
config .network .tls .cert_file ,
19
23
config .network .tls .key_file ,
24
+ config .local_data .rpz ,
20
25
]
21
26
22
27
23
- FilesToWatch = Dict [Path , str ]
24
-
25
-
26
28
if WATCHDOG_LIB :
27
29
from watchdog .events import (
28
30
FileSystemEvent ,
@@ -33,48 +35,76 @@ def tls_cert_files_config(config: KresConfig) -> List[Any]:
33
35
class FilesWatchdogEventHandler (FileSystemEventHandler ):
34
36
def __init__ (self , files : FilesToWatch ) -> None :
35
37
self ._files = files
36
- self ._timer : Optional [Timer ] = None
38
+ self ._policy_timer : Optional [Timer ] = None
39
+ self ._timers : Dict [str , Timer ] = {}
40
+
41
+ def _trigger (self , cmd : Optional [str ]) -> None :
42
+ def policy_reload () -> None :
43
+ assert _config_store is not None
44
+ if compat .asyncio .is_event_loop_running ():
45
+ compat .asyncio .create_task (_config_store .renew ())
46
+ else :
47
+ compat .asyncio .run (_config_store .renew ())
48
+ logger .info ("Reloading policy rules has finished" )
49
+
50
+ if not cmd :
51
+ # skipping if reload was already triggered
52
+ if self ._policy_timer and self ._policy_timer .is_alive ():
53
+ logger .info ("Skipping reloading policy rules, it was already triggered" )
54
+ return
55
+ # start a 5sec timer
56
+ logger .info ("Delayed policy rules reload has started" )
57
+ self ._policy_timer = Timer (5 , policy_reload )
58
+ self ._policy_timer .start ()
59
+ return
37
60
38
- def _reload (self , cmd : str ) -> None :
39
61
def command () -> None :
40
62
if compat .asyncio .is_event_loop_running ():
41
63
compat .asyncio .create_task (command_registered_workers (cmd ))
42
64
else :
43
65
compat .asyncio .run (command_registered_workers (cmd ))
44
- logger .info ("Reloading of TLS certificate files has finished" )
66
+ logger .info (f"Sending ' { cmd } ' command to reload watched files has finished" )
45
67
46
- # skipping if reload was already triggered
47
- if self ._timer and self ._timer .is_alive ():
48
- logger .info ("Skipping TLS certificate files reloading, reload command was already triggered" )
68
+ # skipping if command was already triggered
69
+ if cmd in self ._timers and self ._timers [ cmd ] .is_alive ():
70
+ logger .info (f "Skipping sending ' { cmd } ' command, it was already triggered" )
49
71
return
50
72
# start a 5sec timer
51
- logger .info ("Delayed reload of TLS certificate files has started" )
52
- self ._timer = Timer (5 , command )
53
- self ._timer .start ()
73
+ logger .info (f "Delayed send of ' { cmd } ' command has started" )
74
+ self ._timers [ cmd ] = Timer (5 , command )
75
+ self ._timers [ cmd ] .start ()
54
76
55
77
def on_created (self , event : FileSystemEvent ) -> None :
56
78
src_path = Path (str (event .src_path ))
57
79
if src_path in self ._files .keys ():
58
80
logger .info (f"Watched file '{ src_path } ' has been created" )
59
- self ._reload (self ._files [src_path ])
81
+ self ._trigger (self ._files [src_path ])
60
82
61
83
def on_deleted (self , event : FileSystemEvent ) -> None :
62
84
src_path = Path (str (event .src_path ))
63
85
if src_path in self ._files .keys ():
64
86
logger .warning (f"Watched file '{ src_path } ' has been deleted" )
65
- if self ._timer :
66
- self ._timer .cancel ()
87
+ cmd = self ._files [src_path ]
88
+ if cmd in self ._timers :
89
+ self ._timers [cmd ].cancel ()
67
90
for file in self ._files .keys ():
68
91
if file .parent == src_path :
69
92
logger .warning (f"Watched directory '{ src_path } ' has been deleted" )
70
- if self ._timer :
71
- self ._timer .cancel ()
93
+ cmd = self ._files [file ]
94
+ if cmd in self ._timers :
95
+ self ._timers [cmd ].cancel ()
96
+
97
+ def on_moved (self , event : FileSystemEvent ) -> None :
98
+ src_path = Path (str (event .src_path ))
99
+ if src_path in self ._files .keys ():
100
+ logger .info (f"Watched file '{ src_path } ' has been moved" )
101
+ self ._trigger (self ._files [src_path ])
72
102
73
103
def on_modified (self , event : FileSystemEvent ) -> None :
74
104
src_path = Path (str (event .src_path ))
75
105
if src_path in self ._files .keys ():
76
106
logger .info (f"Watched file '{ src_path } ' has been modified" )
77
- self ._reload (self ._files [src_path ])
107
+ self ._trigger (self ._files [src_path ])
78
108
79
109
_files_watchdog : Optional ["FilesWatchdog" ] = None
80
110
@@ -104,7 +134,7 @@ def stop(self) -> None:
104
134
self ._observer .join ()
105
135
106
136
107
- @only_on_real_changes_update (tls_cert_files_config )
137
+ @only_on_real_changes_update (watched_files_config )
108
138
async def _init_files_watchdog (config : KresConfig ) -> None :
109
139
if WATCHDOG_LIB :
110
140
global _files_watchdog
@@ -119,12 +149,21 @@ async def _init_files_watchdog(config: KresConfig) -> None:
119
149
files_to_watch [config .network .tls .cert_file .to_path ()] = net_tls
120
150
files_to_watch [config .network .tls .key_file .to_path ()] = net_tls
121
151
152
+ # local-data.rpz
153
+ if config .local_data .rpz :
154
+ for rpz in config .local_data .rpz :
155
+ if rpz .watchdog :
156
+ files_to_watch [rpz .file .to_path ()] = None
157
+
122
158
if files_to_watch :
123
159
logger .info ("Initializing files watchdog" )
124
160
_files_watchdog = FilesWatchdog (files_to_watch )
125
161
_files_watchdog .start ()
126
162
127
163
128
164
async def init_files_watchdog (config_store : ConfigStore ) -> None :
165
+ global _config_store
166
+ _config_store = config_store
167
+
129
168
# register files watchdog callback
130
169
await config_store .register_on_change_callback (_init_files_watchdog )
0 commit comments