diff --git a/cmd/snapctl/flags.go b/cmd/snapctl/flags.go index 3f1c9b33b..1067d5974 100644 --- a/cmd/snapctl/flags.go +++ b/cmd/snapctl/flags.go @@ -21,10 +21,6 @@ package main import "github.com/codegangsta/cli" -const ( - DefaultMaxFailures = 10 -) - var ( // Main flags @@ -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 diff --git a/cmd/snapctl/task.go b/cmd/snapctl/task.go index b0818fe96..3013dca99 100644 --- a/cmd/snapctl/task.go +++ b/cmd/snapctl/task.go @@ -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) @@ -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 { @@ -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 diff --git a/core/task.go b/core/task.go index b5a1d212b..bdc7d9dbb 100644 --- a/core/task.go +++ b/core/task.go @@ -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 diff --git a/mgmt/rest/client/client_func_test.go b/mgmt/rest/client/client_func_test.go index d0c725d37..1c6920dca 100644 --- a/mgmt/rest/client/client_func_test.go +++ b/mgmt/rest/client/client_func_test.go @@ -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") }) @@ -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)") }) @@ -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") @@ -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") @@ -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 diff --git a/mgmt/rest/flags.go b/mgmt/rest/flags.go index 9b0b7f730..b6ef2dd0e 100644 --- a/mgmt/rest/flags.go +++ b/mgmt/rest/flags.go @@ -25,10 +25,6 @@ import ( "github.com/codegangsta/cli" ) -const ( - DefaultMaxFailures = 10 -) - var ( flAPIDisabled = cli.BoolFlag{ Name: "disable-api, d", diff --git a/scheduler/task.go b/scheduler/task.go index dbe8a33af..28b672395 100644 --- a/scheduler/task.go +++ b/scheduler/task.go @@ -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 (