From ad724463a58a08b3af82abeb20537a5035740cd2 Mon Sep 17 00:00:00 2001 From: eNV25 Date: Mon, 4 Sep 2023 17:32:04 +0200 Subject: [PATCH] cmd: refactor and use sysdnotify in more commands * cmd: refactor and use sysdnotify in more commands Fixes #5117 --- cmd/rcd/rcd.go | 19 ++----------------- cmd/serve/dlna/dlna.go | 2 ++ cmd/serve/http/http.go | 2 ++ cmd/serve/restic/restic.go | 12 ++---------- cmd/serve/sftp/sftp.go | 2 ++ cmd/serve/webdav/webdav.go | 2 ++ lib/systemd/doc.go | 2 ++ lib/systemd/notify.go | 32 ++++++++++++++++++++++++++++++++ 8 files changed, 46 insertions(+), 27 deletions(-) create mode 100644 lib/systemd/doc.go create mode 100644 lib/systemd/notify.go diff --git a/cmd/rcd/rcd.go b/cmd/rcd/rcd.go index 7565bbe11..c023931f0 100644 --- a/cmd/rcd/rcd.go +++ b/cmd/rcd/rcd.go @@ -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() }, } diff --git a/cmd/serve/dlna/dlna.go b/cmd/serve/dlna/dlna.go index 7ee70c819..2c25918e6 100644 --- a/cmd/serve/dlna/dlna.go +++ b/cmd/serve/dlna/dlna.go @@ -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 }) diff --git a/cmd/serve/http/http.go b/cmd/serve/http/http.go index bbcf6f448..6af35ccd9 100644 --- a/cmd/serve/http/http.go +++ b/cmd/serve/http/http.go @@ -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 }) diff --git a/cmd/serve/restic/restic.go b/cmd/serve/restic/restic.go index de06de0ac..be56fe947 100644 --- a/cmd/serve/restic/restic.go +++ b/cmd/serve/restic/restic.go @@ -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 ` + "`//`" + `. } 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 }) }, diff --git a/cmd/serve/sftp/sftp.go b/cmd/serve/sftp/sftp.go index 10986b5ae..117f3ff61 100644 --- a/cmd/serve/sftp/sftp.go +++ b/cmd/serve/sftp/sftp.go @@ -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 }) diff --git a/cmd/serve/webdav/webdav.go b/cmd/serve/webdav/webdav.go index 9340bb3d4..f482d13d5 100644 --- a/cmd/serve/webdav/webdav.go +++ b/cmd/serve/webdav/webdav.go @@ -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 }) diff --git a/lib/systemd/doc.go b/lib/systemd/doc.go new file mode 100644 index 000000000..5786e6552 --- /dev/null +++ b/lib/systemd/doc.go @@ -0,0 +1,2 @@ +// Package systemd contains utilities for communication with the systemd service manager. +package systemd diff --git a/lib/systemd/notify.go b/lib/systemd/notify.go new file mode 100644 index 000000000..a185b71a6 --- /dev/null +++ b/lib/systemd/notify.go @@ -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() + } +}