当前位置: 首页>>代码示例>>Golang>>正文


Golang RemoteStateWatcher.Stop方法代码示例

本文整理汇总了Golang中github.com/juju/juju/worker/uniter/remotestate.RemoteStateWatcher.Stop方法的典型用法代码示例。如果您正苦于以下问题:Golang RemoteStateWatcher.Stop方法的具体用法?Golang RemoteStateWatcher.Stop怎么用?Golang RemoteStateWatcher.Stop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/juju/juju/worker/uniter/remotestate.RemoteStateWatcher的用法示例。


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

示例1: loop

func (u *Uniter) loop(unitTag names.UnitTag) (err error) {
	if err := u.init(unitTag); err != nil {
		if err == worker.ErrTerminateAgent {
			return err
		}
		return fmt.Errorf("failed to initialize uniter for %q: %v", unitTag, err)
	}
	logger.Infof("unit %q started", u.unit)

	// Install is a special case, as it must run before there
	// is any remote state, and before the remote state watcher
	// is started.
	var charmURL *corecharm.URL
	opState := u.operationExecutor.State()
	if opState.Kind == operation.Install {
		logger.Infof("resuming charm install")
		op, err := u.operationFactory.NewInstall(opState.CharmURL)
		if err != nil {
			return errors.Trace(err)
		}
		if err := u.operationExecutor.Run(op); err != nil {
			return errors.Trace(err)
		}
		charmURL = opState.CharmURL
	} else {
		curl, err := u.unit.CharmURL()
		if err != nil {
			return errors.Trace(err)
		}
		charmURL = curl
	}

	var (
		watcher   *remotestate.RemoteStateWatcher
		watcherMu sync.Mutex
	)

	restartWatcher := func() error {
		watcherMu.Lock()
		defer watcherMu.Unlock()

		if watcher != nil {
			if err := watcher.Stop(); err != nil {
				return errors.Trace(err)
			}
		}
		var err error
		watcher, err = remotestate.NewWatcher(
			remotestate.WatcherConfig{
				State:               remotestate.NewAPIState(u.st),
				LeadershipTracker:   u.leadershipTracker,
				UnitTag:             unitTag,
				UpdateStatusChannel: u.updateStatusAt,
			})
		if err != nil {
			return errors.Trace(err)
		}
		// Stop the uniter if the watcher fails. The watcher may be
		// stopped cleanly, so only kill the tomb if the error is
		// non-nil.
		go func(w *remotestate.RemoteStateWatcher) {
			if err := w.Wait(); err != nil {
				u.tomb.Kill(err)
			}
		}(watcher)
		return nil
	}

	// watcher may be replaced, so use a closure.
	u.addCleanup(func() error {
		watcherMu.Lock()
		defer watcherMu.Unlock()

		if watcher != nil {
			return watcher.Stop()
		}
		return nil
	})

	onIdle := func() error {
		opState := u.operationExecutor.State()
		if opState.Kind != operation.Continue {
			// We should only set idle status if we're in
			// the "Continue" state, which indicates that
			// there is nothing to do and we're not in an
			// error state.
			return nil
		}
		return setAgentStatus(u, params.StatusIdle, "", nil)
	}

	clearResolved := func() error {
		if err := u.unit.ClearResolved(); err != nil {
			return errors.Trace(err)
		}
		watcher.ClearResolvedMode()
		return nil
	}

	for {
//.........这里部分代码省略.........
开发者ID:kakamessi99,项目名称:juju,代码行数:101,代码来源:uniter.go


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