本文整理汇总了Golang中launchpad/net/juju-core/worker.NewRunner函数的典型用法代码示例。如果您正苦于以下问题:Golang NewRunner函数的具体用法?Golang NewRunner怎么用?Golang NewRunner使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewRunner函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: APIWorker
// APIWorker returns a Worker that connects to the API and starts any
// workers that need an API connection.
//
// If a state worker is necessary, APIWorker calls ensureStateWorker.
func (a *MachineAgent) APIWorker(ensureStateWorker func()) (worker.Worker, error) {
st, entity, err := openAPIState(a.Conf.Conf, a)
if err != nil {
// There was an error connecting to the API,
// https://launchpad.net/bugs/1199915 means that we may just
// not have an API password set. So force a state connection at
// this point.
// TODO(jam): Once we can reliably trust that we have API
// passwords set, and we no longer need state
// connections (and possibly agents will be blocked
// from connecting directly to state) we can remove
// this. Currently needed because 1.10 does not set
// the API password and 1.11 requires it
ensureStateWorker()
return nil, err
}
m := entity.(*machineagent.Machine)
needsStateWorker := false
for _, job := range m.Jobs() {
needsStateWorker = needsStateWorker || stateJobs[job]
}
if needsStateWorker {
ensureStateWorker()
}
runner := worker.NewRunner(allFatal, moreImportant)
// Only the machiner currently connects to the API.
// Add other workers here as they are ready.
runner.StartWorker("machiner", func() (worker.Worker, error) {
return machiner.NewMachiner(st.Machiner(), a.Tag()), nil
})
return newCloseWorker(runner, st), nil // Note: a worker.Runner is itself a worker.Worker.
}
示例2: TestOneWorkerStartWhenStopping
func (*runnerSuite) TestOneWorkerStartWhenStopping(c *C) {
worker.RestartDelay = 3 * time.Second
runner := worker.NewRunner(allFatal, noImportance)
starter := newTestWorkerStarter()
starter.stopWait = make(chan struct{})
err := runner.StartWorker("id", testWorkerStart(starter))
c.Assert(err, IsNil)
starter.assertStarted(c, true)
err = runner.StopWorker("id")
c.Assert(err, IsNil)
err = runner.StartWorker("id", testWorkerStart(starter))
c.Assert(err, IsNil)
close(starter.stopWait)
starter.assertStarted(c, false)
// Check that the task is restarted immediately without
// the usual restart timeout delay.
t0 := time.Now()
starter.assertStarted(c, true)
restartDuration := time.Since(t0)
if restartDuration > 1*time.Second {
c.Fatalf("task did not restart immediately")
}
c.Assert(worker.Stop(runner), IsNil)
}
示例3: TestOneWorkerStartFatalError
func (*runnerSuite) TestOneWorkerStartFatalError(c *C) {
runner := worker.NewRunner(allFatal, noImportance)
starter := newTestWorkerStarter()
starter.startErr = errors.New("cannot start test task")
err := runner.StartWorker("id", testWorkerStart(starter))
c.Assert(err, IsNil)
err = runner.Wait()
c.Assert(err, Equals, starter.startErr)
}
示例4: TestOneWorkerStart
func (*runnerSuite) TestOneWorkerStart(c *C) {
runner := worker.NewRunner(noneFatal, noImportance)
starter := newTestWorkerStarter()
err := runner.StartWorker("id", testWorkerStart(starter))
c.Assert(err, IsNil)
starter.assertStarted(c, true)
c.Assert(worker.Stop(runner), IsNil)
starter.assertStarted(c, false)
}
示例5: Init
// Init initializes the command for running.
func (a *MachineAgent) Init(args []string) error {
if !state.IsMachineId(a.MachineId) {
return fmt.Errorf("--machine-id option must be set, and expects a non-negative integer")
}
if err := a.Conf.checkArgs(args); err != nil {
return err
}
a.runner = worker.NewRunner(isFatal, moreImportant)
return nil
}
示例6: APIWorker
// APIWorker returns a Worker that connects to the API and starts any
// workers that need an API connection.
//
// If a state worker is necessary, APIWorker calls ensureStateWorker.
func (a *MachineAgent) APIWorker(ensureStateWorker func()) (worker.Worker, error) {
st, entity, err := openAPIState(a.Conf.Conf, a)
if err != nil {
// There was an error connecting to the API,
// https://launchpad.net/bugs/1199915 means that we may just
// not have an API password set. So force a state connection at
// this point.
// TODO(jam): Once we can reliably trust that we have API
// passwords set, and we no longer need state
// connections (and possibly agents will be blocked
// from connecting directly to state) we can remove
// this. Currently needed because 1.10 does not set
// the API password and 1.11 requires it
ensureStateWorker()
return nil, err
}
needsStateWorker := false
for _, job := range entity.Jobs() {
needsStateWorker = needsStateWorker || stateJobs[job]
}
if needsStateWorker {
ensureStateWorker()
}
runner := worker.NewRunner(allFatal, moreImportant)
// Only the machiner currently connects to the API.
// Add other workers here as they are ready.
runner.StartWorker("machiner", func() (worker.Worker, error) {
return machiner.NewMachiner(st.Machiner(), a.Tag()), nil
})
runner.StartWorker("upgrader", func() (worker.Worker, error) {
// TODO(rog) use id instead of *Machine (or introduce Clone method)
return upgrader.New(st.Upgrader(), a.Tag(), a.Conf.DataDir), nil
})
for _, job := range entity.Jobs() {
switch job {
case params.JobHostUnits:
deployerTask, err := newDeployer(st.Deployer(), a.Tag(), a.Conf.DataDir)
if err != nil {
return nil, err
}
runner.StartWorker("deployer", func() (worker.Worker, error) {
return deployerTask, nil
})
case params.JobManageEnviron:
// Not yet implemented with the API.
case params.JobManageState:
// Not yet implemented with the API.
default:
// TODO(dimitern): Once all workers moved over to using
// the API, report "unknown job type" here.
}
}
return newCloseWorker(runner, st), nil // Note: a worker.Runner is itself a worker.Worker.
}
示例7: TestOneWorkerDieFatalError
func (*runnerSuite) TestOneWorkerDieFatalError(c *C) {
runner := worker.NewRunner(allFatal, noImportance)
starter := newTestWorkerStarter()
err := runner.StartWorker("id", testWorkerStart(starter))
c.Assert(err, IsNil)
starter.assertStarted(c, true)
dieErr := errors.New("error when running")
starter.die <- dieErr
err = runner.Wait()
c.Assert(err, Equals, dieErr)
starter.assertStarted(c, false)
}
示例8: TestOneWorkerStopFatalError
func (*runnerSuite) TestOneWorkerStopFatalError(c *C) {
runner := worker.NewRunner(allFatal, noImportance)
starter := newTestWorkerStarter()
starter.stopErr = errors.New("stop error")
err := runner.StartWorker("id", testWorkerStart(starter))
c.Assert(err, IsNil)
starter.assertStarted(c, true)
err = runner.StopWorker("id")
c.Assert(err, IsNil)
err = runner.Wait()
c.Assert(err, Equals, starter.stopErr)
}
示例9: APIWorkers
func (a *UnitAgent) APIWorkers() (worker.Worker, error) {
st, entity, err := openAPIState(a.Conf.Conf, a)
if err != nil {
return nil, err
}
dataDir := a.Conf.DataDir
runner := worker.NewRunner(allFatal, moreImportant)
runner.StartWorker("upgrader", func() (worker.Worker, error) {
return upgrader.New(st.Upgrader(), entity.Tag(), dataDir), nil
})
return newCloseWorker(runner, st), nil
}
示例10: Init
// Init initializes the command for running.
func (a *UnitAgent) Init(args []string) error {
if a.UnitName == "" {
return requiredError("unit-name")
}
if !names.IsUnit(a.UnitName) {
return fmt.Errorf(`--unit-name option expects "<service>/<n>" argument`)
}
if err := a.Conf.checkArgs(args); err != nil {
return err
}
a.runner = worker.NewRunner(isFatal, moreImportant)
return nil
}
示例11: StateWorkers
// StateWorkers returns a worker that runs the unit agent workers.
func (a *UnitAgent) StateWorkers() (worker.Worker, error) {
st, entity, err := openState(a.Conf.Conf, a)
if err != nil {
return nil, err
}
unit := entity.(*state.Unit)
dataDir := a.Conf.DataDir
runner := worker.NewRunner(allFatal, moreImportant)
runner.StartWorker("uniter", func() (worker.Worker, error) {
return uniter.NewUniter(st, unit.Name(), dataDir), nil
})
return newCloseWorker(runner, st), nil
}
示例12: TestOneWorkerRestartDelay
func (*runnerSuite) TestOneWorkerRestartDelay(c *C) {
worker.RestartDelay = 100 * time.Millisecond
runner := worker.NewRunner(noneFatal, noImportance)
starter := newTestWorkerStarter()
err := runner.StartWorker("id", testWorkerStart(starter))
c.Assert(err, IsNil)
starter.assertStarted(c, true)
starter.die <- fmt.Errorf("non-fatal error")
starter.assertStarted(c, false)
t0 := time.Now()
starter.assertStarted(c, true)
restartDuration := time.Since(t0)
if restartDuration < worker.RestartDelay {
c.Fatalf("restart delay was not respected; got %v want %v", restartDuration, worker.RestartDelay)
}
}
示例13: TestErrorImportance
func (*runnerSuite) TestErrorImportance(c *C) {
moreImportant := func(err0, err1 error) bool {
return err0.(errorLevel) > err1.(errorLevel)
}
id := func(i int) string { return fmt.Sprint(i) }
runner := worker.NewRunner(allFatal, moreImportant)
for i := 0; i < 10; i++ {
starter := newTestWorkerStarter()
starter.stopErr = errorLevel(i)
err := runner.StartWorker(id(i), testWorkerStart(starter))
c.Assert(err, IsNil)
}
err := runner.StopWorker(id(4))
c.Assert(err, IsNil)
err = runner.Wait()
c.Assert(err, Equals, errorLevel(9))
}
示例14: TestOneWorkerRestart
func (*runnerSuite) TestOneWorkerRestart(c *C) {
runner := worker.NewRunner(noneFatal, noImportance)
starter := newTestWorkerStarter()
err := runner.StartWorker("id", testWorkerStart(starter))
c.Assert(err, IsNil)
starter.assertStarted(c, true)
// Check it restarts a few times time.
for i := 0; i < 3; i++ {
starter.die <- fmt.Errorf("an error")
starter.assertStarted(c, false)
starter.assertStarted(c, true)
}
c.Assert(worker.Stop(runner), IsNil)
starter.assertStarted(c, false)
}
示例15: TestAllWorkersStoppedWhenOneDiesWithFatalError
func (*runnerSuite) TestAllWorkersStoppedWhenOneDiesWithFatalError(c *C) {
runner := worker.NewRunner(allFatal, noImportance)
var starters []*testWorkerStarter
for i := 0; i < 10; i++ {
starter := newTestWorkerStarter()
err := runner.StartWorker(fmt.Sprint(i), testWorkerStart(starter))
c.Assert(err, IsNil)
starters = append(starters, starter)
}
for _, starter := range starters {
starter.assertStarted(c, true)
}
dieErr := errors.New("fatal error")
starters[4].die <- dieErr
err := runner.Wait()
c.Assert(err, Equals, dieErr)
for _, starter := range starters {
starter.assertStarted(c, false)
}
}