Implemented actual tested RunEdgeApp and StopEdgeApp commands, added sleep time after container operation and before checking status

loop_loop_execution
Paulo Truta 2021-02-18 22:59:14 +00:00
parent 0bd61ccf75
commit 3cc670b823
3 changed files with 90 additions and 23 deletions

View File

@ -5,6 +5,7 @@ import (
"log"
"os"
"strings"
"time"
"github.com/joho/godotenv"
@ -34,6 +35,7 @@ type EdgeAppService struct {
const configFilename = "/edgebox-compose.yml"
const envFilename = "/edgebox.env"
const defaultContainerOperationSleepTime time.Duration = time.Second * 10
// GetEdgeApps : Returns a list of EdgeApp struct filled with information
func GetEdgeApps() []EdgeApp {
@ -117,7 +119,7 @@ func GetEdgeAppServices(ID string) []EdgeAppService {
var edgeAppServices []EdgeAppService
for _, serviceID := range serviceSlices {
cmdArgs = []string{"-f", utils.GetPath("wsPath") + "/docker-compose.yml", "ps", "-q", serviceID}
cmdArgs = []string{"-f", utils.GetPath("wsPath") + "/docker-compose.yml", "exec", "-T", serviceID, "echo", "'Service Check'"}
cmdResult := utils.Exec("docker-compose", cmdArgs)
isRunning := false
if cmdResult != "" {
@ -133,8 +135,19 @@ func GetEdgeAppServices(ID string) []EdgeAppService {
// RunEdgeApp : Run an EdgeApp and return its most current status
func RunEdgeApp(ID string) EdgeAppStatus {
cmdArgs := []string{"-f", utils.GetPath("wsPath") + "/docker-compose.yml", "up", ID}
utils.Exec("docker-compose", cmdArgs)
services := GetEdgeAppServices(ID)
cmdArgs := []string{}
for _, service := range services {
cmdArgs = []string{"-f", utils.GetPath("wsPath") + "/docker-compose.yml", "start", service.ID}
utils.Exec("docker-compose", cmdArgs)
}
// Wait for it to settle up before continuing...
time.Sleep(defaultContainerOperationSleepTime)
return GetEdgeAppStatus(ID)
@ -143,8 +156,19 @@ func RunEdgeApp(ID string) EdgeAppStatus {
// StopEdgeApp : Stops an EdgeApp and return its most current status
func StopEdgeApp(ID string) EdgeAppStatus {
cmdArgs := []string{"-f", utils.GetPath("wsPath") + "/docker-compose.yml", "down", ID}
utils.Exec("docker-compose", cmdArgs)
services := GetEdgeAppServices(ID)
cmdArgs := []string{}
for _, service := range services {
cmdArgs = []string{"-f", utils.GetPath("wsPath") + "/docker-compose.yml", "stop", service.ID}
utils.Exec("docker-compose", cmdArgs)
}
// Wait for it to settle up before continuing...
time.Sleep(defaultContainerOperationSleepTime)
return GetEdgeAppStatus(ID)

View File

@ -31,6 +31,14 @@ type taskSetupTunnelArgs struct {
NodeName string `json:"node_name"`
}
type taskStartEdgeAppArgs struct {
ID string `json:"id"`
}
type taskStopEdgeAppArgs struct {
ID string `json:"id"`
}
// GetNextTask : Performs a MySQL query over the device's Edgebox API
func GetNextTask() Task {
@ -110,13 +118,29 @@ func ExecuteTask(task Task) Task {
}
case "start_edgeapp":
log.Printf("Starting EdgeApp...")
task.Result = sql.NullString{String: taskStartEdgeApp(), Valid: true}
// ...
log.Println("Starting EdgeApp...")
var args taskStartEdgeAppArgs
err := json.Unmarshal([]byte(task.Args), &args)
if err != nil {
log.Printf("Error reading arguments of start_edgeapp task: %s", err)
} else {
taskResult := taskStartEdgeApp(args)
task.Result = sql.NullString{String: taskResult, Valid: true}
}
case "stop_edgeapp":
log.Printf("Stopping EdgeApp...")
task.Result = sql.NullString{String: taskStopEdgeApp(), Valid: true}
// ...
log.Println("Stopping EdgeApp...")
var args taskStopEdgeAppArgs
err := json.Unmarshal([]byte(task.Args), &args)
if err != nil {
log.Printf("Error reading arguments of stop_edgeapp task: %s", err)
} else {
taskResult := taskStopEdgeApp(args)
task.Result = sql.NullString{String: taskResult, Valid: true}
}
}
}
@ -177,6 +201,34 @@ func taskSetupTunnel(args taskSetupTunnelArgs) string {
}
func taskStartEdgeApp(args taskStartEdgeAppArgs) string {
fmt.Println("Executing taskStartEdgeApp for " + args.ID)
result := edgeapps.RunEdgeApp(args.ID)
resultJSON, _ := json.Marshal(result)
taskGetEdgeApps() // This task will imediatelly update the entry in the api database.
return string(resultJSON)
}
func taskStopEdgeApp(args taskStopEdgeAppArgs) string {
fmt.Println("Executing taskStopEdgeApp for " + args.ID)
result := edgeapps.StopEdgeApp(args.ID)
resultJSON, _ := json.Marshal(result)
taskGetEdgeApps() // This task will imediatelly update the entry in the api database.
return string(resultJSON)
}
func taskGetEdgeApps() string {
fmt.Println("Executing taskGetEdgeApps")
@ -205,13 +257,3 @@ func taskGetEdgeApps() string {
return string(edgeAppsJSON)
}
func taskStartEdgeApp() string {
return "OK"
}
func taskStopEdgeApp() string {
return "OK"
}

View File

@ -4,6 +4,7 @@ import (
"bytes"
"log"
"os/exec"
"fmt"
"github.com/joho/godotenv"
)
@ -18,10 +19,10 @@ func Exec(command string, args []string) string {
err := cmd.Run()
if err != nil {
// TODO: Deal with possibility of error in command, allow explicit error handling and return proper formatted stderr
// log.Println(fmt.Sprint(err) + ": " + stderr.String()) ... Silence...
log.Println(fmt.Sprint(err) + ": " + stderr.String()) // ... Silence...
}
// log.Println("Result: " + out.String()) ... Silence ...
log.Println("Result: " + out.String()) // ... Silence ...
return out.String()