cache: use atexit for cleanup

This commit is contained in:
remusb
2018-01-30 22:35:53 +02:00
parent ed2d4ef4a2
commit b3d8b7e22e
2 changed files with 20 additions and 7 deletions

View File

@@ -23,6 +23,7 @@ import (
"github.com/ncw/rclone/fs/config/obscure"
"github.com/ncw/rclone/fs/hash"
"github.com/ncw/rclone/fs/walk"
"github.com/ncw/rclone/lib/atexit"
"github.com/pkg/errors"
"golang.org/x/net/context"
"golang.org/x/time/rate"
@@ -325,14 +326,14 @@ func NewFs(name, rootPath string) (fs.Fs, error) {
}
// Trap SIGINT and SIGTERM to close the DB handle gracefully
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
signal.Notify(c, syscall.SIGHUP)
atexit.Register(func() {
f.StopBackgroundRunners()
})
go func() {
for {
s := <-c
if s == syscall.SIGINT || s == syscall.SIGTERM {
fs.Debugf(f, "Got signal: %v", s)
f.StopBackgroundRunners()
} else if s == syscall.SIGHUP {
if s == syscall.SIGHUP {
fs.Infof(f, "Clearing cache from signal")
f.DirCacheFlush()
}
@@ -1245,7 +1246,7 @@ func (f *Fs) CleanUpCache(ignoreLastTs bool) {
// can be triggered from a terminate signal or from testing between runs
func (f *Fs) StopBackgroundRunners() {
f.cleanupChan <- false
if f.tempWritePath != "" {
if f.tempWritePath != "" && f.backgroundRunner != nil && f.backgroundRunner.isRunning() {
f.backgroundRunner.close()
}
f.cache.Close()