Skip to content

Commit 2c9b689

Browse files
committed
watchdog
1 parent 2569bd3 commit 2c9b689

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

cmd/crowdsec/serve.go

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56
"os"
67
"os/signal"
@@ -239,6 +240,22 @@ func drainChan(c chan types.Event) {
239240
}
240241
}
241242

243+
// watchdog sends a watchdog signal to systemd every 15 seconds
244+
func watchdog(ctx context.Context) {
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, log.StandardLogger())
254+
}
255+
}
256+
257+
}
258+
242259
func HandleSignals(cConfig *csconfig.Config) error {
243260
var (
244261
newConfig *csconfig.Config
@@ -263,14 +280,21 @@ func HandleSignals(cConfig *csconfig.Config) error {
263280
go func() {
264281
defer trace.CatchPanic("crowdsec/HandleSignals")
265282

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)
267291

268292
for s := range signalChan {
269293
switch s {
270294
// kill -SIGHUP XXXX
271295
case syscall.SIGHUP:
272296
log.Warning("SIGHUP received, reloading")
273-
csdaemon.Notify(csdaemon.Reloading, log.StandardLogger())
297+
csdaemon.Notify(csdaemon.Reloading, logger)
274298

275299
if err = shutdown(cConfig); err != nil {
276300
exitChan <- fmt.Errorf("failed shutdown: %w", err)
@@ -288,7 +312,7 @@ func HandleSignals(cConfig *csconfig.Config) error {
288312
// ctrl+C, kill -SIGINT XXXX, kill -SIGTERM XXXX
289313
case os.Interrupt, syscall.SIGTERM:
290314
log.Warning("SIGTERM received, shutting down")
291-
csdaemon.Notify(csdaemon.Stopping, log.StandardLogger())
315+
csdaemon.Notify(csdaemon.Stopping, logger)
292316

293317
if err = shutdown(cConfig); err != nil {
294318
exitChan <- fmt.Errorf("failed shutdown: %w", err)

0 commit comments

Comments
 (0)