Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1106)

Side by Side Diff: state/agent.go

Issue 5782053: Implement agent interface and helper. (Closed)
Patch Set: Implement agent interface and helper. Created 5 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // launchpad.net/juju/state
2 //
3 // Copyright (c) 2011-2012 Canonical Ltd.
4 package state
5
6 import (
7 "fmt"
8 "launchpad.net/juju/go/state/presence"
9 "time"
10 )
11
12 const (
13 agentPingerPeriod = 1 * time.Second
14 )
15
16 // agent represents a juju agent.
17 type agent struct {
18 st *State
19 path string
20 pinger *presence.Pinger
21 }
22
23 // Connected returns true if its entity state has an agent connected.
24 func (a *agent) Connected() (bool, error) {
25 return presence.Alive(a.st.zk, a.path)
26 }
27
28 // WaitConnected waits until an agent has connected.
29 func (a *agent) WaitConnected(timeout time.Duration) error {
30 alive, watch, err := presence.AliveW(a.st.zk, a.path)
31 if err != nil {
32 return err
33 }
34 // Quick return if already connected.
35 if alive {
36 return nil
37 }
38 // Wait for connection with timeout.
39 select {
40 case alive, ok := <-watch:
41 if !ok {
42 return fmt.Errorf("wait connection closed")
43 }
44 if !alive {
45 return fmt.Errorf("not connected, must not happen")
46 }
47 case <-time.After(timeout):
48 return fmt.Errorf("wait for connected agent timed out")
49 }
50 return nil
51 }
52
53 // Connect informs juju that an associated agent is alive.
54 func (a *agent) Connect() (err error) {
55 if a.pinger != nil {
56 return fmt.Errorf("agent is already connected")
57 }
58 a.pinger, err = presence.StartPinger(a.st.zk, a.path, agentPingerPeriod)
59 return
60 }
61
62 // Disconnect informs juju that an associated agent stops working.
63 func (a *agent) Disconnect() error {
64 if a.pinger == nil {
65 return fmt.Errorf("agent is not connected")
66 }
67 a.pinger.Kill()
68 a.pinger = nil
69 return nil
70 }
OLDNEW
« no previous file with comments | « [revision details] ('k') | state/export_test.go » ('j') | state/export_test.go » ('J')

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld 204d58d