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:
Pradyumna Kaushik 2016-11-10 19:47:03 -05:00 committed by Renan DelValle
parent e27912f99e
commit 0c53bb386f

View file

@ -1,9 +1,11 @@
package def
import (
"bitbucket.org/sunybingcloud/electron/constants"
"encoding/json"
"github.com/pkg/errors"
"os"
"reflect"
)
type Task struct {
@ -15,6 +17,7 @@ type Task struct {
CMD string `json:"cmd"`
Instances *int `json:"inst"`
Host string `json:"host"`
TaskID string `json:"taskID"`
}
func TasksFromJSON(uri string) ([]Task, error) {
@ -34,6 +37,34 @@ func TasksFromJSON(uri string) ([]Task, error) {
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
func (slice WattsSorter) Len() int {
@ -47,3 +78,22 @@ func (slice WattsSorter) Less(i, j int) bool {
func (slice WattsSorter) Swap(i, j int) {
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
}
}