Example #1
0
func createWorkSpecs(namespace coordinate.Namespace) error {
	var err error
	_, err = namespace.SetWorkSpec(map[string]interface{}{
		"name":       "generator",
		"runtime":    "go",
		"task":       "generator",
		"continuous": true,
		"interval":   5,
		"then":       "runner",
	})
	if err != nil {
		return err
	}

	_, err = namespace.SetWorkSpec(map[string]interface{}{
		"name":        "runner",
		"runtime":     "go",
		"task":        "runner",
		"max_getwork": 10,
	})
	if err != nil {
		return err
	}

	return nil
}
Example #2
0
func (api *restAPI) fillWorkSpecShort(namespace coordinate.Namespace, name string, short *restdata.WorkSpecShort) error {
	short.Name = name
	return buildURLs(api.Router,
		"namespace", namespace.Name(),
		"spec", name,
	).URL(&short.URL, "workSpec").Error
}
Example #3
0
// WorkSpec creates a work spec in the named namespace; if it fails,
// fail the test.
func (a *CacheAssertions) WorkSpec(ns coordinate.Namespace, name string) coordinate.WorkSpec {
	workSpec, err := ns.SetWorkSpec(map[string]interface{}{
		"name": name,
	})
	if !a.NoError(err, "error creating work spec") {
		a.FailNow("cannot create work spec")
	}
	return workSpec
}
Example #4
0
func (api *restAPI) fillWorkerShort(namespace coordinate.Namespace, worker coordinate.Worker, short *restdata.WorkerShort) error {
	short.Name = worker.Name()
	return buildURLs(api.Router,
		"namespace", namespace.Name(),
		"worker", short.Name,
	).
		URL(&short.URL, "worker").
		Error
}
Example #5
0
// createWorker creates a worker with a random name.  If there is a
// failure creating the worker, panics.
func createWorker(namespace coordinate.Namespace) coordinate.Worker {
	// Construct a random worker name:
	workerName := strings.Map(func(rune) rune {
		return rune('A' + rand.Intn(26))
	}, "12345678")
	worker, err := namespace.Worker(workerName)
	if err != nil {
		panic(err)
	}
	return worker
}
Example #6
0
func (api *restAPI) attemptURLBuilder(namespace coordinate.Namespace, attempt coordinate.Attempt, startTime time.Time, err error) *urlBuilder {
	unit := attempt.WorkUnit()
	spec := unit.WorkSpec()
	worker := attempt.Worker()
	if err == nil {
		return buildURLs(api.Router,
			"namespace", namespace.Name(),
			"spec", spec.Name(),
			"unit", unit.Name(),
			"worker", worker.Name(),
			"start", startTime.Format(time.RFC3339),
		)
	}
	return &urlBuilder{Error: err}
}
Example #7
0
// TestMetaContinuous specifically checks that you cannot enable the
// "continuous" flag on non-continuous work specs.
func TestMetaContinuous(t *testing.T) {
	var (
		err       error
		namespace coordinate.Namespace
		spec      coordinate.WorkSpec
		meta      coordinate.WorkSpecMeta
	)

	namespace, err = Coordinate.Namespace("TestMetaContinuous")
	if !assert.NoError(t, err) {
		return
	}
	defer namespace.Destroy()

	spec, err = namespace.SetWorkSpec(map[string]interface{}{
		"name":   "spec",
		"min_gb": 1,
	})
	if !assert.NoError(t, err) {
		return
	}

	meta, err = spec.Meta(false)
	if assert.NoError(t, err) {
		assert.False(t, meta.Continuous)
		assert.False(t, meta.CanBeContinuous)
	}

	meta.Continuous = true
	err = spec.SetMeta(meta)
	assert.NoError(t, err)

	meta, err = spec.Meta(false)
	if assert.NoError(t, err) {
		// Cannot set the "continuous" flag
		assert.False(t, meta.Continuous)
		assert.False(t, meta.CanBeContinuous)
	}
}
Example #8
0
func (api *restAPI) fillWorkUnit(namespace coordinate.Namespace, spec coordinate.WorkSpec, unit coordinate.WorkUnit, repr *restdata.WorkUnit) error {
	err := api.fillWorkUnitShort(namespace, spec, unit.Name(), &repr.WorkUnitShort)
	if err == nil {
		repr.Data, err = unit.Data()
	}
	if err == nil {
		var meta coordinate.WorkUnitMeta
		meta, err = unit.Meta()
		repr.Meta = &meta
	}
	if err == nil {
		repr.Status, err = unit.Status()
	}
	if err == nil {
		err = buildURLs(api.Router,
			"namespace", namespace.Name(),
			"spec", spec.Name(),
			"unit", unit.Name(),
		).
			URL(&repr.WorkSpecURL, "workSpec").
			URL(&repr.AttemptsURL, "workUnitAttempts").
			Error
	}
	if err == nil {
		var attempt coordinate.Attempt
		attempt, err = unit.ActiveAttempt()
		if err == nil && attempt != nil {
			// This is cheating, a little, but it's probably
			// the easiest way to reuse this code
			var short restdata.AttemptShort
			err = api.fillAttemptShort(namespace, attempt, &short)
			if err == nil {
				repr.ActiveAttemptURL = short.URL
			}
		}
	}
	return err
}
Example #9
0
func (api *restAPI) fillWorkSpec(namespace coordinate.Namespace, name string, repr *restdata.WorkSpec) error {
	err := api.fillWorkSpecShort(namespace, name, &repr.WorkSpecShort)
	if err == nil {
		err = buildURLs(api.Router,
			"namespace", namespace.Name(),
			"spec", name).
			URL(&repr.WorkUnitsURL, "workUnits").
			Template(&repr.WorkUnitURL, "workUnit", "unit").
			URL(&repr.MetaURL, "workSpecMeta").
			URL(&repr.WorkUnitCountsURL, "workSpecCounts").
			URL(&repr.WorkUnitChangeURL, "workSpecChange").
			URL(&repr.WorkUnitAdjustURL, "workSpecAdjust").
			Error
	}
	if err == nil {
		repr.MetaURL += "{?counts}"
		qs := "{?name*,status*,previous,limit}"
		repr.WorkUnitQueryURL = repr.WorkUnitsURL + qs
		repr.WorkUnitChangeURL += qs
		repr.WorkUnitAdjustURL += qs
	}
	return err
}
Example #10
0
func (api *restAPI) fillNamespaceShort(namespace coordinate.Namespace, summary *restdata.NamespaceShort) error {
	summary.Name = namespace.Name()
	return buildURLs(api.Router, "namespace", summary.Name).
		URL(&summary.URL, "namespace").
		Error
}
Example #11
0
// TestSetMeta tests the basic SetMeta() call and a couple of its
// documented oddities.
func TestSetMeta(t *testing.T) {
	var (
		err       error
		namespace coordinate.Namespace
		spec      coordinate.WorkSpec
		meta      coordinate.WorkSpecMeta
	)

	namespace, err = Coordinate.Namespace("TestSetMeta")
	if !assert.NoError(t, err) {
		return
	}
	defer namespace.Destroy()

	spec, err = namespace.SetWorkSpec(map[string]interface{}{
		"name":       "spec",
		"min_gb":     1,
		"continuous": true,
	})
	if !assert.NoError(t, err) {
		return
	}

	meta, err = spec.Meta(false)
	if assert.NoError(t, err) {
		assert.Equal(t, 0, meta.Priority)
		assert.Equal(t, 20, meta.Weight)
		assert.False(t, meta.Paused)
		assert.True(t, meta.Continuous)
		assert.True(t, meta.CanBeContinuous)
		assert.Zero(t, meta.Interval)
		assert.WithinDuration(t, time.Time{}, meta.NextContinuous, 1*time.Microsecond)
		assert.Equal(t, 0, meta.MaxRunning)
		assert.Equal(t, 0, meta.MaxAttemptsReturned)
		assert.Equal(t, "", meta.NextWorkSpecName)
		assert.Equal(t, 0, meta.AvailableCount)
		assert.Equal(t, 0, meta.PendingCount)
		assert.Equal(t, "", meta.Runtime)
	}

	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,
		Runtime:             "go",
	})
	assert.NoError(t, err)

	meta, err = spec.Meta(false)
	if assert.NoError(t, err) {
		assert.Equal(t, 10, meta.Priority)
		assert.Equal(t, 100, meta.Weight)
		assert.True(t, meta.Paused)
		assert.False(t, meta.Continuous)
		// Cannot clear "can be continuous" flag
		assert.True(t, meta.CanBeContinuous)
		assert.Equal(t, 60*time.Second, meta.Interval)
		assert.WithinDuration(t, time.Time{}, meta.NextContinuous, 1*time.Microsecond)
		assert.Equal(t, 10, meta.MaxRunning)
		assert.Equal(t, 1, meta.MaxAttemptsReturned)
		// Cannot change following work spec
		assert.Equal(t, "", meta.NextWorkSpecName)
		// Cannot set the counts
		assert.Equal(t, 0, meta.AvailableCount)
		assert.Equal(t, 0, meta.PendingCount)
		// Cannot change the language runtime
		assert.Equal(t, "", meta.Runtime)
	}
}
Example #12
0
func (api *restAPI) fillWorker(namespace coordinate.Namespace, worker coordinate.Worker, result *restdata.Worker) error {
	err := api.fillWorkerShort(namespace, worker, &result.WorkerShort)
	if err == nil {
		err = buildURLs(api.Router,
			"namespace", namespace.Name(),
			"worker", worker.Name(),
		).
			URL(&result.RequestAttemptsURL, "workerRequestAttempts").
			URL(&result.MakeAttemptURL, "workerMakeAttempt").
			URL(&result.ActiveAttemptsURL, "workerActiveAttempts").
			URL(&result.AllAttemptsURL, "workerAllAttempts").
			URL(&result.ChildAttemptsURL, "workerChildAttempts").
			Error
	}
	var parent coordinate.Worker
	if err == nil {
		parent, err = worker.Parent()
	}
	if err == nil && parent != nil {
		parentName := parent.Name()
		result.Parent = &parentName
		err = buildURLs(api.Router,
			"namespace", namespace.Name(),
			"worker", parent.Name(),
		).
			URL(&result.ParentURL, "worker").
			Error
	}
	var children []coordinate.Worker
	if err == nil {
		children, err = worker.Children()
	}
	if err == nil {
		result.ChildURLs = make([]string, len(children))
		for i, child := range children {
			err = buildURLs(api.Router,
				"namespace", namespace.Name(),
				"worker", child.Name(),
			).
				URL(&result.ChildURLs[i], "worker").
				Error
			if err != nil {
				break
			}
		}
	}
	if err == nil {
		result.Active, err = worker.Active()
	}
	if err == nil {
		result.Mode, err = worker.Mode()
	}
	if err == nil {
		result.Data, err = worker.Data()
	}
	if err == nil {
		result.Expiration, err = worker.Expiration()
	}
	if err == nil {
		result.LastUpdate, err = worker.LastUpdate()
	}
	return err
}