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

View File

@ -561,6 +561,7 @@ type backgroundWriter struct {
stateCh chan int stateCh chan int
running bool running bool
notifyCh chan BackgroundUploadState notifyCh chan BackgroundUploadState
mu sync.Mutex
} }
func newBackgroundWriter(f *Fs) *backgroundWriter { func newBackgroundWriter(f *Fs) *backgroundWriter {
@ -575,6 +576,10 @@ func newBackgroundWriter(f *Fs) *backgroundWriter {
func (b *backgroundWriter) close() { func (b *backgroundWriter) close() {
b.stateCh <- 2 b.stateCh <- 2
b.mu.Lock()
defer b.mu.Unlock()
b.running = false
} }
func (b *backgroundWriter) pause() { func (b *backgroundWriter) pause() {
@ -585,6 +590,12 @@ func (b *backgroundWriter) play() {
b.stateCh <- 0 b.stateCh <- 0
} }
func (b *backgroundWriter) isRunning() bool {
b.mu.Lock()
defer b.mu.Unlock()
return b.running
}
func (b *backgroundWriter) notify(remote string, status int, err error) { func (b *backgroundWriter) notify(remote string, status int, err error) {
state := BackgroundUploadState{ state := BackgroundUploadState{
Remote: remote, Remote: remote,
@ -601,7 +612,9 @@ func (b *backgroundWriter) notify(remote string, status int, err error) {
func (b *backgroundWriter) run() { func (b *backgroundWriter) run() {
state := 0 state := 0
for { for {
b.mu.Lock()
b.running = true b.running = true
b.mu.Unlock()
select { select {
case s := <-b.stateCh: case s := <-b.stateCh:
state = s state = s
@ -614,7 +627,6 @@ func (b *backgroundWriter) run() {
time.Sleep(time.Millisecond * 500) time.Sleep(time.Millisecond * 500)
continue continue
case 2: case 2:
b.running = false
return return
} }