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

Env variable NO_WINDOWS_SERVICE to force interactive mode on Windows #254

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions cmd/otelcol/main_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,42 @@ package main

import (
"fmt"
"os"

"go.opentelemetry.io/collector/service"
"golang.org/x/sys/windows/svc"
)

func run(params service.Parameters) error {
isInteractive, err := svc.IsAnInteractiveSession()
if err != nil {
return fmt.Errorf("failed to determine if we are running in an interactive session %w", err)
}

if isInteractive {
if useInteractiveMode, err := checkUseInteractiveMode(); err != nil {
return err
} else if useInteractiveMode {
return runInteractive(params)
} else {
return runService(params)
}
}

func checkUseInteractiveMode() (bool, error) {
// If environment variable NO_WINDOWS_SERVICE is set with any value other
// than 0, use interactive mode instead of running as a service. This should
// be set in case running as a service is not possible or desired even
// though the current session is not detected to be interactive
if value, present := os.LookupEnv("NO_WINDOWS_SERVICE"); present && value != "0" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunate there's a double negative ("no" & 0) as opposed to something like WINDOWS_INTERACTIVE, but deviating may not be desired...

return true, nil
}

if isInteractiveSession, err := svc.IsAnInteractiveSession(); err != nil {
return false, fmt.Errorf("failed to determine if we are running in an interactive session %w", err)
} else {
return isInteractiveSession, nil
}
}

func runService(params service.Parameters) error {
// do not need to supply service name when startup is invoked through Service Control Manager directly
if err := svc.Run("", service.NewWindowsService(params)); err != nil {
return fmt.Errorf("failed to start service %w", err)
return fmt.Errorf("failed to start service: %w", err)
}

return nil
Expand Down