Skip to content

Commit

Permalink
docs: add doc to run function & lambdo service
Browse files Browse the repository at this point in the history
  • Loading branch information
WoodenMaiden committed May 16, 2024
1 parent 17b4f6d commit 72e40d8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
31 changes: 15 additions & 16 deletions routes/function/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,6 @@ func (c *Controller) RunFunction(ctx *gin.Context) {

var fn database.Function
var fnState database.FunctionState

fnBody, err := io.ReadAll(ctx.Request.Body); // 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 request body"})
return
}

// does the function exist?
err = c.DB.Where(&database.Function{ID: fnID}).First(&fn).Error
Expand All @@ -210,16 +203,17 @@ func (c *Controller) RunFunction(ctx *gin.Context) {
return
}

// retrieving freshly created function state
fnState, err = c.Scheduler.GetStateByID(
fmt.Sprintf(fnID.String(), ":", res.ID),
)

if err != nil {
return
}
}

if err != nil {
} 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"})
return
}

Expand All @@ -235,9 +229,9 @@ func (c *Controller) RunFunction(ctx *gin.Context) {
// we check if it is ready
fnState, err := c.Scheduler.GetStateByID(stateID)

// if the error is something else than a record not found, we return an error, else we retry since it is not ready yet
if err != nil {
ctx.AbortWithStatusJSON(500, gin.H{"error": err.Error()})
log.Println(err.Error())
ctx.AbortWithStatusJSON(500, gin.H{"Could not cold start the function": err.Error()})
return
}

Expand All @@ -249,27 +243,29 @@ func (c *Controller) RunFunction(ctx *gin.Context) {

// if even after 5 attempts the function is not ready, we return an error
if fnState.Status != database.FnReady {
ctx.AbortWithStatusJSON(500, gin.H{"error": "Function is not ready"})
ctx.AbortWithStatusJSON(503, gin.H{"error": "Function is not ready"})
return
}
}

// we notify everyone that the function is running
err = c.Scheduler.SetStatus(stateID, database.FnRunning)

if err != nil {
log.Println(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("http://", string(fnState.Address), ":", fnState.Port, "/execute"),
"application/json",
bytes.NewReader(fnBody),
ctx.Request.Body,
)

// if the function had trouble running, we update the status to unknown
if err != nil {
ctx.AbortWithStatusJSON(500, gin.H{"error": err.Error()})
ctx.AbortWithStatusJSON(500, gin.H{"error": err.Error()})
_ = c.Scheduler.SetStatus(stateID, database.FnUnknownState)
return
} else {
Expand All @@ -279,6 +275,9 @@ func (c *Controller) RunFunction(ctx *gin.Context) {
err = c.Scheduler.SetStatus(stateID, database.FnReady)

if err != nil {
log.Println(
fmt.Sprint("Could not update state of VM", fnState.ID ,": ", err.Error()),
)
log.Println(err.Error())
}
}
Expand Down
4 changes: 4 additions & 0 deletions scheduler/lambdoService.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ type LambdoService struct {
}

type LambdoSpawnRequest struct {
// URL to the rootfs of the function
RootfsURL string `json:"rootfs"`
// Ports that the virtual machine needs to be exposed
// right now we only support one port
RequestedPorts []uint16 `json:"requestedPorts"`
}

type LambdoSpawnResponse struct {
ID string `json:"ID"`
// Ports mapped by lambdo, leading to the requested ports
Ports []uint16 `json:"ports"`
}

Expand Down

0 comments on commit 72e40d8

Please sign in to comment.