Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check windows service manager settings prior to setting them #1859

Merged
merged 13 commits into from
Sep 12, 2024
38 changes: 35 additions & 3 deletions cmd/launcher/svc_config_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func checkServiceConfiguration(logger *slog.Logger, opts *launcher.Options) {

checkRestartActions(logger, launcherService)

setRecoveryActions(context.TODO(), logger, launcherService)
checkRecoveryActions(context.TODO(), logger, launcherService)
}

// checkDelayedAutostart checks the current value of `DelayedAutostart` (whether to wait ~2 minutes
Expand Down Expand Up @@ -192,9 +192,20 @@ func checkRestartActions(logger *slog.Logger, service *mgr.Service) {
logger.Log(context.TODO(), slog.LevelInfo, "successfully set RecoveryActionsOnNonCrashFailures flag")
}

// setRecoveryActions sets the recovery actions for the launcher service.
// checkRecoveryActions checks if the recovery actions for the launcher service are set.
// sets if one or more of the recovery actions are not set.
// previously defined via wix ServicConfig Element (Util Extension) https://wixtoolset.org/docs/v3/xsd/util/serviceconfig/
func setRecoveryActions(ctx context.Context, logger *slog.Logger, service *mgr.Service) {
func checkRecoveryActions(ctx context.Context, logger *slog.Logger, service *mgr.Service) {
mlwood-dev marked this conversation as resolved.
Show resolved Hide resolved
curRecoveryActions, err := service.RecoveryActions()
if err != nil {
logger.Log(context.TODO(), slog.LevelError,
"querying for current RecoveryActions",
"err", err,
)

return
}

recoveryActions := []mgr.RecoveryAction{
{
// first failure
Expand All @@ -213,10 +224,31 @@ func setRecoveryActions(ctx context.Context, logger *slog.Logger, service *mgr.S
},
}

// If the recovery actions are already set, we don't need to do anything
if recoveryActionsAreSet(curRecoveryActions, recoveryActions) {
return
}

if err := service.SetRecoveryActions(recoveryActions, 24*60*60); err != nil { // 24 hours
logger.Log(ctx, slog.LevelError,
"setting RecoveryActions",
"err", err,
)
}
}

// recoveryActionsAreSet checks if the current recovery actions are set to the desired recovery actions
func recoveryActionsAreSet(curRecoveryActions, recoveryActions []mgr.RecoveryAction) bool {
if curRecoveryActions == nil || len(curRecoveryActions) != len(recoveryActions) {
return false
}
for i := range curRecoveryActions {
if curRecoveryActions[i].Type != recoveryActions[i].Type {
return false
}
if curRecoveryActions[i].Delay != recoveryActions[i].Delay {
return false
}
}
return true
}
Loading