mirror of
https://github.com/octoleo/restic.git
synced 2024-11-22 21:05:10 +00:00
remove global shutdown hook
This commit is contained in:
parent
699ef5e9de
commit
6f2a4dea21
@ -1,64 +1,27 @@
|
|||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddCleanupHandler adds the function f to the list of cleanup handlers so
|
|
||||||
// that it is executed when all the cleanup handlers are run, e.g. when SIGINT
|
|
||||||
// is received.
|
|
||||||
func AddCleanupHandler(f func(code int) (int, error)) {
|
|
||||||
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 {
|
|
||||||
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.
|
|
||||||
func CleanupHandler(c <-chan os.Signal) {
|
|
||||||
for s := range c {
|
|
||||||
debug.Log("signal %v received, cleaning up", s)
|
debug.Log("signal %v received, cleaning up", s)
|
||||||
Warnf("%ssignal %v received, cleaning up\n", clearLine(0), s)
|
Warnf("%ssignal %v received, cleaning up\n", clearLine(0), s)
|
||||||
|
|
||||||
@ -68,22 +31,11 @@ func CleanupHandler(c <-chan os.Signal) {
|
|||||||
_, _ = os.Stderr.WriteString("\n--- STACKTRACE END ---\n")
|
_, _ = os.Stderr.WriteString("\n--- STACKTRACE END ---\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
code := 0
|
cancel()
|
||||||
|
|
||||||
if s == syscall.SIGINT || s == syscall.SIGTERM {
|
|
||||||
code = 130
|
|
||||||
} else {
|
|
||||||
code = 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Exit(code)
|
// Exit terminates the process with the given 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 New Issue
Block a user