Laying out the structure of the Tasks package, and how it will be called in the systemIterator in main

loop_loop_execution
Paulo Truta 2021-02-13 17:04:57 +01:00
parent 8845cb11de
commit 4bb060ba5f
2 changed files with 46 additions and 16 deletions

View File

@ -56,8 +56,8 @@ func main() {
printDbDetails()
dbQueryResult := tasks.PerformQuery()
log.Printf("Query result: %s", dbQueryResult)
nextTask := tasks.GetNextTask()
log.Printf("Query result: Task: %s / Args: %s", nextTask.Task, nextTask.Args)
// infinite loop
for {
@ -98,15 +98,9 @@ func isDatabaseReady() bool {
return false
}
// getNextInstruction : Retrieves next instruction from the database
func getNextInstruction(name *string) string {
log.Printf("Fetching next instruction for %s", *name)
return "Test Instruction Command"
}
func systemIterator(name *string) {
if !isSystemReady() {
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))
@ -114,7 +108,9 @@ func systemIterator(name *string) {
// Wait about 1 second before resumming operations.
log.Printf("Next instruction will be executed 1 second")
time.Sleep(time.Millisecond * time.Duration(1000))
log.Printf("Next instruction: %s", getNextInstruction(name))
task := tasks.GetNextTask()
log.Printf("Next instruction: %s(%s)", task.Task, task.Args)
}
}

View File

@ -18,8 +18,19 @@ var Dbuser string
// Dbpass : Database password (can be tweaked in)
var Dbpass string
// PerformQuery : Performs a MySQL query over the device's Edgebox API
func PerformQuery() string {
// Task : Struct for Task type
type Task struct {
ID int `json:"id"`
Task string `json:"task"`
Args string `json:"args"`
Status string `json:"status"`
Result sql.NullString `json:"result"` // Database fields that can be null must use the sql.NullString type
Created string `json:"created"`
Updated string `json:"updated"`
}
// GetNextTask : Performs a MySQL query over the device's Edgebox API
func GetNextTask() Task {
// Will try to connect to API database, which should be running locally under WS.
db, err := sql.Open("mysql", Dbuser+":"+Dbpass+"@tcp("+Dbhost+")/"+Dbname)
@ -33,16 +44,39 @@ func PerformQuery() string {
defer db.Close()
// perform a db.Query insert
insert, err := db.Query("INSERT INTO options (name, value) VALUES ( 'TEST_OPTION_SYSCTL', 'TEST' );")
results, err := db.Query("SELECT * FROM tasks WHERE status = 0 ORDER BY created ASC LIMIT 1;")
// if there is an error inserting, handle it
if err != nil {
panic(err.Error())
}
// be careful deferring Queries if you are using transactions
defer insert.Close()
var task Task
return "OK"
for results.Next() {
// for each row, scan the result into our task composite object
err = results.Scan(&task.ID, &task.Task, &task.Args, &task.Status, &task.Result, &task.Created, &task.Updated)
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
}
// be careful deferring Queries if you are using transactions
defer results.Close()
return task
}
func updateTaskStatus(taskID *string, newStatus *int) {
}
func executeTask(task *Task) {
}
func publishTaskResult(taskID *string, result *string) {
}