123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- package server
- import (
- "fmt"
- "time"
- "github.com/gin-gonic/gin"
- "github.com/langgenius/dify-sandbox/internal/controller"
- "github.com/langgenius/dify-sandbox/internal/core/runner/python"
- "github.com/langgenius/dify-sandbox/internal/static"
- "github.com/langgenius/dify-sandbox/internal/utils/log"
- )
- func initConfig() {
- // auto migrate database
- err := static.InitConfig("conf/config.yaml")
- if err != nil {
- log.Panic("failed to init config: %v", err)
- }
- log.Info("config init success")
- err = static.SetupRunnerDependencies()
- if err != nil {
- log.Error("failed to setup runner dependencies: %v", err)
- }
- log.Info("runner dependencies init success")
- }
- func initServer() {
- config := static.GetDifySandboxGlobalConfigurations()
- if !config.App.Debug {
- gin.SetMode(gin.ReleaseMode)
- }
- r := gin.Default()
- r.Use(gin.Recovery())
- if gin.Mode() == gin.DebugMode {
- r.Use(gin.Logger())
- }
- controller.Setup(r)
- r.Run(fmt.Sprintf(":%d", config.App.Port))
- }
- func initDependencies() {
- log.Info("installing python dependencies...")
- dependencies := static.GetRunnerDependencies()
- err := python.InstallDependencies(dependencies.PythonRequirements)
- if err != nil {
- log.Panic("failed to install python dependencies: %v", err)
- }
- log.Info("python dependencies installed")
- log.Info("initializing python dependencies sandbox...")
- err = python.PreparePythonDependenciesEnv()
- if err != nil {
- log.Panic("failed to initialize python dependencies sandbox: %v", err)
- }
- log.Info("python dependencies sandbox initialized")
- // start a ticker to update python dependencies to keep the sandbox up-to-date
- go func() {
- updateInterval := static.GetDifySandboxGlobalConfigurations().PythonDepsUpdateInterval
- tickerDuration, err := time.ParseDuration(updateInterval)
- if err != nil {
- log.Error("failed to parse python dependencies update interval, skip periodic updates: %v", err)
- return
- }
- ticker := time.NewTicker(tickerDuration)
- for range ticker.C {
- if err:=updatePythonDependencies(dependencies);err!=nil{
- log.Error("Failed to update Python dependencies: %v", err)
- }
- }
- }()
- }
- func updatePythonDependencies(dependencies static.RunnerDependencies) error {
- log.Info("Updating Python dependencies...")
- if err := python.InstallDependencies(dependencies.PythonRequirements); err != nil {
- log.Error("Failed to install Python dependencies: %v", err)
- return err
- }
- if err := python.PreparePythonDependenciesEnv(); err != nil {
- log.Error("Failed to prepare Python dependencies environment: %v", err)
- return err
- }
- log.Info("Python dependencies updated successfully.")
- return nil
- }
- func Run() {
- // init config
- initConfig()
- // init dependencies, it will cost some times
- go initDependencies()
- initServer()
- }
|