cmd: refactor and use sysdnotify in more commands

* cmd: refactor and use sysdnotify in more commands

Fixes #5117
This commit is contained in:
eNV25 2023-09-04 17:32:04 +02:00 committed by GitHub
parent 6afd7088d3
commit ad724463a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 46 additions and 27 deletions

View File

@ -4,14 +4,12 @@ package rcd
import (
"context"
"log"
"sync"
sysdnotify "github.com/iguanesolutions/go-systemd/v5/notify"
"github.com/rclone/rclone/cmd"
"github.com/rclone/rclone/fs/rc/rcflags"
"github.com/rclone/rclone/fs/rc/rcserver"
"github.com/rclone/rclone/lib/atexit"
libhttp "github.com/rclone/rclone/lib/http"
"github.com/rclone/rclone/lib/systemd"
"github.com/spf13/cobra"
)
@ -58,21 +56,8 @@ See the [rc documentation](/rc/) for more info on the rc flags.
}
// Notify stopping on exit
var finaliseOnce sync.Once
finalise := func() {
finaliseOnce.Do(func() {
_ = sysdnotify.Stopping()
})
}
fnHandle := atexit.Register(finalise)
defer atexit.Unregister(fnHandle)
// Notify ready to systemd
if err := sysdnotify.Ready(); err != nil {
log.Fatalf("failed to notify ready to systemd: %v", err)
}
defer systemd.Notify()()
s.Wait()
finalise()
},
}

View File

@ -22,6 +22,7 @@ import (
"github.com/rclone/rclone/cmd/serve/dlna/data"
"github.com/rclone/rclone/cmd/serve/dlna/dlnaflags"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/lib/systemd"
"github.com/rclone/rclone/vfs"
"github.com/rclone/rclone/vfs/vfsflags"
"github.com/spf13/cobra"
@ -64,6 +65,7 @@ files that they are not able to play back correctly.
if err := s.Serve(); err != nil {
return err
}
defer systemd.Notify()()
s.Wait()
return nil
})

View File

@ -22,6 +22,7 @@ import (
"github.com/rclone/rclone/fs/accounting"
libhttp "github.com/rclone/rclone/lib/http"
"github.com/rclone/rclone/lib/http/serve"
"github.com/rclone/rclone/lib/systemd"
"github.com/rclone/rclone/vfs"
"github.com/rclone/rclone/vfs/vfsflags"
"github.com/spf13/cobra"
@ -92,6 +93,7 @@ control the stats printing.
log.Fatal(err)
}
defer systemd.Notify()()
s.server.Wait()
return nil
})

View File

@ -13,8 +13,6 @@ import (
"strings"
"time"
sysdnotify "github.com/iguanesolutions/go-systemd/v5/notify"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/rclone/rclone/cmd"
@ -25,6 +23,7 @@ import (
"github.com/rclone/rclone/fs/walk"
libhttp "github.com/rclone/rclone/lib/http"
"github.com/rclone/rclone/lib/http/serve"
"github.com/rclone/rclone/lib/systemd"
"github.com/rclone/rclone/lib/terminal"
"github.com/spf13/cobra"
"golang.org/x/net/http2"
@ -180,16 +179,9 @@ with a path of ` + "`/<username>/`" + `.
}
fs.Logf(s.f, "Serving restic REST API on %s", s.URLs())
if err := sysdnotify.Ready(); err != nil {
fs.Logf(s.f, "failed to notify ready to systemd: %v", err)
}
defer systemd.Notify()()
s.Wait()
if err := sysdnotify.Stopping(); err != nil {
fs.Logf(s.f, "failed to notify stopping to systemd: %v", err)
}
return nil
})
},

View File

@ -13,6 +13,7 @@ import (
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/flags"
"github.com/rclone/rclone/fs/rc"
"github.com/rclone/rclone/lib/systemd"
"github.com/rclone/rclone/vfs"
"github.com/rclone/rclone/vfs/vfsflags"
"github.com/spf13/cobra"
@ -135,6 +136,7 @@ provided by OpenSSH in this case.
if err != nil {
return err
}
defer systemd.Notify()()
s.Wait()
return nil
})

View File

@ -24,6 +24,7 @@ import (
"github.com/rclone/rclone/fs/hash"
libhttp "github.com/rclone/rclone/lib/http"
"github.com/rclone/rclone/lib/http/serve"
"github.com/rclone/rclone/lib/systemd"
"github.com/rclone/rclone/vfs"
"github.com/rclone/rclone/vfs/vfsflags"
"github.com/spf13/cobra"
@ -145,6 +146,7 @@ https://learn.microsoft.com/en-us/office/troubleshoot/powerpoint/office-opens-bl
if err != nil {
return err
}
defer systemd.Notify()()
s.Wait()
return nil
})

2
lib/systemd/doc.go Normal file
View File

@ -0,0 +1,2 @@
// Package systemd contains utilities for communication with the systemd service manager.
package systemd

32
lib/systemd/notify.go Normal file
View File

@ -0,0 +1,32 @@
package systemd
import (
"log"
"sync"
sysdnotify "github.com/iguanesolutions/go-systemd/v5/notify"
"github.com/rclone/rclone/lib/atexit"
)
// Notify systemd that the service is starting. This returns a
// function which should be called to notify that the service is
// stopping. This function will be called on exit if the service exits
// on a signal.
func Notify() func() {
if err := sysdnotify.Ready(); err != nil {
log.Printf("failed to notify ready to systemd: %v", err)
}
var finaliseOnce sync.Once
finalise := func() {
finaliseOnce.Do(func() {
if err := sysdnotify.Stopping(); err != nil {
log.Printf("failed to notify stopping to systemd: %v", err)
}
})
}
finaliseHandle := atexit.Register(finalise)
return func() {
atexit.Unregister(finaliseHandle)
finalise()
}
}