Added base task execution algo

loop_loop_execution
Paulo Truta 2021-02-13 21:49:09 +01:00
parent 4bb060ba5f
commit 47df72777b
2 changed files with 61 additions and 16 deletions

View File

@ -13,6 +13,9 @@ import (
"github.com/edgebox-iot/sysctl/internal/tasks"
)
const defaultNotReadySleepTime time.Duration = time.Second * 60
const defaultSleepTime time.Duration = time.Second
func main() {
// load command line arguments
@ -56,9 +59,6 @@ func main() {
printDbDetails()
nextTask := tasks.GetNextTask()
log.Printf("Query result: Task: %s / Args: %s", nextTask.Task, nextTask.Args)
// infinite loop
for {
@ -103,13 +103,18 @@ func systemIterator(name *string) {
if isSystemReady() {
// Wait about 60 seconds before trying again.
log.Printf("System not ready. Next try will be executed in 60 seconds")
time.Sleep(time.Millisecond * time.Duration(60000))
time.Sleep(defaultNotReadySleepTime)
} else {
// Wait about 1 second before resumming operations.
log.Printf("Next instruction will be executed 1 second")
time.Sleep(time.Millisecond * time.Duration(1000))
task := tasks.GetNextTask()
log.Printf("Next instruction: %s(%s)", task.Task, task.Args)
time.Sleep(defaultSleepTime)
nextTask := tasks.GetNextTask()
if nextTask.Task != "" {
log.Printf("Executing task %s / Args: %s", nextTask.Task, nextTask.Args)
tasks.ExecuteTask(nextTask)
} else {
log.Printf("No tasks to execute.")
}
}

View File

@ -2,6 +2,8 @@ package tasks
import (
"database/sql"
"fmt"
"strconv"
_ "github.com/go-sql-driver/mysql" // Mysql Driver
)
@ -69,14 +71,52 @@ func GetNextTask() Task {
}
func updateTaskStatus(taskID *string, newStatus *int) {
}
func executeTask(task *Task) {
}
func publishTaskResult(taskID *string, result *string) {
// ExecuteTask : Performs execution of the given task, updating the task status as it goes, and publishing the task result
func ExecuteTask(task Task) Task {
db, err := sql.Open("mysql", Dbuser+":"+Dbpass+"@tcp("+Dbhost+")/"+Dbname)
if err != nil {
panic(err.Error())
}
defer db.Close()
statusUpdate, err := db.Query("UPDATE tasks SET status = 1 WHERE ID = " + strconv.Itoa(task.ID))
if err != nil {
panic(err.Error())
}
for statusUpdate.Next() {
}
switch task.Task {
case "setup_bootnode":
fmt.Println("Setting up bootnode connection...")
// ...
task.Result = sql.NullString{String: "{status: 'success'}", Valid: true}
case "start_edgeapp":
fmt.Println("Starting EdgeApp...")
// ...
case "stop_edgeapp":
fmt.Println("Stopping EdgeApp...")
// ...
}
if task.Result.Valid {
db.Query("Update tasks SET status = 2, result = '" + task.Result.String + "' WHERE ID = " + strconv.Itoa(task.ID) + ";")
} else {
db.Query("Update tasks SET status = 2, result = 'Invalid Task' WHERE ID = " + strconv.Itoa(task.ID) + ";")
}
if err != nil {
panic(err.Error())
}
returnTask := task
return returnTask
}