Skip to content

Commit

Permalink
watchdog
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Feb 26, 2024
1 parent 2569bd3 commit 7097891
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
30 changes: 27 additions & 3 deletions cmd/crowdsec/serve.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"os"
"os/signal"
Expand Down Expand Up @@ -239,6 +240,22 @@ func drainChan(c chan types.Event) {
}
}

// watchdog sends a watchdog signal to systemd every 15 seconds
func watchdog(ctx context.Context, logger log.FieldLogger) {
ticker := time.NewTicker(15 * time.Second)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return

Check warning on line 251 in cmd/crowdsec/serve.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/serve.go#L250-L251

Added lines #L250 - L251 were not covered by tests
case <-ticker.C:
csdaemon.Notify(csdaemon.Watchdog, logger)
}
}

}

func HandleSignals(cConfig *csconfig.Config) error {
var (
newConfig *csconfig.Config
Expand All @@ -263,14 +280,21 @@ func HandleSignals(cConfig *csconfig.Config) error {
go func() {
defer trace.CatchPanic("crowdsec/HandleSignals")

csdaemon.Notify(csdaemon.Ready, log.StandardLogger())
logger := log.StandardLogger()

csdaemon.Notify(csdaemon.Ready, logger)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

go watchdog(ctx, logger)

for s := range signalChan {
switch s {
// kill -SIGHUP XXXX
case syscall.SIGHUP:
log.Warning("SIGHUP received, reloading")
csdaemon.Notify(csdaemon.Reloading, log.StandardLogger())
csdaemon.Notify(csdaemon.Reloading, logger)

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

if err = shutdown(cConfig); err != nil {
exitChan <- fmt.Errorf("failed shutdown: %w", err)
Expand Down
1 change: 1 addition & 0 deletions config/crowdsec.service
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ExecReload=/usr/local/bin/crowdsec -c /etc/crowdsec/config.yaml -t -error
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
RestartSec=60
WatchdogSec=30

[Install]
WantedBy=multi-user.target
1 change: 1 addition & 0 deletions debian/crowdsec.service
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ExecReload=/usr/bin/crowdsec -c /etc/crowdsec/config.yaml -t -error
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
RestartSec=60
WatchdogSec=30

[Install]
WantedBy=multi-user.target

0 comments on commit 7097891

Please sign in to comment.