Skip to content

Commit

Permalink
refactor: Simplify configuration (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomi authored Nov 27, 2024
1 parent 7f319a3 commit c08c849
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 65 deletions.
6 changes: 4 additions & 2 deletions internal/commands/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,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 @@ -144,7 +146,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 @@ -41,14 +41,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 @@ -58,7 +50,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 @@ -130,7 +124,6 @@ type EnvConfig struct {
AuthToken string
MainServerURI string
TaskBrokerServerURI string
RunnerServerURI string
}

// FromEnv retrieves vars from the environment, validates their values, and
Expand All @@ -141,7 +134,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 @@ -154,12 +146,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 @@ -179,12 +165,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")
}
})
}
}

0 comments on commit c08c849

Please sign in to comment.