github.com/cloudfoundry-attic/ltc@v0.0.0-20151123212628-098adc7919fc/task_examiner/task_examiner.go (about) 1 package task_examiner 2 3 import ( 4 "errors" 5 6 "github.com/cloudfoundry-incubator/receptor" 7 ) 8 9 const TaskNotFoundErrorMessage = "Task not found." 10 11 type TaskInfo struct { 12 TaskGuid string 13 State string 14 CellID string 15 Failed bool 16 FailureReason string 17 Result string 18 } 19 20 //go:generate counterfeiter -o fake_task_examiner/fake_task_examiner.go . TaskExaminer 21 type TaskExaminer interface { 22 TaskStatus(taskName string) (TaskInfo, error) 23 ListTasks() ([]TaskInfo, error) 24 } 25 26 type taskExaminer struct { 27 receptorClient receptor.Client 28 } 29 30 func New(receptorClient receptor.Client) TaskExaminer { 31 return &taskExaminer{receptorClient} 32 } 33 34 func (e *taskExaminer) TaskStatus(taskName string) (TaskInfo, error) { 35 taskResponse, err := e.receptorClient.GetTask(taskName) 36 if err != nil { 37 if receptorError, ok := err.(receptor.Error); ok { 38 if receptorError.Type == receptor.TaskNotFound { 39 return TaskInfo{}, errors.New(TaskNotFoundErrorMessage) 40 } 41 } 42 return TaskInfo{}, err 43 } 44 45 return TaskInfo{ 46 TaskGuid: taskResponse.TaskGuid, 47 State: taskResponse.State, 48 CellID: taskResponse.CellID, 49 Failed: taskResponse.Failed, 50 FailureReason: taskResponse.FailureReason, 51 Result: taskResponse.Result, 52 }, nil 53 } 54 55 func (e *taskExaminer) ListTasks() ([]TaskInfo, error) { 56 taskList, err := e.receptorClient.Tasks() 57 if err != nil { 58 return nil, err 59 } 60 taskInfoList := make([]TaskInfo, 0, len(taskList)) 61 for _, task := range taskList { 62 taskInfo := TaskInfo{ 63 TaskGuid: task.TaskGuid, 64 CellID: task.CellID, 65 Failed: task.Failed, 66 FailureReason: task.FailureReason, 67 Result: task.Result, 68 State: task.State, 69 } 70 taskInfoList = append(taskInfoList, taskInfo) 71 } 72 return taskInfoList, err 73 }