mirror of
https://github.com/garethgeorge/backrest.git
synced 2025-12-12 08:45:38 +00:00
feat: add force kill signal handler that dumps stacks
This commit is contained in:
25
backrest.go
25
backrest.go
@@ -9,8 +9,10 @@ import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
|
||||
"github.com/garethgeorge/backrest/gen/go/v1/v1connect"
|
||||
@@ -46,7 +48,8 @@ func main() {
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
go onterm(cancel)
|
||||
go onterm(os.Interrupt, cancel)
|
||||
go onterm(os.Interrupt, newForceKillHandler())
|
||||
|
||||
// Load the configuration
|
||||
configStore := createConfigProvider()
|
||||
@@ -146,11 +149,13 @@ func createConfigProvider() config.ConfigStore {
|
||||
}
|
||||
}
|
||||
|
||||
func onterm(callback func()) {
|
||||
func onterm(s os.Signal, callback func()) {
|
||||
sigchan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigchan, os.Interrupt, syscall.SIGTERM)
|
||||
signal.Notify(sigchan, s, syscall.SIGTERM)
|
||||
for {
|
||||
<-sigchan
|
||||
callback()
|
||||
}
|
||||
}
|
||||
|
||||
func getSecret() []byte {
|
||||
@@ -174,3 +179,17 @@ func getSecret() []byte {
|
||||
}
|
||||
return secret
|
||||
}
|
||||
|
||||
func newForceKillHandler() func() {
|
||||
var times atomic.Int32
|
||||
return func() {
|
||||
if times.Load() > 0 {
|
||||
buf := make([]byte, 1<<16)
|
||||
runtime.Stack(buf, true)
|
||||
os.Stderr.Write(buf)
|
||||
zap.S().Fatal("dumped all running coroutine stack traces, forcing termination")
|
||||
}
|
||||
times.Add(1)
|
||||
zap.S().Warn("attempting graceful shutdown, to force termination press Ctrl+C again")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ func NewRepoOrchestrator(config *v1.Config, repoConfig *v1.Repo, resticPath stri
|
||||
|
||||
var opts []restic.GenericOption
|
||||
opts = append(opts, restic.WithEnviron())
|
||||
opts = append(opts, restic.WithEnv("RESTIC_PROGRESS_FPS=0.5"))
|
||||
opts = append(opts, restic.WithEnv("RESTIC_PROGRESS_FPS=2"))
|
||||
|
||||
if env := repoConfig.GetEnv(); len(env) != 0 {
|
||||
for _, e := range env {
|
||||
|
||||
@@ -146,7 +146,7 @@ func (t *BackupTask) Run(ctx context.Context, st ScheduledTask, runner TaskRunne
|
||||
zap.S().Warnf("unexpected message type %q in backup progress entry", entry.MessageType)
|
||||
}
|
||||
|
||||
if time.Since(lastSent) < 1*time.Second {
|
||||
if time.Since(lastSent) <= 1000*time.Millisecond {
|
||||
return
|
||||
}
|
||||
lastSent = time.Now()
|
||||
|
||||
@@ -50,8 +50,6 @@ func indexSnapshotsHelper(ctx context.Context, st ScheduledTask, taskRunner Task
|
||||
t := st.Task
|
||||
oplog := taskRunner.OpLog()
|
||||
|
||||
config := taskRunner.Config()
|
||||
|
||||
repo, err := taskRunner.GetRepoOrchestrator(t.RepoID())
|
||||
if err != nil {
|
||||
return fmt.Errorf("couldn't get repo %q: %w", t.RepoID(), err)
|
||||
|
||||
Reference in New Issue
Block a user