Example #1
0
func (ns *namespace) SetWorkSpec(data map[string]interface{}) (coordinate.WorkSpec, error) {
	name, meta, err := coordinate.ExtractWorkSpecMeta(data)
	if err != nil {
		return nil, err
	}

	spec := workSpec{
		namespace: ns,
		name:      name,
	}
	err = withTx(ns, false, func(tx *sql.Tx) error {
		params := queryParams{}
		query := buildSelect([]string{
			workSpecID,
		}, []string{
			workSpecTable,
		}, []string{
			workSpecInNamespace(&params, ns.id),
			workSpecHasName(&params, name),
		})
		row := tx.QueryRow(query, params...)
		err = row.Scan(&spec.id)
		if err == nil {
			err = spec.setData(tx, data, meta)
		} else if err == sql.ErrNoRows {
			var dataBytes []byte
			dataBytes, err = mapToBytes(data)
			if err != nil {
				return err
			}
			params = queryParams{}
			fields := fieldList{}
			fields.Add(&params, "namespace_id", ns.id)
			fields.Add(&params, "name", name)
			fields.Add(&params, "data", dataBytes)
			fields.Add(&params, "priority", meta.Priority)
			fields.Add(&params, "weight", meta.Weight)
			fields.Add(&params, "paused", meta.Paused)
			fields.Add(&params, "continuous", meta.Continuous)
			fields.Add(&params, "can_be_continuous", meta.CanBeContinuous)
			fields.Add(&params, "min_memory_gb", meta.MinMemoryGb)
			fields.Add(&params, "interval", durationToSQL(meta.Interval))
			fields.Add(&params, "next_continuous", timeToNullTime(meta.NextContinuous))
			fields.Add(&params, "max_running", meta.MaxRunning)
			fields.Add(&params, "max_attempts_returned", meta.MaxAttemptsReturned)
			fields.Add(&params, "next_work_spec_name", meta.NextWorkSpecName)
			fields.AddDirect("next_work_spec_preempts", "FALSE")
			fields.Add(&params, "runtime", meta.Runtime)
			query = fields.InsertStatement(workSpecTable) + "RETURNING id"
			row = tx.QueryRow(query, params...)
			err = row.Scan(&spec.id)
		}
		return err
	})
	if err != nil {
		return nil, err
	}
	return &spec, nil
}
Example #2
0
func (spec *workSpec) SetData(data map[string]interface{}) error {
	name, meta, err := coordinate.ExtractWorkSpecMeta(data)
	if err != nil {
		return err
	}
	if name != spec.name {
		return coordinate.ErrChangedName
	}
	return withTx(spec, false, func(tx *sql.Tx) error {
		return spec.setData(tx, data, meta)
	})
}
Example #3
0
// setData is an internal version of SetData() with the same constraints,
// guarantees, and checking.  It assumes the global lock.
func (spec *workSpec) setData(data map[string]interface{}) error {
	name, meta, err := coordinate.ExtractWorkSpecMeta(data)
	if err == nil {
		if name != spec.name {
			err = coordinate.ErrChangedName
		}
	}
	if err == nil {
		spec.data = data
		spec.meta = meta
	}
	return err
}