From 3a250a179b803dfa45e95ec4bbbc2673784b9714 Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Tue, 17 Nov 2015 14:21:14 -0800 Subject: [PATCH] Exctacted a method for parsing checks --- api/tasks.go | 3 +++ jobspec/parse.go | 58 +++++++++++++++++++++++----------------- nomad/structs/structs.go | 2 +- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/api/tasks.go b/api/tasks.go index 27b712f3d..2990b5433 100644 --- a/api/tasks.go +++ b/api/tasks.go @@ -26,6 +26,9 @@ type ServiceCheck struct { Id string Name string Type string + Script string + Http string + Protocol string Interval time.Duration Timeout time.Duration } diff --git a/jobspec/parse.go b/jobspec/parse.go index 915a7d94b..725006be8 100644 --- a/jobspec/parse.go +++ b/jobspec/parse.go @@ -490,31 +490,8 @@ func parseServices(task *structs.Task, serviceObjs *ast.ObjectList) error { } if co := checkList.Filter("check"); len(co.Items) > 0 { - service.Checks = make([]structs.ServiceCheck, len(co.Items)) - for idx, co := range co.Items { - var check structs.ServiceCheck - label := co.Keys[0].Token.Value().(string) - var cm map[string]interface{} - if err := hcl.DecodeObject(&cm, co.Val); err != nil { - return err - } - dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{ - DecodeHook: mapstructure.StringToTimeDurationHookFunc(), - WeaklyTypedInput: true, - Result: &check, - }) - if err != nil { - return err - } - if err := dec.Decode(cm); err != nil { - return err - } - - check.Id = label - if check.Name == "" { - check.Name = label - } - service.Checks[idx] = check + if err := parseChecks(&service, co); err != nil { + return err } } @@ -524,6 +501,37 @@ func parseServices(task *structs.Task, serviceObjs *ast.ObjectList) error { return nil } +func parseChecks(service *structs.Service, checkObjs *ast.ObjectList) error { + service.Checks = make([]structs.ServiceCheck, len(checkObjs.Items)) + for idx, co := range checkObjs.Items { + var check structs.ServiceCheck + label := co.Keys[0].Token.Value().(string) + var cm map[string]interface{} + if err := hcl.DecodeObject(&cm, co.Val); err != nil { + return err + } + dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{ + DecodeHook: mapstructure.StringToTimeDurationHookFunc(), + WeaklyTypedInput: true, + Result: &check, + }) + if err != nil { + return err + } + if err := dec.Decode(cm); err != nil { + return err + } + + check.Id = label + if check.Name == "" { + check.Name = label + } + service.Checks[idx] = check + } + + return nil +} + func parseResources(result *structs.Resources, list *ast.ObjectList) error { list = list.Elem() if len(list.Items) == 0 { diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index f7dde55b1..c76c3fe23 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -1025,7 +1025,7 @@ func (sc *ServiceCheck) Validate() error { // The Service model represents a Consul service defintion type Service struct { - Id string // Id of the service + Id string // Id of the service, this needs to be unique on a local machine Name string // Name of the service, defaults to id Tags []string // List of tags for the service PortLabel string `mapstructure:"port"` // port for the service