git-bug/util/interrupt/cleaner.go

79 lines
1.6 KiB
Go
Raw Normal View History

package interrupt
2018-10-24 00:00:55 +03:00
import (
"fmt"
"os"
"os/signal"
2019-08-30 13:17:29 +03:00
"sync"
2018-10-24 00:00:55 +03:00
"syscall"
)
2019-08-30 13:17:29 +03:00
// CleanerFunc is a function to be executed when an interrupt trigger
type CleanerFunc func() error
// CancelFunc, if called, will disable the associated cleaner.
// This allow to create temporary cleaner. Be mindful though to not
// create too much of them as they are just disabled, not removed from
// memory.
type CancelFunc func()
type wrapper struct {
f CleanerFunc
disabled bool
}
var mu sync.Mutex
var cleaners []*wrapper
var handlerCreated = false
// RegisterCleaner is responsible for registering a cleaner function.
// When a function is registered, the Signal watcher is started in a goroutine.
func RegisterCleaner(cleaner CleanerFunc) CancelFunc {
mu.Lock()
defer mu.Unlock()
w := &wrapper{f: cleaner}
cancel := func() { w.disabled = true }
// prepend to later execute then in reverse order
cleaners = append([]*wrapper{w}, cleaners...)
if handlerCreated {
return cancel
2018-10-24 00:00:55 +03:00
}
2019-08-30 13:17:29 +03:00
handlerCreated = true
go func() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-ch
// Prevent un-terminated ^C character in terminal
fmt.Println()
errl := clean()
for _, err := range errl {
_, _ = fmt.Fprintln(os.Stderr, err)
}
os.Exit(1)
}()
return cancel
2018-10-24 00:00:55 +03:00
}
2018-10-27 12:51:50 +03:00
// clean invokes all registered cleanup functions, and returns a list of errors, if they exist.
2019-08-30 13:17:29 +03:00
func clean() (errorList []error) {
mu.Lock()
defer mu.Unlock()
for _, cleaner := range cleaners {
if cleaner.disabled {
continue
}
err := cleaner.f()
if err != nil {
2019-08-30 13:17:29 +03:00
errorList = append(errorList, err)
}
2018-10-24 00:00:55 +03:00
}
2019-08-30 13:17:29 +03:00
cleaners = []*wrapper{}
return
2018-10-24 00:00:55 +03:00
}