• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang coordinate.WorkSpecMeta类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/dmaze/goordinate/coordinate.WorkSpecMeta的典型用法代码示例。如果您正苦于以下问题:Golang WorkSpecMeta类的具体用法?Golang WorkSpecMeta怎么用?Golang WorkSpecMeta使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了WorkSpecMeta类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: TestMetaContinuous

// 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)
}
开发者ID:dmaze,项目名称:goordinate,代码行数:31,代码来源:work_spec.go


示例2: getWorkFromSpec

// getWorkFromSpec forcibly retrieves a work unit from a work spec.
// It could create a work unit if spec is a continuous spec with no
// available units.  It ignores other constraints, such as whether the
// work spec is paused.
func (w *worker) getWorkFromSpec(spec *workSpec, meta *coordinate.WorkSpecMeta) *attempt {
	var unit *workUnit
	now := w.Coordinate().clock.Now()
	if len(spec.available) != 0 {
		unit = spec.available.Next()
	} else if meta.CanStartContinuous(now) {
		// Make a brand new work unit.  Its key is the string
		// form of a time_t.
		seconds := now.Unix()
		nano := now.Nanosecond()
		milli := nano / 1000000
		name := fmt.Sprintf("%d.%03d", seconds, milli)
		var exists bool
		unit, exists = spec.workUnits[name]
		if !exists {
			unit = &workUnit{
				name:     name,
				data:     map[string]interface{}{},
				workSpec: spec,
			}
			spec.workUnits[name] = unit
		}
		spec.meta.NextContinuous = now.Add(meta.Interval)
	} else {
		return nil
	}
	return w.makeAttempt(unit, time.Duration(0))
}
开发者ID:dmaze,项目名称:goordinate,代码行数:32,代码来源:worker.go


示例3: SetMeta

func (spec *workSpec) SetMeta(meta coordinate.WorkSpecMeta) error {
	globalLock(spec)
	defer globalUnlock(spec)

	// Preserve immutable fields (taking advantage of meta pass-by-value)
	meta.CanBeContinuous = spec.meta.CanBeContinuous
	meta.NextWorkSpecName = spec.meta.NextWorkSpecName

	// If this cannot be continuous, force-clear that flag
	if !meta.CanBeContinuous {
		meta.Continuous = false
	}

	spec.meta = meta
	return nil
}
开发者ID:dmaze,项目名称:goordinate,代码行数:16,代码来源:work_spec.go


示例4: requestAttemptsForSpec

func (w *worker) requestAttemptsForSpec(req coordinate.AttemptRequest, spec *workSpec, meta *coordinate.WorkSpecMeta) ([]coordinate.Attempt, error) {
	var (
		attempts []coordinate.Attempt
		count    int
		err      error
	)

	// Adjust the work unit count based on what's possible here
	count = req.NumberOfWorkUnits
	if count < 1 {
		count = 1
	}
	if meta.MaxAttemptsReturned > 0 && count > meta.MaxAttemptsReturned {
		count = meta.MaxAttemptsReturned
	}
	if meta.MaxRunning > 0 && count > meta.MaxRunning-meta.PendingCount {
		count = meta.MaxRunning - meta.PendingCount
	}

	// Now choose units and create attempts
	err = withTx(w, func(tx *sql.Tx) error {
		units, err := w.chooseWorkUnits(tx, spec, count)
		if err != nil {
			return err
		}
		now := w.Coordinate().clock.Now()
		if len(units) == 0 && meta.CanStartContinuous(now) {
			units, err = w.createContinuousUnits(tx, spec, meta)
		}
		if err != nil {
			return err
		}
		length := time.Duration(15) * time.Minute
		for _, unit := range units {
			a, err := makeAttempt(tx, unit, w, length)
			if err != nil {
				return err
			}
			attempts = append(attempts, a)
		}
		return nil
	})
	return attempts, err
}
开发者ID:dmaze,项目名称:goordinate,代码行数:44,代码来源:attempt.go


示例5: ControlWorkSpec

// 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
}
开发者ID:dmaze,项目名称:goordinate,代码行数:54,代码来源:specs.go


示例6: allMetas

