mirror of
https://github.com/rclone/rclone.git
synced 2024-11-25 01:44:41 +01:00
cmd: refactor and use sysdnotify in more commands
* cmd: refactor and use sysdnotify in more commands Fixes #5117
This commit is contained in:
parent
6afd7088d3
commit
ad724463a5
@ -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()
|
||||
},
|
||||
}
|
||||
|
@ -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
|
||||
})
|
||||
|
@ -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
|
||||
})
|
||||
|
@ -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
|
||||
})
|
||||
},
|
||||
|
@ -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
|
||||
})
|
||||
|
@ -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
2
lib/systemd/doc.go
Normal 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
32
lib/systemd/notify.go
Normal 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()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user