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

Commit

Permalink
Fixes #1113: removes fixed 'max-failure' value from task creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom McSweeney committed Aug 10, 2016
1 parent 6309bcd commit 8dfc2f0
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 27 deletions.
9 changes: 2 additions & 7 deletions cmd/snapctl/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ package main

import "github.com/codegangsta/cli"

const (
DefaultMaxFailures = 10
)

var (

// Main flags
Expand Down Expand Up @@ -126,10 +122,9 @@ var (
Name: "deadline",
Usage: "The deadline for the task to be killed after started if the task runs too long (All tasks default to 5s)",
}
flTaskMaxFailures = cli.IntFlag{
flTaskMaxFailures = cli.StringFlag{
Name: "max-failures",
Usage: "The number of consecutive failures before snap disable the task (Default 10 consective failures)",
Value: DefaultMaxFailures,
Usage: "The number of consecutive failures before snap disable the task",
}

// metric
Expand Down
26 changes: 19 additions & 7 deletions cmd/snapctl/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ func createTask(ctx *cli.Context) error {
return err
}

func stringValToInt(val string) (int, error) {
// parse the input (string) value as an integer (log a Fatal
// error if we cannot parse the value as an integer)
parsedField, err := strconv.Atoi(val)
if err != nil {
splitErr := strings.Split(err.Error(), ": ")
errStr := splitErr[len(splitErr)-1]
// return a value of zero and the error encountered during string parsing
return 0, fmt.Errorf("Value '%v' cannot be parsed as an integer (%v)", val, errStr)
}
// return the integer equivalent of the input string and a nil error (indicating success)
return parsedField, nil
}

func createTaskUsingTaskManifest(ctx *cli.Context) error {
path := ctx.String("task-manifest")
ext := filepath.Ext(path)
Expand Down Expand Up @@ -129,12 +143,6 @@ func createTaskUsingTaskManifest(ctx *cli.Context) error {
return fmt.Errorf("Invalid version provided")
}

// If the number of failures does not specific, default value is 10
if t.MaxFailures == 0 {
fmt.Println("If the number of maximum failures is not specified, use default value of", DefaultMaxFailures)
t.MaxFailures = DefaultMaxFailures
}

r := pClient.CreateTask(t.Schedule, t.Workflow, t.Name, t.Deadline, !ctx.IsSet("no-start"), t.MaxFailures)

if r.Err != nil {
Expand Down Expand Up @@ -198,7 +206,11 @@ func createTaskUsingWFManifest(ctx *cli.Context) error {

// Deadline for a task
dl := ctx.String("deadline")
maxFailures := ctx.Int("max-failures")
maxFailuresStr := ctx.String("max-failures")
maxFailures, err := stringValToInt(maxFailuresStr)
if err != nil {
return fmt.Errorf("Value for command-line option 'max-failures' (%v) cannot be parsed as an integer\n", maxFailuresStr)
}

var sch *client.Schedule
// None of these mean it is a simple schedule
Expand Down
7 changes: 6 additions & 1 deletion core/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,12 @@ func CreateTaskFromContent(body io.ReadCloser,
if tr.Name != "" {
opts = append(opts, SetTaskName(tr.Name))
}
opts = append(opts, OptionStopOnFailure(10))

// if a MaxFailures value is included as part of the task creation request
if tr.MaxFailures > 0 {
// then set the appropriate value in the opts
opts = append(opts, OptionStopOnFailure(tr.MaxFailures))
}

if mode == nil {
mode = &tr.Start
Expand Down
12 changes: 6 additions & 6 deletions mgmt/rest/client/client_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func TestSnapClient(t *testing.T) {
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, rest.DefaultMaxFailures)
tt := c.CreateTask(sch, wf, "baron", "", true, 0)
So(tt.Err, ShouldNotBeNil)
So(tt.Err.Error(), ShouldContainSubstring, "Metric not found: /intel/mock/foo")
})
Expand Down Expand Up @@ -193,7 +193,7 @@ func TestSnapClient(t *testing.T) {
So(p.AvailablePlugins, ShouldBeEmpty)
})
Convey("invalid task (missing publisher)", func() {
tf := c.CreateTask(sch, wf, "baron", "", false, rest.DefaultMaxFailures)
tf := c.CreateTask(sch, wf, "baron", "", false, 0)
So(tf.Err, ShouldNotBeNil)
So(tf.Err.Error(), ShouldContainSubstring, "Plugin not found: type(publisher) name(mock-file)")
})
Expand Down Expand Up @@ -338,11 +338,11 @@ func TestSnapClient(t *testing.T) {
Convey("Tasks", func() {
Convey("Passing a bad task manifest", func() {
wfb := getWMFromSample("bad.json")
ttb := c.CreateTask(sch, wfb, "bad", "", true, rest.DefaultMaxFailures)
ttb := c.CreateTask(sch, wfb, "bad", "", true, 0)
So(ttb.Err, ShouldNotBeNil)
})

tf := c.CreateTask(sch, wf, "baron", "", false, rest.DefaultMaxFailures)
tf := c.CreateTask(sch, wf, "baron", "", false, 0)
Convey("valid task not started on creation", func() {
So(tf.Err, ShouldBeNil)
So(tf.Name, ShouldEqual, "baron")
Expand Down Expand Up @@ -385,7 +385,7 @@ func TestSnapClient(t *testing.T) {
})
})

tt := c.CreateTask(sch, wf, "baron", "", true, rest.DefaultMaxFailures)
tt := c.CreateTask(sch, wf, "baron", "", true, 0)
Convey("valid task started on creation", func() {
So(tt.Err, ShouldBeNil)
So(tt.Name, ShouldEqual, "baron")
Expand Down Expand Up @@ -473,7 +473,7 @@ func TestSnapClient(t *testing.T) {
Convey("event stream", func() {
rest.StreamingBufferWindow = 0.01
sch := &Schedule{Type: "simple", Interval: "100ms"}
tf := c.CreateTask(sch, wf, "baron", "", false, rest.DefaultMaxFailures)
tf := c.CreateTask(sch, wf, "baron", "", false, 0)

type ea struct {
events []string
Expand Down
4 changes: 0 additions & 4 deletions mgmt/rest/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ import (
"github.com/codegangsta/cli"
)

const (
DefaultMaxFailures = 10
)

var (
flAPIDisabled = cli.BoolFlag{
Name: "disable-api, d",
Expand Down
4 changes: 2 additions & 2 deletions scheduler/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ import (
const (
// DefaultDeadlineDuration - The default timeout is 5 second
DefaultDeadlineDuration = time.Second * 5
// DefaultStopOnFailure - The default stopping a failure is after three tries
DefaultStopOnFailure = 3
// DefaultStopOnFailure is used to set the number of failures before a task is disabled
DefaultStopOnFailure = 10
)

var (
Expand Down

0 comments on commit 8dfc2f0

Please sign in to comment.