cmd/storagenode: use the right subcommand for restarting windows services

Commit 3cf89633e9 is changed how the cobra subcommands are created for storagenode (with prefering local variables instead of package level variables).

However, there is a bug which makes it impossible to restart Storagenode services on Windows: the refactored code creates the rootCmd/runCmd twice: therefore the ctx of the running process is not exactly the same as the ctx which supposed to be stopped / cancelled.

This patch fixes this problem with re-using exising, initialized command instead of creating a new one for cancellation.

Fixes: https://github.com/storj/storj/issues/5845

Change-Id: Ib8a4b80d4574e448f65c8558e927c0908c9c5eed
This commit is contained in:
Márton Elek 2023-05-08 13:09:18 +02:00 committed by Elek, Márton
parent 5977357a60
commit 56dbe7738d
3 changed files with 26 additions and 15 deletions

View File

@ -16,13 +16,13 @@ import (
func main() {
process.SetHardcodedApplicationName("storagenode")
if startAsService() {
return
}
allowDefaults := !isFilewalkerCommand()
rootCmd, _ := newRootCmd(allowDefaults)
if startAsService(rootCmd) {
return
}
loggerFunc := func(logger *zap.Logger) *zap.Logger {
return logger.With(zap.String("process", rootCmd.Use))
}

View File

@ -6,6 +6,8 @@
package main
func startAsService() bool {
import "github.com/spf13/cobra"
func startAsService(*cobra.Command) bool {
return false
}

View File

@ -23,9 +23,7 @@ import (
"storj.io/private/process"
)
var rootCmd, runCmd *cobra.Command
func startAsService() bool {
func startAsService(cmd *cobra.Command) bool {
isService, err := svc.IsWindowsService()
if err != nil {
zap.L().Fatal("Failed to determine if session is a service.", zap.Error(err))
@ -43,12 +41,10 @@ func startAsService() bool {
return false
}
var factory *Factory
rootCmd, factory = newRootCmd(true)
runCmd = newRunCmd(factory)
// Initialize the Windows Service handler
err = svc.Run("storagenode", &service{})
err = svc.Run("storagenode", &service{
rootCmd: cmd,
})
if err != nil {
zap.L().Fatal("Service failed.", zap.Error(err))
}
@ -56,16 +52,29 @@ func startAsService() bool {
return true
}
type service struct{}
type service struct {
rootCmd *cobra.Command
}
func (m *service) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
var runCmd *cobra.Command
for _, c := range m.rootCmd.Commands() {
if c.Use == "run" {
runCmd = c
}
}
if runCmd == nil {
panic("Assertion is failed: 'run' sub-command is not found.")
}
changes <- svc.Status{State: svc.StartPending}
var group errgroup.Group
group.Go(func() error {
process.Exec(rootCmd)
process.Exec(m.rootCmd)
return nil
})
defer func() { _ = group.Wait() }()