Skip to content

Commit

Permalink
fix winsw process manager
Browse files Browse the repository at this point in the history
  • Loading branch information
et-nik committed Feb 29, 2024
1 parent be9f8bf commit 6450fe3
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions internal/processmanager/winsw.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const (

outputSizeLimit = 30000

errorCodeCannotStart = 1053
errorCodeCannotStart = 1053
errorCodeServiceNotExist = 1060

commandInstall = "install"
commandRefresh = "refresh"
Expand All @@ -53,7 +54,7 @@ func NewWinSW(cfg *config.Config, _, detailedExecutor contracts.Executor) *WinSW
}

func (pm *WinSW) Install(ctx context.Context, server *domain.Server, out io.Writer) (domain.Result, error) {
createdNewService, err := pm.makeService(ctx, server)
createdNewService, err := pm.makeService(ctx, server, out)
if err != nil {
return domain.ErrorResult, errors.WithMessage(err, "failed to make service")
}
Expand Down Expand Up @@ -159,7 +160,7 @@ func (pm *WinSW) command(
return domain.ErrorResult, errors.WithMessage(err, "failed to check user")
}

createdNewService, err := pm.makeService(ctx, server)
createdNewService, err := pm.makeService(ctx, server, out)
if err != nil {
return domain.ErrorResult, errors.WithMessage(err, "failed to make service")
}
Expand All @@ -179,7 +180,7 @@ func (pm *WinSW) command(
if err != nil {
return domain.ErrorResult, errors.WithMessage(err, "failed to refresh service")
}
if result != domain.SuccessResult {
if result == errorCodeServiceNotExist {
logger.Warn(ctx, "failed to refresh service config, trying to install service")

result, err = pm.runWinSWCommand(ctx, commandInstall, server, out)
Expand All @@ -189,6 +190,8 @@ func (pm *WinSW) command(
if result != domain.SuccessResult {
return domain.ErrorResult, errors.New("failed to refresh and install service")
}
} else if result != domain.SuccessResult {
return domain.ErrorResult, errors.New("failed to refresh service")
}
}

Expand Down Expand Up @@ -284,10 +287,11 @@ func checkUser(name string) error {
return nil
}

func (pm *WinSW) makeService(ctx context.Context, server *domain.Server) (bool, error) {
func (pm *WinSW) makeService(ctx context.Context, server *domain.Server, out io.Writer) (bool, error) {
serviceFile := pm.serviceFile(server)

if _, err := os.Stat(servicesConfigPath); errors.Is(err, os.ErrNotExist) {
_, _ = out.Write([]byte("Creating directory " + servicesConfigPath + "\n"))
err := os.MkdirAll(servicesConfigPath, 0755)
if err != nil {
return false, errors.WithMessage(err, "failed to create directory")
Expand All @@ -301,6 +305,7 @@ func (pm *WinSW) makeService(ctx context.Context, server *domain.Server) (bool,
return false, errors.WithMessage(err, "failed to check file")
}
if err != nil && errors.Is(err, os.ErrNotExist) {
_, _ = out.Write([]byte("Service file not found\n"))
serviceFileNotExist = true
}

Expand All @@ -311,14 +316,20 @@ func (pm *WinSW) makeService(ctx context.Context, server *domain.Server) (bool,

// If service file exists, check if it's the same
if !serviceFileNotExist {
_, _ = out.Write([]byte("Service file exists\n"))
_, _ = out.Write([]byte("Checking if service configuration wasn't changed\n"))

oldServiceConfig, err := os.ReadFile(serviceFile)
if err != nil {
return false, errors.WithMessage(err, "failed to read file")
}

if string(oldServiceConfig) == serviceConfig {
_, _ = out.Write([]byte("Service configuration wasn't changed\n"))
return false, nil
}

_, _ = out.Write([]byte("Service configuration was changed\n"))
}

flag := os.O_TRUNC | os.O_WRONLY
Expand All @@ -342,6 +353,11 @@ func (pm *WinSW) makeService(ctx context.Context, server *domain.Server) (bool,
return false, errors.WithMessage(err, "failed to write to file")
}

err = f.Sync()
if err != nil {
return false, errors.WithMessage(err, "failed to sync file")
}

return serviceFileNotExist, nil
}

Expand Down

0 comments on commit 6450fe3

Please sign in to comment.