From bff595e31b8ce49d09348e2932c165d5b4995847 Mon Sep 17 00:00:00 2001 From: WoodenMaiden Date: Wed, 10 Apr 2024 13:58:36 +0200 Subject: [PATCH] fix: change file architecture and retry logic --- routes/function/controller.go | 19 ++++++++++++ routes/{runFunction.go => function/routes.go} | 29 ++++++++++--------- routes/router.go | 2 ++ 3 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 routes/function/controller.go rename routes/{runFunction.go => function/routes.go} (58%) diff --git a/routes/function/controller.go b/routes/function/controller.go new file mode 100644 index 0000000..1c5cfdd --- /dev/null +++ b/routes/function/controller.go @@ -0,0 +1,19 @@ +package function + +import ( + "gorm.io/gorm" + "github.com/gin-gonic/gin" + +) + +type Controller struct { + DB *gorm.DB +} + +func ConfigureRoutes(router *gin.Engine, db *gorm.DB) { + controller := Controller{DB: db} + group := router.Group("/function") + + + group.POST("/:id/run", controller.RunFunction) +} diff --git a/routes/runFunction.go b/routes/function/routes.go similarity index 58% rename from routes/runFunction.go rename to routes/function/routes.go index 62b2c02..58793a3 100644 --- a/routes/runFunction.go +++ b/routes/function/routes.go @@ -1,4 +1,4 @@ -package routes +package function import ( "errors" @@ -15,12 +15,7 @@ import ( func (c *Controller) RunFunction(ctx *gin.Context) { fnID, err := uuid.Parse(ctx.Param("id")) - var fnBody any; // because the body is entirely defined by the user, we just forward it to the function - - if err := ctx.BindJSON(&fnBody); err != nil { - ctx.AbortWithStatusJSON(400, gin.H{"error": "Could not parse function body"}) - return - } + //var fnBody any; // because the body is entirely defined by the user, we just forward it to the function without checking it if err != nil { ctx.AbortWithStatusJSON(400, gin.H{"error": "Invalid function ID"}) @@ -30,23 +25,26 @@ func (c *Controller) RunFunction(ctx *gin.Context) { var fn database.FunctionState err = c.DB.Where(&database.FunctionState{ID: fnID}).First(&fn).Error - if (err != nil && errors.Is(err, gorm.ErrRecordNotFound)) { + if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { ctx.AbortWithStatusJSON(404, gin.H{"error": "Function not found"}) return } - if (fn.Status != "Ready") { - log.Println("Waiting for new function to be ready") + if fn.Status != "Ready" { + log.Println("Waiting for function", fn.ID, "to be ready") + time.Sleep(100 * time.Millisecond) + + for attempts := 0; attempts < 5; attempts++ { + err = c.DB.Where(&database.FunctionState{ID: fnID, Status: "Ready"}).First(&fn).Error - for tries := 0; tries < 5; tries++ { if !errors.Is(err, gorm.ErrRecordNotFound) { + log.Println(err) ctx.AbortWithStatusJSON(500, gin.H{"error": err.Error()}) return } else { log.Println("Function", fn.ID, "is not ready yet... Retrying") time.Sleep(100 * time.Millisecond) - err = c.DB.Where(&database.FunctionState{ID: fnID, Status: "Ready"}).First(&fn).Error } if fn.Status == "Ready" { @@ -56,8 +54,13 @@ func (c *Controller) RunFunction(ctx *gin.Context) { }; } + + if fn.Status != "Ready" { + ctx.AbortWithStatusJSON(500, gin.H{"error": "Function is not ready"}) + return + } } - // forward the body to fn.Address:fn.Port + // TODO: forward the body to fn.Address:fn.Port ctx.JSON(200, fn) } \ No newline at end of file diff --git a/routes/router.go b/routes/router.go index d237ac0..7a5f7e6 100644 --- a/routes/router.go +++ b/routes/router.go @@ -4,6 +4,7 @@ import ( "log" "github.com/do4-2022/grobuzin/routes/user" + "github.com/do4-2022/grobuzin/routes/function" "github.com/gin-gonic/gin" "gorm.io/gorm" ) @@ -17,6 +18,7 @@ func GetRoutes(db *gorm.DB, JWTSecret string) *gin.Engine { log.Println("Setting up routes", requireAuthMiddleware) user.ConfigureRoutes(router, db, JWTSecret) + function.ConfigureRoutes(router, db) return router