1
1
package main
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
5
6
"os"
6
7
"os/signal"
@@ -239,6 +240,22 @@ func drainChan(c chan types.Event) {
239
240
}
240
241
}
241
242
243
+ // watchdog sends a watchdog signal to systemd every 15 seconds
244
+ func watchdog (ctx context.Context , logger log.FieldLogger ) {
245
+ ticker := time .NewTicker (15 * time .Second )
246
+ defer ticker .Stop ()
247
+
248
+ for {
249
+ select {
250
+ case <- ctx .Done ():
251
+ return
252
+ case <- ticker .C :
253
+ csdaemon .Notify (csdaemon .Watchdog , logger )
254
+ }
255
+ }
256
+
257
+ }
258
+
242
259
func HandleSignals (cConfig * csconfig.Config ) error {
243
260
var (
244
261
newConfig * csconfig.Config
@@ -263,14 +280,21 @@ func HandleSignals(cConfig *csconfig.Config) error {
263
280
go func () {
264
281
defer trace .CatchPanic ("crowdsec/HandleSignals" )
265
282
266
- csdaemon .Notify (csdaemon .Ready , log .StandardLogger ())
283
+ logger := log .StandardLogger ()
284
+
285
+ csdaemon .Notify (csdaemon .Ready , logger )
286
+
287
+ ctx , cancel := context .WithCancel (context .Background ())
288
+ defer cancel ()
289
+
290
+ go watchdog (ctx , logger )
267
291
268
292
for s := range signalChan {
269
293
switch s {
270
294
// kill -SIGHUP XXXX
271
295
case syscall .SIGHUP :
272
296
log .Warning ("SIGHUP received, reloading" )
273
- csdaemon .Notify (csdaemon .Reloading , log . StandardLogger () )
297
+ csdaemon .Notify (csdaemon .Reloading , logger )
274
298
275
299
if err = shutdown (cConfig ); err != nil {
276
300
exitChan <- fmt .Errorf ("failed shutdown: %w" , err )
@@ -288,7 +312,7 @@ func HandleSignals(cConfig *csconfig.Config) error {
288
312
// ctrl+C, kill -SIGINT XXXX, kill -SIGTERM XXXX
289
313
case os .Interrupt , syscall .SIGTERM :
290
314
log .Warning ("SIGTERM received, shutting down" )
291
- csdaemon .Notify (csdaemon .Stopping , log . StandardLogger () )
315
+ csdaemon .Notify (csdaemon .Stopping , logger )
292
316
293
317
if err = shutdown (cConfig ); err != nil {
294
318
exitChan <- fmt .Errorf ("failed shutdown: %w" , err )
0 commit comments