2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-04 02:48:21 +00:00
restic/cmd/restic/cleanup.go

42 lines
1010 B
Go
Raw Normal View History

2015-07-19 15:50:55 +00:00
package main
import (
2024-03-29 22:58:48 +00:00
"context"
2015-07-19 15:50:55 +00:00
"os"
"os/signal"
"syscall"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/debug"
2015-07-19 15:50:55 +00:00
)
2024-03-29 22:58:48 +00:00
func createGlobalContext() context.Context {
ctx, cancel := context.WithCancel(context.Background())
2015-07-19 15:50:55 +00:00
2024-03-29 22:58:48 +00:00
ch := make(chan os.Signal, 1)
go cleanupHandler(ch, cancel)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
2015-07-19 15:50:55 +00:00
2024-03-29 22:58:48 +00:00
return ctx
2015-07-19 15:50:55 +00:00
}
2024-03-29 22:58:48 +00:00
// cleanupHandler handles the SIGINT and SIGTERM signals.
func cleanupHandler(c <-chan os.Signal, cancel context.CancelFunc) {
s := <-c
debug.Log("signal %v received, cleaning up", s)
Warnf("%ssignal %v received, cleaning up\n", clearLine(0), s)
2015-07-19 15:57:18 +00:00
2024-03-29 22:58:48 +00:00
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")
2015-07-19 15:50:55 +00:00
}
2024-03-29 22:58:48 +00:00
cancel()
2015-07-19 15:50:55 +00:00
}
2024-03-29 22:58:48 +00:00
// Exit terminates the process with the given exit code.
func Exit(code int) {
debug.Log("exiting with status code %d", code)
os.Exit(code)
}