forked from TrueCloudLab/restic
remove global shutdown hook
This commit is contained in:
parent
699ef5e9de
commit
6f2a4dea21
3 changed files with 33 additions and 83 deletions
|
@ -1,89 +1,41 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"sync"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/restic/restic/internal/debug"
|
"github.com/restic/restic/internal/debug"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cleanupHandlers struct {
|
func createGlobalContext() context.Context {
|
||||||
sync.Mutex
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
list []func(code int) (int, error)
|
|
||||||
done bool
|
ch := make(chan os.Signal, 1)
|
||||||
ch chan os.Signal
|
go cleanupHandler(ch, cancel)
|
||||||
|
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
|
return ctx
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
// cleanupHandler handles the SIGINT and SIGTERM signals.
|
||||||
cleanupHandlers.ch = make(chan os.Signal, 1)
|
func cleanupHandler(c <-chan os.Signal, cancel context.CancelFunc) {
|
||||||
go CleanupHandler(cleanupHandlers.ch)
|
s := <-c
|
||||||
signal.Notify(cleanupHandlers.ch, syscall.SIGINT, syscall.SIGTERM)
|
debug.Log("signal %v received, cleaning up", s)
|
||||||
}
|
Warnf("%ssignal %v received, cleaning up\n", clearLine(0), s)
|
||||||
|
|
||||||
// AddCleanupHandler adds the function f to the list of cleanup handlers so
|
if val, _ := os.LookupEnv("RESTIC_DEBUG_STACKTRACE_SIGINT"); val != "" {
|
||||||
// that it is executed when all the cleanup handlers are run, e.g. when SIGINT
|
_, _ = os.Stderr.WriteString("\n--- STACKTRACE START ---\n\n")
|
||||||
// is received.
|
_, _ = os.Stderr.WriteString(debug.DumpStacktrace())
|
||||||
func AddCleanupHandler(f func(code int) (int, error)) {
|
_, _ = os.Stderr.WriteString("\n--- STACKTRACE END ---\n")
|
||||||
cleanupHandlers.Lock()
|
|
||||||
defer cleanupHandlers.Unlock()
|
|
||||||
|
|
||||||
// reset the done flag for integration tests
|
|
||||||
cleanupHandlers.done = false
|
|
||||||
|
|
||||||
cleanupHandlers.list = append(cleanupHandlers.list, f)
|
|
||||||
}
|
|
||||||
|
|
||||||
// RunCleanupHandlers runs all registered cleanup handlers
|
|
||||||
func RunCleanupHandlers(code int) int {
|
|
||||||
cleanupHandlers.Lock()
|
|
||||||
defer cleanupHandlers.Unlock()
|
|
||||||
|
|
||||||
if cleanupHandlers.done {
|
|
||||||
return code
|
|
||||||
}
|
}
|
||||||
cleanupHandlers.done = true
|
|
||||||
|
|
||||||
for _, f := range cleanupHandlers.list {
|
cancel()
|
||||||
var err error
|
|
||||||
code, err = f(code)
|
|
||||||
if err != nil {
|
|
||||||
Warnf("error in cleanup handler: %v\n", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cleanupHandlers.list = nil
|
|
||||||
return code
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CleanupHandler handles the SIGINT and SIGTERM signals.
|
// Exit terminates the process with the given exit code.
|
||||||
func CleanupHandler(c <-chan os.Signal) {
|
|
||||||
for s := range c {
|
|
||||||
debug.Log("signal %v received, cleaning up", s)
|
|
||||||
Warnf("%ssignal %v received, cleaning up\n", clearLine(0), s)
|
|
||||||
|
|
||||||
if val, _ := os.LookupEnv("RESTIC_DEBUG_STACKTRACE_SIGINT"); val != "" {
|
|
||||||
_, _ = os.Stderr.WriteString("\n--- STACKTRACE START ---\n\n")
|
|
||||||
_, _ = os.Stderr.WriteString(debug.DumpStacktrace())
|
|
||||||
_, _ = os.Stderr.WriteString("\n--- STACKTRACE END ---\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
code := 0
|
|
||||||
|
|
||||||
if s == syscall.SIGINT || s == syscall.SIGTERM {
|
|
||||||
code = 130
|
|
||||||
} else {
|
|
||||||
code = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
Exit(code)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Exit runs the cleanup handlers and then terminates the process with the
|
|
||||||
// given exit code.
|
|
||||||
func Exit(code int) {
|
func Exit(code int) {
|
||||||
code = RunCleanupHandlers(code)
|
|
||||||
debug.Log("exiting with status code %d", code)
|
debug.Log("exiting with status code %d", code)
|
||||||
os.Exit(code)
|
os.Exit(code)
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,8 +96,6 @@ var globalOptions = GlobalOptions{
|
||||||
stderr: os.Stderr,
|
stderr: os.Stderr,
|
||||||
}
|
}
|
||||||
|
|
||||||
var internalGlobalCtx context.Context
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
backends := location.NewRegistry()
|
backends := location.NewRegistry()
|
||||||
backends.Register(azure.NewFactory())
|
backends.Register(azure.NewFactory())
|
||||||
|
@ -111,15 +109,6 @@ func init() {
|
||||||
backends.Register(swift.NewFactory())
|
backends.Register(swift.NewFactory())
|
||||||
globalOptions.backends = backends
|
globalOptions.backends = backends
|
||||||
|
|
||||||
var cancel context.CancelFunc
|
|
||||||
internalGlobalCtx, cancel = context.WithCancel(context.Background())
|
|
||||||
AddCleanupHandler(func(code int) (int, error) {
|
|
||||||
// Must be called before the unlock cleanup handler to ensure that the latter is
|
|
||||||
// not blocked due to limited number of backend connections, see #1434
|
|
||||||
cancel()
|
|
||||||
return code, nil
|
|
||||||
})
|
|
||||||
|
|
||||||
f := cmdRoot.PersistentFlags()
|
f := cmdRoot.PersistentFlags()
|
||||||
f.StringVarP(&globalOptions.Repo, "repo", "r", "", "`repository` to backup to or restore from (default: $RESTIC_REPOSITORY)")
|
f.StringVarP(&globalOptions.Repo, "repo", "r", "", "`repository` to backup to or restore from (default: $RESTIC_REPOSITORY)")
|
||||||
f.StringVarP(&globalOptions.RepositoryFile, "repository-file", "", "", "`file` to read the repository location from (default: $RESTIC_REPOSITORY_FILE)")
|
f.StringVarP(&globalOptions.RepositoryFile, "repository-file", "", "", "`file` to read the repository location from (default: $RESTIC_REPOSITORY_FILE)")
|
||||||
|
|
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
@ -118,7 +119,13 @@ func main() {
|
||||||
debug.Log("main %#v", os.Args)
|
debug.Log("main %#v", os.Args)
|
||||||
debug.Log("restic %s compiled with %v on %v/%v",
|
debug.Log("restic %s compiled with %v on %v/%v",
|
||||||
version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
|
version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
|
||||||
err = cmdRoot.ExecuteContext(internalGlobalCtx)
|
|
||||||
|
ctx := createGlobalContext()
|
||||||
|
err = cmdRoot.ExecuteContext(ctx)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
err = ctx.Err()
|
||||||
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case restic.IsAlreadyLocked(err):
|
case restic.IsAlreadyLocked(err):
|
||||||
|
@ -140,11 +147,13 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
var exitCode int
|
var exitCode int
|
||||||
switch err {
|
switch {
|
||||||
case nil:
|
case err == nil:
|
||||||
exitCode = 0
|
exitCode = 0
|
||||||
case ErrInvalidSourceData:
|
case err == ErrInvalidSourceData:
|
||||||
exitCode = 3
|
exitCode = 3
|
||||||
|
case errors.Is(err, context.Canceled):
|
||||||
|
exitCode = 130
|
||||||
default:
|
default:
|
||||||
exitCode = 1
|
exitCode = 1
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue