Esempio n. 1
0
// TestDefaultMeta tests that WorkSpec.Meta gets the correct defaults,
// which in a couple of cases are not zero values.
func (s *Suite) TestDefaultMeta(c *check.C) {
	var (
		err  error
		spec coordinate.WorkSpec
		meta coordinate.WorkSpecMeta
	)
	spec, err = s.Namespace.SetWorkSpec(map[string]interface{}{
		"name":   "spec",
		"min_gb": 1,
	})
	c.Assert(err, check.IsNil)

	meta, err = spec.Meta(false)
	c.Assert(err, check.IsNil)
	c.Check(meta.Priority, check.Equals, 0)
	c.Check(meta.Weight, check.Equals, 20)
	c.Check(meta.Paused, check.Equals, false)
	c.Check(meta.Continuous, check.Equals, false)
	c.Check(meta.CanBeContinuous, check.Equals, false)
	c.Check(meta.Interval, check.Equals, time.Duration(0))
	c.Check(meta.NextContinuous, check.Equals, time.Time{})
	c.Check(meta.MaxRunning, check.Equals, 0)
	c.Check(meta.MaxAttemptsReturned, check.Equals, 0)
	c.Check(meta.NextWorkSpecName, check.Equals, "")
	c.Check(meta.AvailableCount, check.Equals, 0)
	c.Check(meta.PendingCount, check.Equals, 0)
}
Esempio n. 2
0
// TestMetaContinuous specifically checks that you cannot enable the
// "continuous" flag on non-continuous work specs.
func (s *Suite) TestMetaContinuous(c *check.C) {
	var (
		err  error
		spec coordinate.WorkSpec
		meta coordinate.WorkSpecMeta
	)

	// ...also...
	spec, err = s.Namespace.SetWorkSpec(map[string]interface{}{
		"name":   "spec",
		"min_gb": 1,
	})
	c.Assert(err, check.IsNil)

	meta, err = spec.Meta(false)
	c.Assert(err, check.IsNil)
	c.Check(meta.Continuous, check.Equals, false)
	c.Check(meta.CanBeContinuous, check.Equals, false)

	meta.Continuous = true
	err = spec.SetMeta(meta)
	c.Assert(err, check.IsNil)

	meta, err = spec.Meta(false)
	c.Assert(err, check.IsNil)
	// Cannot set the "continuous" flag
	c.Check(meta.Continuous, check.Equals, false)
	c.Check(meta.CanBeContinuous, check.Equals, false)
}
Esempio n. 3
0
// TestPrefilledMeta tests that WorkSpec.Meta() fills in correctly from
// "magic" keys in a work spec.
func (s *Suite) TestPrefilledMeta(c *check.C) {
	var (
		err  error
		spec coordinate.WorkSpec
		meta coordinate.WorkSpecMeta
	)
	spec, err = s.Namespace.SetWorkSpec(map[string]interface{}{
		"name":        "spec",
		"min_gb":      1,
		"priority":    10,
		"weight":      100,
		"disabled":    true,
		"continuous":  true,
		"interval":    60,
		"max_running": 10,
		"max_getwork": 1,
		"then":        "spec2",
	})
	c.Assert(err, check.IsNil)

	meta, err = spec.Meta(false)
	c.Assert(err, check.IsNil)
	c.Check(meta.Priority, check.Equals, 10)
	c.Check(meta.Weight, check.Equals, 100)
	c.Check(meta.Paused, check.Equals, true)
	c.Check(meta.Continuous, check.Equals, true)
	c.Check(meta.CanBeContinuous, check.Equals, true)
	c.Check(meta.Interval, check.Equals, time.Duration(60)*time.Second)
	c.Check(meta.NextContinuous, check.Equals, time.Time{})
	c.Check(meta.MaxRunning, check.Equals, 10)
	c.Check(meta.MaxAttemptsReturned, check.Equals, 1)
	c.Check(meta.NextWorkSpecName, check.Equals, "spec2")
	c.Check(meta.AvailableCount, check.Equals, 0)
	c.Check(meta.PendingCount, check.Equals, 0)
}
Esempio n. 4
0
// ControlWorkSpec makes changes to a work spec that are not directly
// reflected in the work spec definition.  This allows work specs to
// be paused or to stop generating new continuous jobs.
// ControlWorkSpecOptions has a complete listing of what can be done.
func (jobs *JobServer) ControlWorkSpec(workSpecName string, options map[string]interface{}) (bool, string, error) {
	var (
		cwsOptions ControlWorkSpecOptions
		decoder    *mapstructure.Decoder
		err        error
		metadata   mapstructure.Metadata
		workSpec   coordinate.WorkSpec
		wsMeta     coordinate.WorkSpecMeta
	)

	workSpec, err = jobs.Namespace.WorkSpec(workSpecName)
	if err == nil {
		// We care a lot about "false" vs. not present for
		// these things.  Manually create the decoder.
		config := mapstructure.DecoderConfig{
			Result:   &cwsOptions,
			Metadata: &metadata,
		}
		decoder, err = mapstructure.NewDecoder(&config)
	}
	if err == nil {
		err = decoder.Decode(options)
	}

	// Get the existing metadata, then change it based on what
	// we got provided
	if err == nil {
		wsMeta, err = workSpec.Meta(false)
	}
	if err == nil {
		for _, key := range metadata.Keys {
			switch key {
			case "Continuous":
				wsMeta.Continuous = cwsOptions.Continuous
			case "Status":
				wsMeta.Paused = cwsOptions.Status == Paused
			case "Weight":
				wsMeta.Weight = cwsOptions.Weight
			case "Interval":
				wsMeta.Interval = time.Duration(cwsOptions.Interval) * time.Second
			case "MaxRunning":
				wsMeta.MaxRunning = cwsOptions.MaxRunning
			}
		}
	}
	if err == nil {
		err = workSpec.SetMeta(wsMeta)
	}
	return err == nil, "", err
}
Esempio n. 5
0
// GetWorkSpecMeta returns a set of control options for a given work
// spec.  The returned map has the full set of keys that
// ControlWorkSpec() will accept.
func (jobs *JobServer) GetWorkSpecMeta(workSpecName string) (result map[string]interface{}, _ string, err error) {
	var (
		workSpec coordinate.WorkSpec
		meta     coordinate.WorkSpecMeta
	)

	workSpec, err = jobs.Namespace.WorkSpec(workSpecName)
	if err == nil {
		meta, err = workSpec.Meta(false)
	}
	if err == nil {
		result = make(map[string]interface{})
		if meta.Paused {
			result["status"] = Paused
		} else {
			result["status"] = Runnable
		}
		result["continuous"] = meta.Continuous
		result["interval"] = meta.Interval.Seconds()
		result["max_running"] = meta.MaxRunning
		result["weight"] = meta.Weight
	}
	return
}
Esempio n. 6
0
// TestSetMeta tests the basic SetMeta() call and a couple of its
// documented oddities.
func (s *Suite) TestSetMeta(c *check.C) {
	var (
		err  error
		spec coordinate.WorkSpec
		meta coordinate.WorkSpecMeta
	)
	spec, err = s.Namespace.SetWorkSpec(map[string]interface{}{
		"name":       "spec",
		"min_gb":     1,
		"continuous": true,
	})
	c.Assert(err, check.IsNil)

	meta, err = spec.Meta(false)
	c.Assert(err, check.IsNil)
	c.Check(meta.Priority, check.Equals, 0)
	c.Check(meta.Weight, check.Equals, 20)
	c.Check(meta.Paused, check.Equals, false)
	c.Check(meta.Continuous, check.Equals, true)
	c.Check(meta.CanBeContinuous, check.Equals, true)
	c.Check(meta.Interval, check.Equals, time.Duration(0))
	c.Check(meta.NextContinuous, check.Equals, time.Time{})
	c.Check(meta.MaxRunning, check.Equals, 0)
	c.Check(meta.MaxAttemptsReturned, check.Equals, 0)
	c.Check(meta.NextWorkSpecName, check.Equals, "")
	c.Check(meta.AvailableCount, check.Equals, 0)
	c.Check(meta.PendingCount, check.Equals, 0)

	err = spec.SetMeta(coordinate.WorkSpecMeta{
		Priority:            10,
		Weight:              100,
		Paused:              true,
		Continuous:          false,
		CanBeContinuous:     false,
		Interval:            time.Duration(60) * time.Second,
		MaxRunning:          10,
		MaxAttemptsReturned: 1,
		NextWorkSpecName:    "then",
		AvailableCount:      100,
		PendingCount:        50,
	})
	c.Assert(err, check.IsNil)

	meta, err = spec.Meta(false)
	c.Assert(err, check.IsNil)
	c.Check(meta.Priority, check.Equals, 10)
	c.Check(meta.Weight, check.Equals, 100)
	c.Check(meta.Paused, check.Equals, true)
	c.Check(meta.Continuous, check.Equals, false)
	// Cannot clear "can be continuous" flag
	c.Check(meta.CanBeContinuous, check.Equals, true)
	c.Check(meta.Interval, check.Equals, time.Duration(60)*time.Second)
	c.Check(meta.NextContinuous, check.Equals, time.Time{})
	c.Check(meta.MaxRunning, check.Equals, 10)
	c.Check(meta.MaxAttemptsReturned, check.Equals, 1)
	// Cannot change following work spec
	c.Check(meta.NextWorkSpecName, check.Equals, "")
	// Cannot set the counts
	c.Check(meta.AvailableCount, check.Equals, 0)
	c.Check(meta.PendingCount, check.Equals, 0)
}