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

refactor: Simplify configuration #29

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions internal/commands/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ func (l *LaunchCommand) Execute() error {

// 3. filter environment variables

defaultEnvs := []string{"LANG", "PATH", "TZ", "TERM", env.EnvVarIdleTimeout, env.EnvVarRunnerServerEnabled}
defaultEnvs := []string{"LANG", "PATH", "TZ", "TERM", env.EnvVarIdleTimeout}
allowedEnvs := append(defaultEnvs, runnerCfg.AllowedEnv...)
runnerEnv := env.AllowedOnly(allowedEnvs)
// Static values
runnerEnv = append(runnerEnv, "N8N_RUNNERS_SERVER_ENABLED=true")

logs.Debugf("Filtered environment variables")

Expand Down Expand Up @@ -140,7 +142,7 @@ func (l *LaunchCommand) Execute() error {
return fmt.Errorf("failed to start runner process: %w", err)
}

go http.MonitorRunnerHealth(ctx, cmd, envCfg.RunnerServerURI, &wg)
go http.MonitorRunnerHealth(ctx, cmd, env.RunnerServerURI, &wg)

err = cmd.Wait()
if err != nil && err.Error() == "signal: killed" {
Expand Down
23 changes: 3 additions & 20 deletions internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ const (
// runner
// ------------------------

// EnvVarRunnerServerURI is the env var for the URI of the runner's server.
// Used for monitoring the runner's health, typically at http://127.0.0.1:5681.
EnvVarRunnerServerURI = "N8N_RUNNER_URI"

// EnvVarRunnerServerEnabled is the env var for whether the runner's health
// check server should be started.
EnvVarRunnerServerEnabled = "N8N_RUNNERS_SERVER_ENABLED"

// EnvVarIdleTimeout is the env var for how long (in seconds) a runner may be
// idle for before exit.
EnvVarIdleTimeout = "N8N_RUNNERS_AUTO_SHUTDOWN_TIMEOUT"
Expand All @@ -57,7 +49,9 @@ const (
defaultIdleTimeoutValue = "15" // seconds
DefaultMainServerURI = "http://127.0.0.1:5678"
DefaultTaskBrokerServerURI = "http://127.0.0.1:5679"
DefaultRunnerServerURI = "http://127.0.0.1:5681"

// URI of the runner. Used for monitoring the runner's health
RunnerServerURI = "http://127.0.0.1:5681"
)

// AllowedOnly filters the current environment down to only those
Expand Down Expand Up @@ -127,7 +121,6 @@ type EnvConfig struct {
AuthToken string
MainServerURI string
TaskBrokerServerURI string
RunnerServerURI string
}

// FromEnv retrieves vars from the environment, validates their values, and
Expand All @@ -138,7 +131,6 @@ func FromEnv() (*EnvConfig, error) {
authToken := os.Getenv(EnvVarAuthToken)
mainServerURI := os.Getenv(EnvVarMainServerURI)
taskBrokerServerURI := os.Getenv(EnvVarTaskBrokerServerURI)
runnerServerURI := os.Getenv(EnvVarRunnerServerURI)
idleTimeout := os.Getenv(EnvVarIdleTimeout)

if authToken == "" {
Expand All @@ -151,12 +143,6 @@ func FromEnv() (*EnvConfig, error) {
errs = append(errs, err)
}

if runnerServerURI == "" {
runnerServerURI = DefaultRunnerServerURI
} else if err := validateURL(runnerServerURI, EnvVarRunnerServerURI); err != nil {
errs = append(errs, err)
}

if taskBrokerServerURI == "" {
taskBrokerServerURI = DefaultTaskBrokerServerURI
} else if err := validateURL(taskBrokerServerURI, EnvVarTaskBrokerServerURI); err != nil {
Expand All @@ -176,12 +162,9 @@ func FromEnv() (*EnvConfig, error) {
return nil, errors.Join(errs...)
}

os.Setenv(EnvVarRunnerServerEnabled, "true")

return &EnvConfig{
AuthToken: authToken,
MainServerURI: mainServerURI,
TaskBrokerServerURI: taskBrokerServerURI,
RunnerServerURI: runnerServerURI,
}, nil
}
45 changes: 2 additions & 43 deletions internal/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,22 +156,19 @@ func TestFromEnv(t *testing.T) {
EnvVarAuthToken: "token123",
EnvVarMainServerURI: "http://localhost:9000",
EnvVarTaskBrokerServerURI: "http://localhost:9001",
EnvVarRunnerServerURI: "http://localhost:9002",
EnvVarIdleTimeout: "30",
},
expected: &EnvConfig{
AuthToken: "token123",
MainServerURI: "http://localhost:9000",
TaskBrokerServerURI: "http://localhost:9001",
RunnerServerURI: "http://localhost:9002",
},
},
{
name: "missing auth token",
envVars: map[string]string{
EnvVarMainServerURI: "http://localhost:5678",
EnvVarTaskBrokerServerURI: "http://localhost:5679",
EnvVarRunnerServerURI: "http://localhost:5681",
},
expectError: true,
},
Expand All @@ -181,7 +178,6 @@ func TestFromEnv(t *testing.T) {
EnvVarAuthToken: "token123",
EnvVarMainServerURI: "http://\\invalid:5678",
EnvVarTaskBrokerServerURI: "http://localhost:5679",
EnvVarRunnerServerURI: "http://localhost:5681",
},
expectError: true,
},
Expand All @@ -190,13 +186,11 @@ func TestFromEnv(t *testing.T) {
envVars: map[string]string{
EnvVarAuthToken: "token123",
EnvVarTaskBrokerServerURI: "http://localhost:5679",
EnvVarRunnerServerURI: "http://localhost:5681",
},
expected: &EnvConfig{
AuthToken: "token123",
MainServerURI: DefaultMainServerURI,
TaskBrokerServerURI: "http://localhost:5679",
RunnerServerURI: "http://localhost:5681",
},
},
{
Expand All @@ -205,46 +199,19 @@ func TestFromEnv(t *testing.T) {
EnvVarAuthToken: "token123",
EnvVarMainServerURI: "http://localhost:5678",
EnvVarTaskBrokerServerURI: "http://\\invalid:5679",
EnvVarRunnerServerURI: "http://localhost:5681",
},
expectError: true,
},
{
name: "missing task broker server URI",
envVars: map[string]string{
EnvVarAuthToken: "token123",
EnvVarMainServerURI: "http://localhost:5678",
EnvVarRunnerServerURI: "http://localhost:5681",
EnvVarAuthToken: "token123",
EnvVarMainServerURI: "http://localhost:5678",
},
expected: &EnvConfig{
AuthToken: "token123",
MainServerURI: "http://localhost:5678",
TaskBrokerServerURI: DefaultTaskBrokerServerURI,
RunnerServerURI: "http://localhost:5681",
},
},
{
name: "invalid runner server URI",
envVars: map[string]string{
EnvVarAuthToken: "token123",
EnvVarMainServerURI: "http://localhost:5678",
EnvVarTaskBrokerServerURI: "http://localhost:5679",
EnvVarRunnerServerURI: "http://\\invalid:5681",
},
expectError: true,
},
{
name: "missing runner server URI",
envVars: map[string]string{
EnvVarAuthToken: "token123",
EnvVarMainServerURI: "http://localhost:5678",
EnvVarTaskBrokerServerURI: "http://localhost:5679",
},
expected: &EnvConfig{
AuthToken: "token123",
MainServerURI: "http://localhost:5678",
TaskBrokerServerURI: "http://localhost:5679",
RunnerServerURI: DefaultRunnerServerURI,
},
},
{
Expand All @@ -253,7 +220,6 @@ func TestFromEnv(t *testing.T) {
EnvVarAuthToken: "token123",
EnvVarMainServerURI: "127.0.0.1:5678",
EnvVarTaskBrokerServerURI: "http://localhost:5679",
EnvVarRunnerServerURI: "http://localhost:5681",
},
expectError: true,
},
Expand All @@ -263,7 +229,6 @@ func TestFromEnv(t *testing.T) {
EnvVarAuthToken: "token123",
EnvVarMainServerURI: "localhost:5678",
EnvVarTaskBrokerServerURI: "http://localhost:5679",
EnvVarRunnerServerURI: "http://localhost:5681",
},
expectError: true,
},
Expand All @@ -273,7 +238,6 @@ func TestFromEnv(t *testing.T) {
EnvVarAuthToken: "token123",
EnvVarMainServerURI: "http://localhost:5678",
EnvVarTaskBrokerServerURI: "http://localhost:5679",
EnvVarRunnerServerURI: "http://localhost:5681",
EnvVarIdleTimeout: "invalid",
},
expectError: true,
Expand All @@ -284,7 +248,6 @@ func TestFromEnv(t *testing.T) {
EnvVarAuthToken: "token123",
EnvVarMainServerURI: "http://localhost:5678",
EnvVarTaskBrokerServerURI: "http://localhost:5679",
EnvVarRunnerServerURI: "http://localhost:5681",
EnvVarIdleTimeout: "-1",
},
expectError: true,
Expand Down Expand Up @@ -320,10 +283,6 @@ func TestFromEnv(t *testing.T) {
if !reflect.DeepEqual(envCfg, tt.expected) {
t.Errorf("FromEnv() = %+v, want %+v", envCfg, tt.expected)
}

if os.Getenv(EnvVarRunnerServerEnabled) != "true" {
t.Error("FromEnv() did not set runner server enabled to true")
}
})
}
}