Skip to content

Commit

Permalink
fix winsw command by reinstalling
Browse files Browse the repository at this point in the history
  • Loading branch information
et-nik committed Feb 27, 2024
1 parent 28a9543 commit 79f7a44
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion internal/processmanager/winsw.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,23 @@ func (pm *WinSW) command(

result, err = pm.runWinSWCommand(ctx, command, server, out)
if err != nil {
return domain.ErrorResult, errors.WithMessage(err, "failed to exec command")
if command == "start" {
logger.Warn(ctx, errors.WithMessage(err, "failed to run command"))
result, err = pm.tryFixReinstallService(ctx, server, out)
if err != nil {
return domain.ErrorResult, errors.WithMessage(err, "failed to try fix by reinstalling service")
}
if result != domain.SuccessResult {
return domain.ErrorResult, errors.New("failed to try fix by reinstalling service")
}

result, err = pm.runWinSWCommand(ctx, command, server, out)
if err != nil {
return domain.ErrorResult, errors.WithMessage(err, "failed to exec command")
}
} else {
return domain.ErrorResult, errors.WithMessage(err, "failed to exec command")
}
}

return result, nil
Expand Down Expand Up @@ -199,6 +215,26 @@ func (pm *WinSW) SendInput(
return domain.ErrorResult, errors.New("input is not supported on Windows")
}

func (pm *WinSW) tryFixReinstallService(
ctx context.Context, server *domain.Server, out io.Writer,
) (domain.Result, error) {
result, err := pm.runWinSWCommand(ctx, "uninstall", server, out)
if err != nil {
logger.Warn(ctx, errors.WithMessage(err, "failed to uninstall service"))
}

result, err = pm.runWinSWCommand(ctx, "install", server, out)
if err != nil {
logger.Warn(ctx, errors.WithMessage(err, "failed to install service"))
}

if result != domain.SuccessResult {
return domain.ErrorResult, errors.New("failed to install service")
}

return domain.SuccessResult, nil
}

func checkUser(name string) error {
if name == "" {
return ErrEmptyUser
Expand Down

0 comments on commit 79f7a44

Please sign in to comment.