Skip to content

Commit

Permalink
feat: return more info about the function
Browse files Browse the repository at this point in the history
  • Loading branch information
nponsard committed May 23, 2024
1 parent 197f4a6 commit 914b154
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ require (
github.com/google/uuid v1.6.0
github.com/joho/godotenv v1.5.1
github.com/minio/minio-go/v7 v7.0.69
github.com/google/uuid v1.6.0
github.com/redis/go-redis/v9 v9.5.1
gorm.io/gorm v1.25.8
)

require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/caarlos0/env/v10 v10.0.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
Expand All @@ -27,7 +25,6 @@ require (
github.com/kr/text v0.2.0 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/redis/go-redis/v9 v9.5.1 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/rs/xid v1.5.0 // indirect
golang.org/x/sync v0.6.0 // indirect
Expand Down
35 changes: 19 additions & 16 deletions routes/function/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,22 @@ func (cont *Controller) GetOneFunction(c *gin.Context) {
return
}

dto := FunctionDTO{
Name: function.Name,
Description: function.Description,
Language: function.Language,
Files: files,
dto := GetFunctionDTO{
Name: function.Name,
Description: function.Description,
Language: function.Language,
Files: files,
Built: function.Built,
BuildTimestamp: function.BuildTimestamp,
OwnerID: function.OwnerID,

Check failure on line 63 in routes/function/controller.go

View workflow job for this annotation

GitHub Actions / lint

function.OwnerID undefined (type database.Function has no field or method OwnerID) (typecheck)

Check failure on line 63 in routes/function/controller.go

View workflow job for this annotation

GitHub Actions / lint

function.OwnerID undefined (type database.Function has no field or method OwnerID)) (typecheck)

Check failure on line 63 in routes/function/controller.go

View workflow job for this annotation

GitHub Actions / build

function.OwnerID undefined (type database.Function has no field or method OwnerID)
}

c.JSON(http.StatusOK, dto)
}

func (cont *Controller) PostFunction(c *gin.Context) {
id := uuid.New()
var dto FunctionDTO
var dto CreateFunctionDTO
if err := c.ShouldBindJSON(&dto); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
Expand All @@ -90,7 +93,7 @@ func (cont *Controller) PostFunction(c *gin.Context) {
}

func (cont *Controller) PutFunction(c *gin.Context) {
var json FunctionDTO
var json CreateFunctionDTO
if err := c.ShouldBindJSON(&json); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
Expand Down Expand Up @@ -173,7 +176,7 @@ type BuilderRequest struct {
}

func (c *Controller) RunFunction(ctx *gin.Context) {
fnID, err := uuid.Parse(ctx.Param("id"))
fnID, err := uuid.Parse(ctx.Param("id"))

if err != nil {
ctx.AbortWithStatusJSON(400, gin.H{"error": "Invalid function ID"})
Expand Down Expand Up @@ -215,7 +218,7 @@ func (c *Controller) RunFunction(ctx *gin.Context) {
log.Println(err.Error())
ctx.AbortWithStatusJSON(500, gin.H{"error": "Could not cold start the function"})
return
}
}
} else if err != nil { // else if the error is not a record not found, we return an error
log.Println(err.Error())
ctx.AbortWithStatusJSON(500, gin.H{"error": "Could not cold start the function"})
Expand All @@ -234,16 +237,16 @@ func (c *Controller) RunFunction(ctx *gin.Context) {
// we check if it is ready
fnState, err := c.Scheduler.GetStateByID(stateID)

if err != nil {
if err != nil {
log.Println(err.Error())
ctx.AbortWithStatusJSON(500, gin.H{"Could not cold start the function": err.Error()})
return
}

if fnState.Status == int(database.FnReady) {
if fnState.Status == int(database.FnReady) {
log.Println("Function", fnID, "is ready")
break
};
break
}
}

// if even after 5 attempts the function is not ready, we return an error
Expand All @@ -257,11 +260,11 @@ func (c *Controller) RunFunction(ctx *gin.Context) {
err = c.Scheduler.SetStatus(stateID, database.FnRunning)

if err != nil {
log.Println(fmt.Sprint("Could not update state of VM", fnState.ID ,": ", err.Error()))
log.Println(fmt.Sprint("Could not update state of VM", fnState.ID, ": ", err.Error()))
ctx.AbortWithStatusJSON(500, gin.H{"error": "Cannot update function's status"})
return
}

_, err = http.Post(
fmt.Sprint(string(fnState.Address), ":", fnState.Port, "/execute"),
"application/json",
Expand All @@ -281,7 +284,7 @@ func (c *Controller) RunFunction(ctx *gin.Context) {

if err != nil {
log.Println(
fmt.Sprint("Could not update state of VM", fnState.ID ,": ", err.Error()),
fmt.Sprint("Could not update state of VM", fnState.ID, ": ", err.Error()),
)
log.Println(err.Error())
}
Expand Down
12 changes: 11 additions & 1 deletion routes/function/entity.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
package function

type FunctionDTO struct {
type CreateFunctionDTO struct {
Name string `json:"name" binding:"required"`
Description string `json:"description" binding:"required"`
Language string `json:"language" binding:"required"`
Files map[string]string `json:"files" binding:"required"`
}

type GetFunctionDTO struct {
Name string `json:"name"`
Description string `json:"description"`
Language string `json:"language"`
Files map[string]string `json:"files"`
Built bool `json:"built"`
BuildTimestamp int64 `json:"build_timestamp"`
OwnerID int `json:"owner_id"`
}

0 comments on commit 914b154

Please sign in to comment.