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
This commit is contained in:
Marvin Zhang
2025-02-14 14:02:04 +08:00
parent 4317a03971
commit 67181700c8
2 changed files with 17 additions and 0 deletions

View File

@@ -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

View File

@@ -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)
}