Skip to content

Commit

Permalink
feat: Add task timeout env var (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov authored Dec 10, 2024
1 parent fe8429f commit 854fb76
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This launcher is intended for deployment as a sidecar container alongside one or
"N8N_RUNNERS_TASK_BROKER_URI",
"N8N_RUNNERS_MAX_PAYLOAD",
"N8N_RUNNERS_MAX_CONCURRENCY",
"N8N_RUNNERS_TASK_TIMEOUT",
"NODE_FUNCTION_ALLOW_BUILTIN",
"NODE_FUNCTION_ALLOW_EXTERNAL",
"NODE_OPTIONS"
Expand Down Expand Up @@ -59,6 +60,8 @@ Task runner config fields:
- `ENVIRONMENT`: Mapped to `Environment`
- `N8N_VERSION`: Mapped to `Release`

- Optionally, set `N8N_RUNNERS_TASK_TIMEOUT` to specify how long (in seconds) a task may run for before it is aborted. Default is `60`.

4. Run the launcher:

```sh
Expand Down
4 changes: 4 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ type Config struct {
// before automatically shutting down, until later relaunched.
AutoShutdownTimeout string `env:"N8N_RUNNERS_AUTO_SHUTDOWN_TIMEOUT, default=15"`

// TaskTimeout is the max time (in seconds) a task may run for before it is
// aborted.
TaskTimeout string `env:"N8N_RUNNERS_TASK_TIMEOUT, default=60"`

// TaskBrokerURI is the URI of the task broker server.
TaskBrokerURI string `env:"N8N_RUNNERS_TASK_BROKER_URI, default=http://127.0.0.1:5679"`

Expand Down
5 changes: 5 additions & 0 deletions internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const (
// EnvVarAutoShutdownTimeout is the env var for how long (in seconds) a runner
// may be idle for before exit.
EnvVarAutoShutdownTimeout = "N8N_RUNNERS_AUTO_SHUTDOWN_TIMEOUT"

// EnvVarTaskTimeout is the env var for how long (in seconds) a task may run
// for before it is aborted.
EnvVarTaskTimeout = "N8N_RUNNERS_TASK_TIMEOUT"
)

const (
Expand Down Expand Up @@ -80,6 +84,7 @@ func PrepareRunnerEnv(cfg *config.Config) []string {
runnerEnv := allowedOnly(allowedEnvs)
runnerEnv = append(runnerEnv, "N8N_RUNNERS_HEALTH_CHECK_SERVER_ENABLED=true")
runnerEnv = append(runnerEnv, fmt.Sprintf("%s=%s", EnvVarAutoShutdownTimeout, cfg.AutoShutdownTimeout))
runnerEnv = append(runnerEnv, fmt.Sprintf("%s=%s", EnvVarTaskTimeout, cfg.TaskTimeout))

return runnerEnv
}
6 changes: 6 additions & 0 deletions internal/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func TestPrepareRunnerEnv(t *testing.T) {
name: "includes default and allowed env vars",
config: &config.Config{
AutoShutdownTimeout: "15",
TaskTimeout: "60",
Runner: &config.RunnerConfig{
AllowedEnv: []string{"CUSTOM_VAR1", "CUSTOM_VAR2"},
},
Expand All @@ -177,6 +178,7 @@ func TestPrepareRunnerEnv(t *testing.T) {
"LANG=en_US.UTF-8",
"N8N_RUNNERS_AUTO_SHUTDOWN_TIMEOUT=15",
"N8N_RUNNERS_HEALTH_CHECK_SERVER_ENABLED=true",
"N8N_RUNNERS_TASK_TIMEOUT=60",
"PATH=/usr/bin",
"TERM=xterm",
"TZ=UTC",
Expand All @@ -186,6 +188,7 @@ func TestPrepareRunnerEnv(t *testing.T) {
name: "handles empty allowed env list",
config: &config.Config{
AutoShutdownTimeout: "15",
TaskTimeout: "60",
Runner: &config.RunnerConfig{
AllowedEnv: []string{},
},
Expand All @@ -199,13 +202,15 @@ func TestPrepareRunnerEnv(t *testing.T) {
"LANG=en_US.UTF-8",
"N8N_RUNNERS_AUTO_SHUTDOWN_TIMEOUT=15",
"N8N_RUNNERS_HEALTH_CHECK_SERVER_ENABLED=true",
"N8N_RUNNERS_TASK_TIMEOUT=60",
"PATH=/usr/bin",
},
},
{
name: "handles custom auto-shutdown timeout",
config: &config.Config{
AutoShutdownTimeout: "30",
TaskTimeout: "60",
Runner: &config.RunnerConfig{
AllowedEnv: []string{},
},
Expand All @@ -217,6 +222,7 @@ func TestPrepareRunnerEnv(t *testing.T) {
expected: []string{
"N8N_RUNNERS_AUTO_SHUTDOWN_TIMEOUT=30",
"N8N_RUNNERS_HEALTH_CHECK_SERVER_ENABLED=true",
"N8N_RUNNERS_TASK_TIMEOUT=60",
"PATH=/usr/bin",
},
},
Expand Down

0 comments on commit 854fb76

Please sign in to comment.