瀏覽代碼

use environment variable config python init time

非法操作 6 月之前
父節點
當前提交
468c0743db

+ 3 - 0
.env.example

@@ -59,6 +59,9 @@ DIFY_PLUGIN_SERVERLESS_CONNECTOR_API_KEY=HeRFb6yrzAy5vUSlJWK2lUl36mpkaRycv4witbQ
 # otherwise, it should be /usr/bin/python3
 PYTHON_INTERPRETER_PATH=/Users/yeuoly/miniconda3/envs/dify-plugin-sdk/bin/python
 
+# python environment init timeout, if the python environment init process is not finished within this time, it will be killed
+PYTHON_ENV_INIT_TIMEOUT=120
+
 # pprof enabled, for debugging
 PPROF_ENABLED=false
 

+ 1 - 1
internal/core/plugin_manager/launcher.go

@@ -129,7 +129,7 @@ func (p *PluginManager) launchLocal(pluginUniqueIdentifier plugin_entities.Plugi
 		return nil, nil, nil, failed(err.Error())
 	}
 
-	localPluginRuntime := local_runtime.NewLocalPluginRuntime(p.pythonInterpreterPath)
+	localPluginRuntime := local_runtime.NewLocalPluginRuntime(p.pythonInterpreterPath, p.pythonEnvInitTimeout)
 	localPluginRuntime.PluginRuntime = plugin.runtime
 	localPluginRuntime.BasicChecksum = basic_runtime.BasicChecksum{
 		MediaTransport: basic_runtime.NewMediaTransport(p.mediaBucket),

+ 2 - 2
internal/core/plugin_manager/local_runtime/environment_python.go

@@ -161,9 +161,9 @@ func (p *LocalPluginRuntime) InitPythonEnvironment() error {
 				break
 			}
 
-			if time.Since(lastActiveAt) > 180*time.Second {
+			if time.Since(lastActiveAt) > time.Duration(p.pythonEnvInitTimeout)*time.Second {
 				cmd.Process.Kill()
-				err_msg.WriteString("init process exited due to long time no activity")
+				err_msg.WriteString(fmt.Sprintf("init process exited due to no activity for %d seconds", p.pythonEnvInitTimeout))
 				break
 			}
 		}

+ 5 - 0
internal/core/plugin_manager/local_runtime/type.go

@@ -17,6 +17,9 @@ type LocalPluginRuntime struct {
 	// python interpreter path, currently only support python
 	pythonInterpreterPath string
 
+	// python env init timeout
+	pythonEnvInitTimeout int
+
 	// to create a new python virtual environment, we need a default python interpreter
 	// by using its venv module
 	defaultPythonInterpreterPath string
@@ -30,8 +33,10 @@ type LocalPluginRuntime struct {
 
 func NewLocalPluginRuntime(
 	pythonInterpreterPath string,
+	pythonEnvInitTimeout int,
 ) *LocalPluginRuntime {
 	return &LocalPluginRuntime{
 		defaultPythonInterpreterPath: pythonInterpreterPath,
+		pythonEnvInitTimeout:         pythonEnvInitTimeout,
 	}
 }

+ 4 - 0
internal/core/plugin_manager/manager.go

@@ -56,6 +56,9 @@ type PluginManager struct {
 	// python interpreter path
 	pythonInterpreterPath string
 
+	// python env init timeout
+	pythonEnvInitTimeout int
+
 	// remote plugin server
 	remotePluginServer debugging_runtime.RemotePluginServerInterface
 
@@ -91,6 +94,7 @@ func InitGlobalManager(oss oss.OSS, configuration *app.Config) *PluginManager {
 		localPluginLaunchingLock: lock.NewGranularityLock(),
 		maxLaunchingLock:         make(chan bool, 2), // by default, we allow 2 plugins launching at the same time
 		pythonInterpreterPath:    configuration.PythonInterpreterPath,
+		pythonEnvInitTimeout:     configuration.PythonEnvInitTimeout,
 		platform:                 configuration.Platform,
 	}
 

+ 1 - 0
internal/types/app/config.go

@@ -82,6 +82,7 @@ type Config struct {
 	MaxAWSLambdaTransactionTimeout int   `envconfig:"MAX_AWS_LAMBDA_TRANSACTION_TIMEOUT"`
 
 	PythonInterpreterPath string `envconfig:"PYTHON_INTERPRETER_PATH"`
+	PythonEnvInitTimeout  int    `envconfig:"PYTHON_ENV_INIT_TIMEOUT" validate:"required"`
 
 	DisplayClusterLog bool `envconfig:"DISPLAY_CLUSTER_LOG"`