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

This commit is contained in:
Paulo Truta
2021-02-13 17:04:57 +01:00
parent 8845cb11de
commit 4bb060ba5f
2 changed files with 46 additions and 16 deletions
+40 -6
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) {
}