Laying out the structure of the Tasks package, and how it will be called in the systemIterator in main
parent
8845cb11de
commit
4bb060ba5f
|
@ -56,8 +56,8 @@ func main() {
|
||||||
|
|
||||||
printDbDetails()
|
printDbDetails()
|
||||||
|
|
||||||
dbQueryResult := tasks.PerformQuery()
|
nextTask := tasks.GetNextTask()
|
||||||
log.Printf("Query result: %s", dbQueryResult)
|
log.Printf("Query result: Task: %s / Args: %s", nextTask.Task, nextTask.Args)
|
||||||
|
|
||||||
// infinite loop
|
// infinite loop
|
||||||
for {
|
for {
|
||||||
|
@ -98,15 +98,9 @@ func isDatabaseReady() bool {
|
||||||
return false
|
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) {
|
func systemIterator(name *string) {
|
||||||
|
|
||||||
if !isSystemReady() {
|
if isSystemReady() {
|
||||||
// Wait about 60 seconds before trying again.
|
// Wait about 60 seconds before trying again.
|
||||||
log.Printf("System not ready. Next try will be executed in 60 seconds")
|
log.Printf("System not ready. Next try will be executed in 60 seconds")
|
||||||
time.Sleep(time.Millisecond * time.Duration(60000))
|
time.Sleep(time.Millisecond * time.Duration(60000))
|
||||||
|
@ -114,7 +108,9 @@ func systemIterator(name *string) {
|
||||||
// Wait about 1 second before resumming operations.
|
// Wait about 1 second before resumming operations.
|
||||||
log.Printf("Next instruction will be executed 1 second")
|
log.Printf("Next instruction will be executed 1 second")
|
||||||
time.Sleep(time.Millisecond * time.Duration(1000))
|
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)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,8 +18,19 @@ var Dbuser string
|
||||||
// Dbpass : Database password (can be tweaked in)
|
// Dbpass : Database password (can be tweaked in)
|
||||||
var Dbpass string
|
var Dbpass string
|
||||||
|
|
||||||
// PerformQuery : Performs a MySQL query over the device's Edgebox API
|
// Task : Struct for Task type
|
||||||
func PerformQuery() string {
|
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.
|
// Will try to connect to API database, which should be running locally under WS.
|
||||||
db, err := sql.Open("mysql", Dbuser+":"+Dbpass+"@tcp("+Dbhost+")/"+Dbname)
|
db, err := sql.Open("mysql", Dbuser+":"+Dbpass+"@tcp("+Dbhost+")/"+Dbname)
|
||||||
|
@ -33,16 +44,39 @@ func PerformQuery() string {
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
// perform a db.Query insert
|
// 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 there is an error inserting, handle it
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// be careful deferring Queries if you are using transactions
|
var task Task
|
||||||
defer insert.Close()
|
|
||||||
|
|
||||||
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) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue