Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Modified task error codes for #467
Browse files Browse the repository at this point in the history
  • Loading branch information
tiffanyfay committed Nov 5, 2015
1 parent 84bcd9b commit 7743c3a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 20 deletions.
6 changes: 3 additions & 3 deletions mgmt/rest/client/client_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ func TestPulseClient(t *testing.T) {
Convey("StopTask", func() {
t1 := c.StopTask(uuid)
So(t1.Err, ShouldNotBeNil)
So(t1.Err.Error(), ShouldEqual, fmt.Sprintf("error 0: No task found with ID '%s' ", uuid))
So(t1.Err.Error(), ShouldEqual, fmt.Sprintf("error 0: Task not found: ID(%s) ", uuid))
})
Convey("RemoveTask", func() {
t1 := c.RemoveTask(uuid)
So(t1.Err, ShouldNotBeNil)
So(t1.Err.Error(), ShouldEqual, fmt.Sprintf("No task found with ID '%s'", uuid))
So(t1.Err.Error(), ShouldEqual, fmt.Sprintf("Task not found: ID(%s)", uuid))
})
Convey("invalid task (missing metric)", func() {
tt := c.CreateTask(sch, wf, "baron", true)
Expand Down Expand Up @@ -389,7 +389,7 @@ func TestPulseClient(t *testing.T) {
}
}()
<-wait
So(r.Err.Error(), ShouldEqual, "No task found with ID '1'")
So(r.Err.Error(), ShouldEqual, "Task not found: ID(1)")
})
Convey("event stream", func() {
rest.StreamingBufferWindow = 0.01
Expand Down
33 changes: 27 additions & 6 deletions mgmt/rest/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"io"
"net/http"
"sort"
"strings"
"time"

log "github.com/Sirupsen/logrus"
Expand All @@ -42,6 +43,7 @@ var (
StreamingBufferWindow = 0.1

ErrStreamingUnsupported = errors.New("Streaming unsupported")
ErrTaskNotFound = errors.New("Task not found")
)

type configItem struct {
Expand Down Expand Up @@ -153,7 +155,11 @@ func (s *Server) watchTask(w http.ResponseWriter, r *http.Request, p httprouter.
}
tc, err1 := s.mt.WatchTask(id, tw)
if err1 != nil {
respond(404, rbody.FromError(err1), w)
if strings.Contains(err1.Error(), ErrTaskNotFound.Error()) {
respond(404, rbody.FromError(err1), w)
return
}
respond(500, rbody.FromError(err1), w)
return
}

Expand Down Expand Up @@ -220,15 +226,18 @@ func (s *Server) watchTask(w http.ResponseWriter, r *http.Request, p httprouter.
// exit since this client is no longer listening
respond(200, &rbody.ScheduledTaskWatchingEnded{}, w)
}

}
}

func (s *Server) startTask(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
id := p.ByName("id")
errs := s.mt.StartTask(id)
if errs != nil {
respond(404, rbody.FromPulseErrors(errs), w)
if strings.Contains(errs[0].Error(), ErrTaskNotFound.Error()) {
respond(404, rbody.FromPulseErrors(errs), w)
return
}
respond(500, rbody.FromPulseErrors(errs), w)
return
}
// TODO should return resource
Expand All @@ -239,7 +248,11 @@ func (s *Server) stopTask(w http.ResponseWriter, r *http.Request, p httprouter.P
id := p.ByName("id")
errs := s.mt.StopTask(id)
if errs != nil {
respond(404, rbody.FromPulseErrors(errs), w)
if strings.Contains(errs[0].Error(), ErrTaskNotFound.Error()) {
respond(404, rbody.FromPulseErrors(errs), w)
return
}
respond(500, rbody.FromPulseErrors(errs), w)
return
}
respond(200, &rbody.ScheduledTaskStopped{ID: id}, w)
Expand All @@ -249,7 +262,11 @@ func (s *Server) removeTask(w http.ResponseWriter, r *http.Request, p httprouter
id := p.ByName("id")
err := s.mt.RemoveTask(id)
if err != nil {
respond(404, rbody.FromError(err), w)
if strings.Contains(err.Error(), ErrTaskNotFound.Error()) {
respond(404, rbody.FromError(err), w)
return
}
respond(500, rbody.FromError(err), w)
return
}
respond(200, &rbody.ScheduledTaskRemoved{ID: id}, w)
Expand All @@ -260,7 +277,11 @@ func (s *Server) enableTask(w http.ResponseWriter, r *http.Request, p httprouter
id := p.ByName("id")
tsk, err := s.mt.EnableTask(id)
if err != nil {
respond(404, rbody.FromError(err), w)
if strings.Contains(err.Error(), ErrTaskNotFound.Error()) {
respond(404, rbody.FromError(err), w)
return
}
respond(500, rbody.FromError(err), w)
return
}
task := &rbody.ScheduledTaskEnabled{}
Expand Down
37 changes: 26 additions & 11 deletions scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,11 @@ func (s *scheduler) CreateTask(sch schedule.Schedule, wfMap *wmap.WorkflowMap, s
func (s *scheduler) RemoveTask(id string) error {
t, err := s.getTask(id)
if err != nil {
log.WithFields(log.Fields{
"_module": "scheduler",
"block": "RemoveTask",
"task id": id,
}).Error(ErrTaskNotFound)
s.logger.WithFields(log.Fields{
"_block": "remove-task",
"_error": ErrTaskNotFound,
"task-id": id,
}).Error("error removing task")
return err
}
event := &scheduler_event.TaskDeletedEvent{
Expand All @@ -245,6 +245,11 @@ func (s *scheduler) GetTasks() map[string]core.Task {
func (s *scheduler) GetTask(id string) (core.Task, error) {
t, err := s.getTask(id)
if err != nil {
s.logger.WithFields(log.Fields{
"_block": "get-task",
"_error": ErrTaskNotFound,
"task-id": id,
}).Error("error getting task")
return nil, err // We do this to send back an explicit nil on the interface
}
return t, nil
Expand All @@ -254,6 +259,11 @@ func (s *scheduler) GetTask(id string) (core.Task, error) {
func (s *scheduler) StartTask(id string) []perror.PulseError {
t, err := s.getTask(id)
if err != nil {
s.logger.WithFields(log.Fields{
"_block": "start-task",
"_error": ErrTaskNotFound,
"task-id": id,
}).Error("error starting task")
return []perror.PulseError{
perror.New(err),
}
Expand Down Expand Up @@ -295,9 +305,9 @@ func (s *scheduler) StopTask(id string) []perror.PulseError {
if err != nil {
s.logger.WithFields(log.Fields{
"_block": "stop-task",
"_error": err.Error(),
"_error": ErrTaskNotFound,
"task-id": id,
}).Warning("error on stopping of task")
}).Error("error stopping task")
return []perror.PulseError{
perror.New(err),
}
Expand Down Expand Up @@ -339,9 +349,9 @@ func (s *scheduler) EnableTask(id string) (core.Task, error) {
if e != nil {
s.logger.WithFields(log.Fields{
"_block": "enable-task",
"_error": e.Error(),
"_error": ErrTaskNotFound,
"task-id": id,
}).Warning("error on enabling a task")
}).Error("error enabling task")
return nil, e
}

Expand All @@ -351,7 +361,7 @@ func (s *scheduler) EnableTask(id string) (core.Task, error) {
"_block": "enable-task",
"_error": err.Error(),
"task-id": id,
}).Warning("error on enabling a task")
}).Error("error enabling task")
return nil, err
}
s.logger.WithFields(log.Fields{
Expand Down Expand Up @@ -402,6 +412,11 @@ func (s *scheduler) SetMetricManager(mm managesMetrics) {
func (s *scheduler) WatchTask(id string, tw core.TaskWatcherHandler) (core.TaskWatcherCloser, error) {
task, err := s.getTask(id)
if err != nil {
s.logger.WithFields(log.Fields{
"_block": "watch-task",
"_error": ErrTaskNotFound,
"task-id": id,
}).Error("error watching task")
return nil, err
}
return s.taskWatcherColl.add(task.ID(), tw)
Expand Down Expand Up @@ -470,7 +485,7 @@ func (s *scheduler) HandleGomitEvent(e gomit.Event) {
func (s *scheduler) getTask(id string) (*task, error) {
task := s.tasks.Get(id)
if task == nil {
return nil, fmt.Errorf("No task found with ID '%v'", id)
return nil, fmt.Errorf("%v: ID(%v)", ErrTaskNotFound, id)
}
return task, nil
}
Expand Down

0 comments on commit 7743c3a

Please sign in to comment.