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

Commit

Permalink
Fix for #489 - disabled task shouldn't be allowed to start
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzej-k committed Dec 3, 2015
1 parent 0f26b68 commit 620f91f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
9 changes: 7 additions & 2 deletions mgmt/rest/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ var (
// The amount of time to buffer streaming events before flushing in seconds
StreamingBufferWindow = 0.1

ErrStreamingUnsupported = errors.New("Streaming unsupported")
ErrTaskNotFound = errors.New("Task not found")
ErrStreamingUnsupported = errors.New("Streaming unsupported")
ErrTaskNotFound = errors.New("Task not found")
ErrTaskDisabledNotRunnable = errors.New("Task is disabled. Cannot be started")
)

type configItem struct {
Expand Down Expand Up @@ -240,6 +241,10 @@ func (s *Server) startTask(w http.ResponseWriter, r *http.Request, p httprouter.
respond(404, rbody.FromSnapErrors(errs), w)
return
}
if strings.Contains(errs[0].Error(), ErrTaskDisabledNotRunnable.Error()) {
respond(409, rbody.FromSnapErrors(errs), w)
return
}
respond(500, rbody.FromSnapErrors(errs), w)
return
}
Expand Down
17 changes: 13 additions & 4 deletions scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ import (
var (
HandlerRegistrationName = "scheduler"

ErrMetricManagerNotSet = errors.New("MetricManager is not set.")
ErrSchedulerNotStarted = errors.New("Scheduler is not started.")
ErrTaskAlreadyRunning = errors.New("Task is already running.")
ErrTaskAlreadyStopped = errors.New("Task is already stopped.")
ErrMetricManagerNotSet = errors.New("MetricManager is not set.")
ErrSchedulerNotStarted = errors.New("Scheduler is not started.")
ErrTaskAlreadyRunning = errors.New("Task is already running.")
ErrTaskAlreadyStopped = errors.New("Task is already stopped.")
ErrTaskDisabledNotRunnable = errors.New("Task is disabled. Cannot be started.")
)

type schedulerState int
Expand Down Expand Up @@ -305,6 +306,14 @@ func (s *scheduler) startTask(id, source string) []serror.SnapError {
}
}

if t.state == core.TaskDisabled {
logger.WithFields(log.Fields{
"task-id": t.ID(),
}).Error("Task is disabled and must be enabled before starting")
return []serror.SnapError{
serror.New(ErrTaskDisabledNotRunnable),
}
}
if t.state == core.TaskFiring || t.state == core.TaskSpinning {
logger.WithFields(log.Fields{
"task-id": t.ID(),
Expand Down
11 changes: 11 additions & 0 deletions scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,17 @@ func TestScheduler(t *testing.T) {
So(err1, ShouldBeNil)
So(etsk.State(), ShouldEqual, core.TaskStopped)
})
Convey("Start disabled task", func() {
tsk, _ := s.CreateTask(schedule.NewSimpleSchedule(time.Millisecond*100), w, false)
So(tsk, ShouldNotBeNil)

t := s.tasks.Get(tsk.ID())
t.state = core.TaskDisabled

err := s.StartTask(tsk.ID())
So(err[0].Error(), ShouldResemble, "Task is disabled. Cannot be started.")
So(t.state, ShouldEqual, core.TaskDisabled)
})
})
Convey("Stop()", t, func() {
Convey("Should set scheduler state to SchedulerStopped", func() {
Expand Down

0 comments on commit 620f91f

Please sign in to comment.