Skip to content

Commit

Permalink
feat: link ownerID to function
Browse files Browse the repository at this point in the history
  • Loading branch information
WoodenMaiden committed May 13, 2024
1 parent 1e1507c commit 9f5f0eb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
13 changes: 9 additions & 4 deletions database/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import (
)

type Function struct {
ID uuid.UUID `json:"id" ,gorm:"primarykey;type:uuid;default:gen_random_uuid()"`
Name string `json:"name"`
Description string `json:"description"`
Language string `json:"language"`
ID uuid.UUID `json:"id" ,gorm:"primarykey;type:uuid;default:gen_random_uuid()"`
Name string `json:"name"`
Description string `json:"description"`
Language string `json:"language"`

OwnerID int `json:"owner_id"`
Owner User `json:"-"`

FunctionStates []FunctionState `json:"-"`
}
12 changes: 6 additions & 6 deletions database/functionState.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
type FunctionState struct {
gorm.Model
ID uuid.UUID `json:"id" ,gorm:"primarykey;type:uuid;default:gen_random_uuid()"`
Code string `json:"code"`
Status string `json:"status"`
Address string `json:"address"`
Port uint16 `json:"port"`
// OwnerID
Status string `json:"status"`
Address string `json:"address"`
Port uint16 `json:"port"`

FunctionID uuid.UUID `json:"function_id"`
}

func (FunctionState) TableName() string {
return "function_state"
return "function_states"
}
8 changes: 4 additions & 4 deletions routes/function/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (c *Controller) RunFunction(ctx *gin.Context) {
}

var fn database.FunctionState
err = c.DB.Where(&database.FunctionState{ID: fnID}).First(&fn).Error
err = c.DB.Where(&database.FunctionState{FunctionID: fnID}).First(&fn).Error

if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
ctx.AbortWithStatusJSON(404, gin.H{"error": "Function not found"})
Expand All @@ -190,20 +190,20 @@ func (c *Controller) RunFunction(ctx *gin.Context) {
time.Sleep(100 * time.Millisecond)

for attempts := 0; attempts < 5; attempts++ {
err = c.DB.Where(&database.FunctionState{ID: fnID, Status: "Ready"}).First(&fn).Error
err = c.DB.Where(&database.FunctionState{FunctionID: fnID, Status: "Ready"}).First(&fn).Error

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")
log.Println("Function", fn.FunctionID, "is not ready yet... Retrying")

time.Sleep(100 * time.Millisecond)
}

if fn.Status == "Ready" {
log.Println("Function", fn.ID, "is ready")
log.Println("Function", fn.FunctionID, "is ready")

break
};
Expand Down

0 comments on commit 9f5f0eb

Please sign in to comment.