1
mirror of https://github.com/rclone/rclone synced 2024-11-23 00:06:55 +01:00

atexit: add Signalled() function - set if called from a signal #4804

This commit is contained in:
Nick Craig-Wood 2020-11-27 10:49:24 +00:00
parent 1cfce703b2
commit 83406bc473

View File

@ -8,6 +8,7 @@ import (
"os" "os"
"os/signal" "os/signal"
"sync" "sync"
"sync/atomic"
"github.com/rclone/rclone/fs" "github.com/rclone/rclone/fs"
) )
@ -18,6 +19,7 @@ var (
exitChan chan os.Signal exitChan chan os.Signal
exitOnce sync.Once exitOnce sync.Once
registerOnce sync.Once registerOnce sync.Once
signalled int32
) )
// FnHandle is the type of the handle returned by function `Register` // FnHandle is the type of the handle returned by function `Register`
@ -40,6 +42,7 @@ func Register(fn func()) FnHandle {
if sig == nil { if sig == nil {
return return
} }
atomic.StoreInt32(&signalled, 1)
fs.Infof(nil, "Signal received: %s", sig) fs.Infof(nil, "Signal received: %s", sig)
Run() Run()
fs.Infof(nil, "Exiting...") fs.Infof(nil, "Exiting...")
@ -50,6 +53,11 @@ func Register(fn func()) FnHandle {
return &fn return &fn
} }
// Signalled returns true if an exit signal has been received
func Signalled() bool {
return atomic.LoadInt32(&signalled) != 0
}
// Unregister a function using the handle returned by `Register` // Unregister a function using the handle returned by `Register`
func Unregister(handle FnHandle) { func Unregister(handle FnHandle) {
fnsMutex.Lock() fnsMutex.Lock()