Skip to content

Commit

Permalink
add health / refresh dependency (#34)
Browse files Browse the repository at this point in the history
* add health / refresh dependency

* health check

---------

Co-authored-by: hobo.l <[email protected]>
  • Loading branch information
luckylhb90 and hobo-l-20230331 authored Aug 19, 2024
1 parent 072abe7 commit f1e5df2
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 19 deletions.
51 changes: 35 additions & 16 deletions internal/controller/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,43 @@ import (
"github.com/gin-gonic/gin"
"github.com/langgenius/dify-sandbox/internal/middleware"
"github.com/langgenius/dify-sandbox/internal/static"
"net/http"
)

func Setup(eng *gin.Engine) {
eng.Use(middleware.Auth())
func Setup(Router *gin.Engine) {
PublicGroup := Router.Group("")
PrivateGroup := Router.Group("/v1/sandbox/")

eng.POST(
"/v1/sandbox/run",
middleware.MaxRequest(static.GetDifySandboxGlobalConfigurations().MaxRequests),
middleware.MaxWorker(static.GetDifySandboxGlobalConfigurations().MaxWorkers),
RunSandboxController,
)
eng.GET(
"/v1/sandbox/dependencies",
GetDependencies,
)
PrivateGroup.Use(middleware.Auth())

eng.POST(
"/v1/sandbox/dependencies/update",
UpdateDependencies,
)
{
// health check
PublicGroup.GET("/health", func(c *gin.Context) {
c.JSON(http.StatusOK, "ok")
})
}

InitRunRouter(PrivateGroup)
InitDependencyRouter(PrivateGroup)
}

func InitDependencyRouter(Router *gin.RouterGroup) {
dependencyRouter := Router.Group("dependencies")
{
dependencyRouter.GET("", GetDependencies)
dependencyRouter.POST("update", UpdateDependencies)
dependencyRouter.GET("refresh", RefreshDependencies)
}
}

func InitRunRouter(Router *gin.RouterGroup) {
runRouter := Router.Group("")
{
runRouter.POST(
"run",
middleware.MaxRequest(static.GetDifySandboxGlobalConfigurations().MaxRequests),
middleware.MaxWorker(static.GetDifySandboxGlobalConfigurations().MaxWorkers),
RunSandboxController,
)
}
}
13 changes: 13 additions & 0 deletions internal/controller/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,16 @@ func UpdateDependencies(c *gin.Context) {
}
})
}

func RefreshDependencies(c *gin.Context) {
BindRequest(c, func(req struct {
Language string `json:"language" form:"language" binding:"required"`
}) {
switch req.Language {
case "python3":
c.JSON(200, service.RefreshPython3Dependencies())
default:
c.JSON(400, types.ErrorResponse(-400, "unsupported language"))
}
})
}
12 changes: 12 additions & 0 deletions internal/core/runner/python/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package python
import (
_ "embed"
"fmt"
"github.com/langgenius/dify-sandbox/internal/static"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -142,3 +143,14 @@ func InstallDependencies(requirements string) error {
func ListDependencies() []types.Dependency {
return python_dependencies.ListDependencies()
}

func RefreshDependencies() []types.Dependency {
log.Info("updating python dependencies...")
dependencies := static.GetRunnerDependencies()
err := InstallDependencies(dependencies.PythonRequirements)
if err != nil {
log.Error("failed to update python dependencies: %v", err)
}
log.Info("python dependencies updated")
return python_dependencies.ListDependencies()
}
11 changes: 8 additions & 3 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,20 @@ func initServer() {
}

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...")
dependenices := static.GetRunnerDependencies()
err := python.InstallDependencies(dependenices.PythonRequirements)
dependencies := static.GetRunnerDependencies()
err := python.InstallDependencies(dependencies.PythonRequirements)
if err != nil {
log.Panic("failed to install python dependencies: %v", err)
}
Expand All @@ -59,7 +64,7 @@ func initDependencies() {
ticker := time.NewTicker(30 * time.Minute)
for range ticker.C {
log.Info("updating python dependencies...")
err := python.InstallDependencies(dependenices.PythonRequirements)
err := python.InstallDependencies(dependencies.PythonRequirements)
if err != nil {
log.Error("failed to update python dependencies: %v", err)
}
Expand Down
10 changes: 10 additions & 0 deletions internal/service/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ func ListPython3Dependencies() *types.DifySandboxResponse {
})
}

type RefreshDependenciesResponse struct {
Dependencies []runner_types.Dependency `json:"dependencies"`
}

func RefreshPython3Dependencies() *types.DifySandboxResponse {
return types.SuccessResponse(&RefreshDependenciesResponse{
Dependencies: python.RefreshDependencies(),
})
}

type UpdateDependenciesResponse struct{}

func UpdateDependencies() *types.DifySandboxResponse {
Expand Down

0 comments on commit f1e5df2

Please sign in to comment.