Index: mstate/machine.go |
=== modified file 'mstate/machine.go' |
--- mstate/machine.go 2012-09-10 15:29:01 +0000 |
+++ mstate/machine.go 2012-09-11 13:23:57 +0000 |
@@ -80,35 +80,29 @@ |
} |
// AgentAlive returns whether the respective remote agent is alive. |
-func (m *Machine) AgentAlive() bool { |
- return m.st.presencew.Alive(m.globalKey()) |
+func (m *Machine) AgentAlive() (bool, error) { |
+ return m.st.pwatcher.Alive(m.globalKey()) |
} |
// WaitAgentAlive blocks until the respective agent is alive. |
-func (m *Machine) WaitAgentAlive(timeout time.Duration) error { |
+func (m *Machine) WaitAgentAlive(timeout time.Duration) (err error) { |
+ defer trivial.ErrorContextf(&err, "waiting for agent of machine %v", m) |
ch := make(chan presence.Change) |
- m.st.presencew.Add(m.globalKey(), ch) |
- defer m.st.presencew.Remove(m.globalKey(), ch) |
- // Initial check. |
- select { |
- case change := <-ch: |
- if change.Alive { |
- return nil |
- } |
- case <-time.After(timeout): |
- return fmt.Errorf("waiting for agent of machine %v: still not alive after timeout", m) |
- } |
- // Hasn't been alive, so now wait for change. |
- select { |
- case change := <-ch: |
- if change.Alive { |
- return nil |
- } |
- panic(fmt.Sprintf("presence reported dead status twice in a row for machine %v", m)) |
- case <-time.After(timeout): |
- return fmt.Errorf("waiting for agent of machine %v: still not alive after timeout", m) |
- } |
- panic("unreachable") |
+ m.st.pwatcher.Watch(m.globalKey(), ch) |
+ defer m.st.pwatcher.Unwatch(m.globalKey(), ch) |
+ for i := 0; i < 2; i++ { |
+ select { |
+ case change := <-ch: |
+ if change.Alive { |
+ return nil |
+ } |
+ case <-time.After(timeout): |
+ return fmt.Errorf("still not alive after timeout") |
+ case <-m.st.pwatcher.Dying(): |
+ return m.st.pwatcher.Err() |
+ } |
+ } |
+ panic(fmt.Sprintf("presence reported dead status twice in a row for machine %v", m)) |
} |
// SetAgentAlive signals that the agent for machine m is alive. |