2021-02-13 16:06:13 +01:00
|
|
|
package tasks
|
2021-02-13 15:51:14 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
2021-02-13 16:06:13 +01:00
|
|
|
_ "github.com/go-sql-driver/mysql" // Mysql Driver
|
2021-02-13 15:51:14 +01:00
|
|
|
)
|
|
|
|
|
2021-02-13 16:06:13 +01:00
|
|
|
// Dbhost : Database host (can be tweaked in makefile)
|
2021-02-13 15:51:14 +01:00
|
|
|
var Dbhost string
|
2021-02-13 16:06:13 +01:00
|
|
|
|
|
|
|
// Dbname : Database name (can be tweaked in makefile)
|
2021-02-13 15:51:14 +01:00
|
|
|
var Dbname string
|
2021-02-13 16:06:13 +01:00
|
|
|
|
|
|
|
// Dbuser : Database user (can be tweaked in makefile)
|
2021-02-13 15:51:14 +01:00
|
|
|
var Dbuser string
|
2021-02-13 16:06:13 +01:00
|
|
|
|
|
|
|
// Dbpass : Database password (can be tweaked in)
|
2021-02-13 15:51:14 +01:00
|
|
|
var Dbpass string
|
|
|
|
|
2021-02-13 17:04:57 +01:00
|
|
|
// 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 {
|
2021-02-13 15:51:14 +01:00
|
|
|
|
|
|
|
// Will try to connect to API database, which should be running locally under WS.
|
|
|
|
db, err := sql.Open("mysql", Dbuser+":"+Dbpass+"@tcp("+Dbhost+")/"+Dbname)
|
|
|
|
|
|
|
|
// if there is an error opening the connection, handle it
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// defer the close till after the main function has finished executing
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
// perform a db.Query insert
|
2021-02-13 17:04:57 +01:00
|
|
|
results, err := db.Query("SELECT * FROM tasks WHERE status = 0 ORDER BY created ASC LIMIT 1;")
|
2021-02-13 15:51:14 +01:00
|
|
|
|
|
|
|
// if there is an error inserting, handle it
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
|
2021-02-13 17:04:57 +01:00
|
|
|
var task Task
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-13 15:51:14 +01:00
|
|
|
// be careful deferring Queries if you are using transactions
|
2021-02-13 17:04:57 +01:00
|
|
|
defer results.Close()
|
|
|
|
|
|
|
|
return task
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateTaskStatus(taskID *string, newStatus *int) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func executeTask(task *Task) {
|
|
|
|
|
|
|
|
}
|
2021-02-13 15:51:14 +01:00
|
|
|
|
2021-02-13 17:04:57 +01:00
|
|
|
func publishTaskResult(taskID *string, result *string) {
|
2021-02-13 15:51:14 +01:00
|
|
|
|
|
|
|
}
|