// AllMetas retrieves the metadata for all work specs.  This is
// expected to run within a pre-existing transaction.  On success,
// returns maps from work spec name to work spec object and to
// metadata object.
func (ns *namespace) allMetas(tx *sql.Tx, withCounts bool) (map[string]*workSpec, map[string]*coordinate.WorkSpecMeta, error) {
	query := buildSelect([]string{
		workSpecID,
		workSpecName,
		workSpecPriority,
		workSpecWeight,
		workSpecPaused,
		workSpecContinuous,
		workSpecCanBeContinuous,
		workSpecMinMemoryGb,
		workSpecInterval,
		workSpecNextContinuous,
		workSpecMaxRunning,
		workSpecMaxAttemptsReturned,
		workSpecNextWorkSpec,
	}, []string{
		workSpecTable,
	}, []string{
		inThisNamespace,
	})
	rows, err := tx.Query(query, ns.id)
	if err != nil {
		return nil, nil, err
	}
	specs := make(map[string]*workSpec)
	metas := make(map[string]*coordinate.WorkSpecMeta)
	err = scanRows(rows, func() error {
		var (
			spec           workSpec
			meta           coordinate.WorkSpecMeta
			interval       string
			nextContinuous pq.NullTime
			err            error
		)
		err = rows.Scan(&spec.id, &spec.name, &meta.Priority,
			&meta.Weight, &meta.Paused, &meta.Continuous,
			&meta.CanBeContinuous, &meta.MinMemoryGb,
			&interval, &nextContinuous, &meta.MaxRunning,
			&meta.MaxAttemptsReturned,
			&meta.NextWorkSpecName)
		if err != nil {
			return err
		}
		spec.namespace = ns
		meta.NextContinuous = nullTimeToTime(nextContinuous)
		meta.Interval, err = sqlToDuration(interval)
		if err != nil {
			return err
		}
		specs[spec.name] = &spec
		metas[spec.name] = &meta
		return nil
	})
	if err != nil {
		return nil, nil, err
	}
	if withCounts {
		// A single query that selects both "available" and
		// "pending" is hopelessly expensive.  Also, in the
		// only place this is called (in RequestAttempts) we
		// need to know whether or not there are any available
		// attempts, but we don't really care how many there
		// are so long as there are more than zero.
		//
		// Pending:
		query = buildSelect([]string{workSpecName, "COUNT(*)"},
			[]string{workSpecTable, workUnitTable, attemptTable},
			[]string{
				inThisNamespace, // binds $1
				workUnitInSpec,
				attemptThisWorkUnit,
				attemptIsPending,
			})
		query += " GROUP BY " + workSpecName
		rows, err = tx.Query(query, ns.id)
		if err != nil {
			return nil, nil, err
		}
		err = scanRows(rows, func() error {
			var name string
			var count int
			err := rows.Scan(&name, &count)
			if err == nil {
				metas[name].PendingCount = count
			}
			return err
		})

		// Available count (0/1):
		query = buildSelect([]string{"1"},
			[]string{workUnitTable},
			[]string{workUnitInSpec, hasNoAttempt})
		query = buildSelect(
			[]string{
				workSpecName,
				"EXISTS(" + query + ")",
//.........这里部分代码省略.........
开发者ID:dmaze,项目名称:goordinate,代码行数:101,代码来源:work_spec.go


示例7: Meta

func (spec *workSpec) Meta(withCounts bool) (coordinate.WorkSpecMeta, error) {
	// If we need counts, we need to run expiry so that the
	// available/pending counts are rightish
	if withCounts {
		_ = withTx(spec, func(tx *sql.Tx) error {
			return expireAttempts(spec, tx)
		})
	}
	var meta coordinate.WorkSpecMeta
	err := withTx(spec, func(tx *sql.Tx) error {
		var (
			query          string
			interval       string
			nextContinuous pq.NullTime
		)
		query = buildSelect([]string{
			workSpecPriority,
			workSpecWeight,
			workSpecPaused,
			workSpecContinuous,
			workSpecCanBeContinuous,
			workSpecMinMemoryGb,
			workSpecInterval,
			workSpecNextContinuous,
			workSpecMaxRunning,
			workSpecMaxAttemptsReturned,
			workSpecNextWorkSpec,
		}, []string{
			workSpecTable,
		}, []string{
			isWorkSpec, // binds $1
		})
		row := tx.QueryRow(query, spec.id)
		err := row.Scan(
			&meta.Priority,
			&meta.Weight,
			&meta.Paused,
			&meta.Continuous,
			&meta.CanBeContinuous,
			&meta.MinMemoryGb,
			&interval,
			&nextContinuous,
			&meta.MaxRunning,
			&meta.MaxAttemptsReturned,
			&meta.NextWorkSpecName,
		)
		if err != nil {
			return err
		}
		meta.NextContinuous = nullTimeToTime(nextContinuous)
		meta.Interval, err = sqlToDuration(interval)
		if err != nil {
			return err
		}

		// Find counts with a second query, if requested
		if !withCounts {
			return nil
		}
		query = buildSelect([]string{
			attemptStatus,
			"COUNT(*)",
		}, []string{
			workUnitAttemptJoin,
		}, []string{
			inThisWorkSpec, // binds $1
		})
		query += " GROUP BY " + attemptStatus
		rows, err := tx.Query(query, spec.id)
		if err != nil {
			return err
		}
		return scanRows(rows, func() error {
			var status sql.NullString
			var count int
			err := rows.Scan(&status, &count)
			if err != nil {
				return err
			}
			if !status.Valid {
				meta.AvailableCount += count
			} else {
				switch status.String {
				case "expired":
					meta.AvailableCount += count
				case "retryable":
					meta.AvailableCount += count
				case "pending":
					meta.PendingCount += count
				}
			}
			return nil
		})
	})
	return meta, err
}
开发者ID:dmaze,项目名称:goordinate,代码行数:96,代码来源:work_spec.go



注:本文中的github.com/dmaze/goordinate/coordinate.WorkSpecMeta类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang coordinate.WorkUnit类代码示例发布时间:2022-05-23
下一篇:
Golang coordinate.WorkSpec类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap