11
11
12
12
logger = logging .getLogger (__name__ )
13
13
14
+ FilesToWatch = Dict [Path , Optional [str ]]
15
+
14
16
15
17
def tls_cert_files_config (config : KresConfig ) -> List [Any ]:
16
18
return [
@@ -20,9 +22,6 @@ def tls_cert_files_config(config: KresConfig) -> List[Any]:
20
22
]
21
23
22
24
23
- FilesToWatch = Dict [Path , str ]
24
-
25
-
26
25
if WATCHDOG_LIB :
27
26
from watchdog .events import (
28
27
FileSystemEvent ,
@@ -33,48 +32,65 @@ def tls_cert_files_config(config: KresConfig) -> List[Any]:
33
32
class FilesWatchdogEventHandler (FileSystemEventHandler ):
34
33
def __init__ (self , files : FilesToWatch ) -> None :
35
34
self ._files = files
36
- self ._timer : Optional [Timer ] = None
35
+ self ._policy_timer : Optional [Timer ] = None
36
+ self ._timers : Dict [str , Timer ] = {}
37
+
38
+ def _trigger (self , cmd : Optional [str ]) -> None :
39
+ def policy_reload () -> None :
40
+ logger .info ("Policy rules reloaded" )
41
+
42
+ if not cmd :
43
+ # skipping if reload was already triggered
44
+ if self ._policy_timer and self ._policy_timer .is_alive ():
45
+ logger .info ("Skipping reloading policy rules, it was already triggered" )
46
+ return
47
+ # start a 5sec timer
48
+ logger .info ("Delayed policy rules reload has started" )
49
+ self ._policy_timer = Timer (5 , policy_reload )
50
+ self ._policy_timer .start ()
51
+ return
37
52
38
- def _reload (self , cmd : str ) -> None :
39
53
def command () -> None :
40
54
if compat .asyncio .is_event_loop_running ():
41
55
compat .asyncio .create_task (command_registered_workers (cmd ))
42
56
else :
43
57
compat .asyncio .run (command_registered_workers (cmd ))
44
- logger .info ("Reloading of TLS certificate files has finished" )
58
+ logger .info (f"Sending ' { cmd } ' command to reload watched files has finished" )
45
59
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" )
60
+ # skipping if command was already triggered
61
+ if cmd in self ._timers and self ._timers [ cmd ] .is_alive ():
62
+ logger .info (f "Skipping sending ' { cmd } ' command, it was already triggered" )
49
63
return
50
64
# 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 ()
65
+ logger .info (f "Delayed send of ' { cmd } ' command has started" )
66
+ self ._timers [ cmd ] = Timer (5 , command )
67
+ self ._timers [ cmd ] .start ()
54
68
55
69
def on_created (self , event : FileSystemEvent ) -> None :
56
70
src_path = Path (str (event .src_path ))
57
71
if src_path in self ._files .keys ():
58
72
logger .info (f"Watched file '{ src_path } ' has been created" )
59
- self ._reload (self ._files [src_path ])
73
+ self ._trigger (self ._files [src_path ])
60
74
61
75
def on_deleted (self , event : FileSystemEvent ) -> None :
62
76
src_path = Path (str (event .src_path ))
63
77
if src_path in self ._files .keys ():
64
78
logger .warning (f"Watched file '{ src_path } ' has been deleted" )
65
- if self ._timer :
66
- self ._timer .cancel ()
79
+ cmd = self ._files [src_path ]
80
+ if cmd in self ._timers :
81
+ self ._timers [cmd ].cancel ()
67
82
for file in self ._files .keys ():
68
83
if file .parent == src_path :
69
84
logger .warning (f"Watched directory '{ src_path } ' has been deleted" )
70
- if self ._timer :
71
- self ._timer .cancel ()
85
+ cmd = self ._files [file ]
86
+ if cmd in self ._timers :
87
+ self ._timers [cmd ].cancel ()
72
88
73
89
def on_modified (self , event : FileSystemEvent ) -> None :
74
90
src_path = Path (str (event .src_path ))
75
91
if src_path in self ._files .keys ():
76
92
logger .info (f"Watched file '{ src_path } ' has been modified" )
77
- self ._reload (self ._files [src_path ])
93
+ self ._trigger (self ._files [src_path ])
78
94
79
95
_files_watchdog : Optional ["FilesWatchdog" ] = None
80
96
@@ -119,6 +135,12 @@ async def _init_files_watchdog(config: KresConfig) -> None:
119
135
files_to_watch [config .network .tls .cert_file .to_path ()] = net_tls
120
136
files_to_watch [config .network .tls .key_file .to_path ()] = net_tls
121
137
138
+ # local-data.rpz
139
+ if config .local_data .rpz :
140
+ for rpz in config .local_data .rpz :
141
+ if rpz .watchdog :
142
+ files_to_watch [rpz .file .to_path ()] = None
143
+
122
144
if files_to_watch :
123
145
logger .info ("Initializing files watchdog" )
124
146
_files_watchdog = FilesWatchdog (files_to_watch )
0 commit comments