本文整理汇总了Golang中github.com/dmaze/goordinate/coordinate.WorkSpec.WorkUnit方法的典型用法代码示例。如果您正苦于以下问题:Golang WorkSpec.WorkUnit方法的具体用法?Golang WorkSpec.WorkUnit怎么用?Golang WorkSpec.WorkUnit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/dmaze/goordinate/coordinate.WorkSpec
的用法示例。
在下文中一共展示了WorkSpec.WorkUnit方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestTrivialWorkUnitFlow
// TestTrivialWorkUnitFlow tests work unit creation, deletion, and existence.
func (s *Suite) TestTrivialWorkUnitFlow(c *check.C) {
var (
count int
err error
spec coordinate.WorkSpec
unit coordinate.WorkUnit
units map[string]coordinate.WorkUnit
)
spec, err = s.Namespace.SetWorkSpec(map[string]interface{}{
"name": "spec",
"min_gb": 1,
})
c.Assert(err, check.IsNil)
unit, err = spec.AddWorkUnit("unit", map[string]interface{}{}, 0)
c.Assert(err, check.IsNil)
c.Check(unit.Name(), check.Equals, "unit")
c.Check(unit.WorkSpec().Name(), check.Equals, "spec")
unit, err = spec.WorkUnit("unit")
c.Assert(err, check.IsNil)
c.Check(unit.Name(), check.Equals, "unit")
c.Check(unit.WorkSpec().Name(), check.Equals, "spec")
units, err = spec.WorkUnits(coordinate.WorkUnitQuery{})
c.Assert(err, check.IsNil)
c.Check(units, check.HasLen, 1)
c.Check(units["unit"], check.NotNil)
c.Check(units["unit"].Name(), check.Equals, "unit")
c.Check(units["unit"].WorkSpec().Name(), check.Equals, "spec")
count, err = spec.DeleteWorkUnits(coordinate.WorkUnitQuery{})
c.Assert(err, check.IsNil)
c.Check(count, check.Equals, 1)
unit, err = spec.WorkUnit("unit")
c.Assert(err, check.IsNil)
c.Check(unit, check.IsNil)
units, err = spec.WorkUnits(coordinate.WorkUnitQuery{})
c.Assert(err, check.IsNil)
c.Check(units, check.HasLen, 0)
}
示例2: UpdateWorkUnit
// UpdateWorkUnit causes some state change in a work unit. If the
// work unit is pending, this is the principal interface to complete
// or renew it; if it is already complete this can cause it to be
// retried.
func (jobs *JobServer) UpdateWorkUnit(
workSpecName string,
workUnitKey string,
options map[string]interface{},
) (bool, string, error) {
// Note that in several corner cases, the behavior of this as
// written disagrees with Python coordinated's:
//
// * If neither "lease_time" nor "status" is specified,
// Python coordinated immediately returns False without
// checking if workUnitKey is valid
//
// * Python coordinated allows arbitrary status changes,
// including AVAILABLE -> FINISHED
//
// * This openly ignores "worker_id", as distinct from Python
// coordinated, which logs an obscure warning and changes it,
// but only on a renew
var (
attempt coordinate.Attempt
changed bool
err error
status coordinate.AttemptStatus
uwuOptions UpdateWorkUnitOptions
workSpec coordinate.WorkSpec
workUnit coordinate.WorkUnit
)
err = decode(&uwuOptions, options)
if err == nil {
workSpec, err = jobs.Namespace.WorkSpec(workSpecName)
}
if err == nil {
workUnit, err = workSpec.WorkUnit(workUnitKey)
}
if err == nil {
if workUnit == nil {
return false, fmt.Sprintf("no such work unit key=%v", workUnitKey), nil
}
}
if err == nil {
attempt, err = workUnit.ActiveAttempt()
}
if err == nil && attempt != nil {
status, err = attempt.Status()
}
if err == nil && attempt != nil {
if status == coordinate.Expired || status == coordinate.Retryable {
// The Python Coordinate API sees both of these
// statuses as "available", and we want to fall
// into the next block.
attempt = nil
}
}
if err == nil && attempt == nil {
// Caller is trying to manipulate an AVAILABLE work
// unit. Cowardly refuse to start a new attempt on
// their behalf, or to update the persistent work unit
// data this way. (In theory there's no reason we
// *couldn't* do either, though I'm not aware of any
// callers that do; add_work_unit will replace
// existing work units and is the more typical way to
// refresh data.)
err = errors.New("update_work_unit will not adjust an available work unit")
}
if err == nil {
switch status {
case coordinate.Pending:
changed = true // or there's an error
switch uwuOptions.Status {
case 0, Pending:
err = uwuRenew(attempt, uwuOptions)
case Available:
err = attempt.Expire(uwuOptions.Data)
case Finished:
err = attempt.Finish(uwuOptions.Data)
case Failed:
err = attempt.Fail(uwuOptions.Data)
default:
err = errors.New("update_work_unit invalid status")
}
case coordinate.Expired:
err = errors.New("update_work_unit logic error, trying to refresh expired unit")
case coordinate.Finished:
switch uwuOptions.Status {
case 0, Finished:
changed = false // no-op
case Available:
err = workUnit.ClearActiveAttempt()
changed = true
case Failed:
changed = false // see below
default:
err = errors.New("update_work_unit cannot change finished unit")
}
case coordinate.Failed:
switch uwuOptions.Status {
//.........这里部分代码省略.........