Skip to content

Commit

Permalink
fix: change file architecture and retry logic
Browse files Browse the repository at this point in the history
  • Loading branch information
WoodenMaiden committed Apr 10, 2024
1 parent 135b5fc commit bff595e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
19 changes: 19 additions & 0 deletions routes/function/controller.go
Original file line number Diff line number Diff line change
@@ -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)
}
29 changes: 16 additions & 13 deletions routes/runFunction.go → routes/function/routes.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package routes
package function

import (
"errors"
Expand All @@ -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"})
Expand All @@ -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" {
Expand All @@ -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)
}
2 changes: 2 additions & 0 deletions routes/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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

Expand Down

0 comments on commit bff595e

Please sign in to comment.