2018-01-25 18:10:21 +08:00
|
|
|
// Package atexit provides handling for functions you want called when
|
|
|
|
// the program exits unexpectedly due to a signal.
|
|
|
|
//
|
|
|
|
// You should also make sure you call Run in the normal exit path.
|
|
|
|
package atexit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
fns []func()
|
2018-05-12 17:40:44 +08:00
|
|
|
exitChan chan os.Signal
|
2018-01-25 18:10:21 +08:00
|
|
|
exitOnce sync.Once
|
|
|
|
registerOnce sync.Once
|
|
|
|
)
|
|
|
|
|
|
|
|
// Register a function to be called on exit
|
|
|
|
func Register(fn func()) {
|
|
|
|
fns = append(fns, fn)
|
|
|
|
// Run AtExit handlers on SIGINT or SIGTERM so everything gets
|
|
|
|
// tidied up properly
|
|
|
|
registerOnce.Do(func() {
|
2018-05-12 17:40:44 +08:00
|
|
|
exitChan = make(chan os.Signal, 1)
|
|
|
|
signal.Notify(exitChan, os.Interrupt) // syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT
|
2018-01-25 18:10:21 +08:00
|
|
|
go func() {
|
2018-05-12 17:40:44 +08:00
|
|
|
sig, closed := <-exitChan
|
2018-05-13 00:59:25 +08:00
|
|
|
if closed || sig == nil {
|
2018-05-12 17:40:44 +08:00
|
|
|
return
|
|
|
|
}
|
2018-01-25 18:10:21 +08:00
|
|
|
fs.Infof(nil, "Signal received: %s", sig)
|
|
|
|
Run()
|
|
|
|
fs.Infof(nil, "Exiting...")
|
|
|
|
os.Exit(0)
|
|
|
|
}()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-05-12 17:40:44 +08:00
|
|
|
// IgnoreSignals disables the signal handler and prevents Run from beeing executed automatically
|
|
|
|
func IgnoreSignals() {
|
|
|
|
registerOnce.Do(func() {})
|
|
|
|
if exitChan != nil {
|
|
|
|
signal.Stop(exitChan)
|
|
|
|
close(exitChan)
|
|
|
|
exitChan = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 18:10:21 +08:00
|
|
|
// Run all the at exit functions if they haven't been run already
|
|
|
|
func Run() {
|
|
|
|
exitOnce.Do(func() {
|
|
|
|
for _, fn := range fns {
|
|
|
|
fn()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|