From 67181700c86335aba3d435da8a6a28bc144f1b25 Mon Sep 17 00:00:00 2001 From: Marvin Zhang Date: Fri, 14 Feb 2025 14:02:04 +0800 Subject: [PATCH] feat: improve task runner environment configuration - Remove Crawlab-specific environment variables from the task runner's environment - Automatically create workspace directory if it doesn't exist - Enhance environment setup to prevent potential configuration conflicts --- core/task/handler/runner.go | 10 ++++++++++ core/utils/config.go | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/core/task/handler/runner.go b/core/task/handler/runner.go index ef61a2e0..80b7d33c 100644 --- a/core/task/handler/runner.go +++ b/core/task/handler/runner.go @@ -390,6 +390,16 @@ func (r *Runner) configureEnv() { // Default envs r.cmd.Env = os.Environ() + + // Remove CRAWLAB_ prefixed environment variables + for i := 0; i < len(r.cmd.Env); i++ { + if strings.HasPrefix(r.cmd.Env[i], "CRAWLAB_") { + r.cmd.Env = append(r.cmd.Env[:i], r.cmd.Env[i+1:]...) + i-- + } + } + + // Task-specific environment variables r.cmd.Env = append(r.cmd.Env, "CRAWLAB_TASK_ID="+r.tid.Hex()) // Global environment variables diff --git a/core/utils/config.go b/core/utils/config.go index eb107c64..e5c92eb8 100644 --- a/core/utils/config.go +++ b/core/utils/config.go @@ -5,6 +5,7 @@ import ( "github.com/gin-gonic/gin" "github.com/mitchellh/go-homedir" "github.com/spf13/viper" + "os" "path/filepath" "strings" ) @@ -93,6 +94,12 @@ func GetWorkspace() string { if res := viper.GetString("workspace"); res != "" { return res } + if !Exists(filepath.Join(homedirPath, DefaultWorkspace)) { + err := os.MkdirAll(filepath.Join(homedirPath, DefaultWorkspace), os.ModePerm) + if err != nil { + logger.Warnf("cannot create workspace directory: %v", err) + } + } return filepath.Join(homedirPath, DefaultWorkspace) }