Added TaskID as a field. Added a function UpdateHost() to update the host on which the task runs. Added a setter for TaskID. Added a comparator called Compare() that compares to instances of Task
This commit is contained in:
parent
e27912f99e
commit
0c53bb386f
1 changed files with 50 additions and 0 deletions
50
def/task.go
50
def/task.go
|
@ -1,9 +1,11 @@
|
||||||
package def
|
package def
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bitbucket.org/sunybingcloud/electron/constants"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"os"
|
"os"
|
||||||
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Task struct {
|
type Task struct {
|
||||||
|
@ -15,6 +17,7 @@ type Task struct {
|
||||||
CMD string `json:"cmd"`
|
CMD string `json:"cmd"`
|
||||||
Instances *int `json:"inst"`
|
Instances *int `json:"inst"`
|
||||||
Host string `json:"host"`
|
Host string `json:"host"`
|
||||||
|
TaskID string `json:"taskID"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func TasksFromJSON(uri string) ([]Task, error) {
|
func TasksFromJSON(uri string) ([]Task, error) {
|
||||||
|
@ -34,6 +37,34 @@ func TasksFromJSON(uri string) ([]Task, error) {
|
||||||
return tasks, nil
|
return tasks, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update the host on which the task needs to be scheduled.
|
||||||
|
func (tsk *Task) UpdateHost(new_host string) bool {
|
||||||
|
// Validation
|
||||||
|
is_correct_host := false
|
||||||
|
for _, existing_host := range constants.Hosts {
|
||||||
|
if host == existing_host {
|
||||||
|
is_correct_host = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !is_correct_host {
|
||||||
|
return false
|
||||||
|
} else {
|
||||||
|
tsk.Host = new_host
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the taskID of the task.
|
||||||
|
func (tsk *Task) SetTaskID(taskID string) bool {
|
||||||
|
// Validation
|
||||||
|
if taskID == "" {
|
||||||
|
return false
|
||||||
|
} else {
|
||||||
|
tsk.TaskID = taskID
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type WattsSorter []Task
|
type WattsSorter []Task
|
||||||
|
|
||||||
func (slice WattsSorter) Len() int {
|
func (slice WattsSorter) Len() int {
|
||||||
|
@ -47,3 +78,22 @@ func (slice WattsSorter) Less(i, j int) bool {
|
||||||
func (slice WattsSorter) Swap(i, j int) {
|
func (slice WattsSorter) Swap(i, j int) {
|
||||||
slice[i], slice[j] = slice[j], slice[i]
|
slice[i], slice[j] = slice[j], slice[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Compare two tasks.
|
||||||
|
func Compare(task1 *Task, task2 *Task) bool {
|
||||||
|
// If comparing the same pointers (checking the addresses).
|
||||||
|
if task1 == task2 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
// Checking member equality
|
||||||
|
if reflect.DeepEqual(*task1, *task2) {
|
||||||
|
// Need to check for the task ID
|
||||||
|
if task1.TaskID == task2.TaskID {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Reference in a new issue