mirror of
https://github.com/restic/restic.git
synced 2024-12-27 02:08:37 +00:00
Allow more than one cleanup handler
This commit is contained in:
parent
b6b1f41f2e
commit
3cabadab43
2 changed files with 60 additions and 17 deletions
59
cmd/restic/cleanup.go
Normal file
59
cmd/restic/cleanup.go
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"sync"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/restic/restic/debug"
|
||||||
|
)
|
||||||
|
|
||||||
|
var cleanupHandlers struct {
|
||||||
|
sync.Mutex
|
||||||
|
list []func() error
|
||||||
|
}
|
||||||
|
|
||||||
|
var stderr = os.Stderr
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
c := make(chan os.Signal)
|
||||||
|
signal.Notify(c, syscall.SIGINT)
|
||||||
|
|
||||||
|
go CleanupHandler(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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() error) {
|
||||||
|
cleanupHandlers.Lock()
|
||||||
|
defer cleanupHandlers.Unlock()
|
||||||
|
|
||||||
|
cleanupHandlers.list = append(cleanupHandlers.list, f)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunCleanupHandlers runs all registered cleanup handlers
|
||||||
|
func RunCleanupHandlers() {
|
||||||
|
cleanupHandlers.Lock()
|
||||||
|
defer cleanupHandlers.Unlock()
|
||||||
|
|
||||||
|
for _, f := range cleanupHandlers.list {
|
||||||
|
err := f()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(stderr, "error in cleanup handler: %v\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// CleanupHandler handles the SIGINT signal.
|
||||||
|
func CleanupHandler(c <-chan os.Signal) {
|
||||||
|
for s := range c {
|
||||||
|
debug.Log("CleanupHandler", "signal %v received, cleaning up", s)
|
||||||
|
fmt.Println("\x1b[2KInterrupt received, cleaning up")
|
||||||
|
RunCleanupHandlers()
|
||||||
|
fmt.Println("exiting")
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,9 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/restic/restic"
|
"github.com/restic/restic"
|
||||||
|
@ -123,19 +121,5 @@ func unlockAll() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
c := make(chan os.Signal)
|
AddCleanupHandler(unlockAll)
|
||||||
signal.Notify(c, syscall.SIGINT)
|
|
||||||
|
|
||||||
go CleanupHandler(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
// CleanupHandler handles the SIGINT signal.
|
|
||||||
func CleanupHandler(c <-chan os.Signal) {
|
|
||||||
for s := range c {
|
|
||||||
debug.Log("CleanupHandler", "signal %v received, cleaning up", s)
|
|
||||||
fmt.Println("\x1b[2KInterrupt received, cleaning up")
|
|
||||||
unlockAll()
|
|
||||||
fmt.Println("exiting")
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue