// Watch starts an NotifyWatcher for each given machine. func (m *MachinerAPI) Watch(args params.Entities) (params.NotifyWatchResults, error) { result := params.NotifyWatchResults{ Results: make([]params.NotifyWatchResult, len(args.Entities)), } if len(args.Entities) == 0 { return result, nil } for i, entity := range args.Entities { err := common.ErrPerm if m.auth.AuthOwner(entity.Tag) { var machine *state.Machine machine, err = m.st.Machine(state.MachineIdFromTag(entity.Tag)) if err == nil { watch := machine.Watch() // Consume the initial event. Technically, API // calls to Watch 'transmit' the initial event // in the Watch response. But NotifyWatchers // have no state to transmit. if _, ok := <-watch.Changes(); ok { result.Results[i].NotifyWatcherId = m.resources.Register(watch) } else { err = watcher.MustErr(watch) } } } result.Results[i].Error = common.ServerError(err) } return result, nil }
func newMachineToolWaiter(m *state.Machine) *toolsWaiter { w := m.Watch() waiter := &toolsWaiter{ changes: make(chan struct{}, 1), watcher: w, tooler: m, } go func() { for _ = range w.Changes() { waiter.changes <- struct{}{} } close(waiter.changes) }() return waiter }
func (s *ProvisionerSuite) waitMachine(c *C, m *state.Machine, check func() bool) { w := m.Watch() defer stop(c, w) timeout := time.After(500 * time.Millisecond) resync := time.After(0) for { select { case <-w.Changes(): if check() { return } case <-resync: resync = time.After(50 * time.Millisecond) s.State.StartSync() case <-timeout: c.Fatalf("machine %v wait timed out", m) } } }
func (s *CommonProvisionerSuite) waitMachine(c *C, m *state.Machine, check func() bool) { // TODO(jam): We need to grow a new method on NotifyWatcherC // that calls StartSync while waiting for changes, then // waitMachine and waitHardwareCharacteristics can use that // instead w := m.Watch() defer stop(c, w) timeout := time.After(coretesting.LongWait) resync := time.After(0) for { select { case <-w.Changes(): if check() { return } case <-resync: resync = time.After(coretesting.ShortWait) s.State.StartSync() case <-timeout: c.Fatalf("machine %v wait timed out", m) } } }