mirror of https://github.com/docker/docs.git
watch take a callback
This commit is contained in:
parent
c85b0a9c39
commit
005b5fe288
|
|
@ -5,9 +5,9 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
|
"github.com/docker/swarm/cluster"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
|
|
@ -28,7 +28,7 @@ func (n Node) String() string {
|
||||||
type DiscoveryService interface {
|
type DiscoveryService interface {
|
||||||
Initialize(string, int) error
|
Initialize(string, int) error
|
||||||
Fetch() ([]*Node, error)
|
Fetch() ([]*Node, error)
|
||||||
Watch() <-chan time.Time
|
Watch(*cluster.Cluster, func(*cluster.Cluster, []*Node))
|
||||||
Register(string) error
|
Register(string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,10 @@ package etcd
|
||||||
import (
|
import (
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
"github.com/coreos/go-etcd/etcd"
|
"github.com/coreos/go-etcd/etcd"
|
||||||
|
"github.com/docker/swarm/cluster"
|
||||||
"github.com/docker/swarm/discovery"
|
"github.com/docker/swarm/discovery"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -64,18 +64,16 @@ func (s *EtcdDiscoveryService) Fetch() ([]*discovery.Node, error) {
|
||||||
return nodes, nil
|
return nodes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *EtcdDiscoveryService) Watch() <-chan time.Time {
|
func (s *EtcdDiscoveryService) Watch(c *cluster.Cluster, refresh func(c *cluster.Cluster, nodes []*discovery.Node)) {
|
||||||
watchChan := make(chan *etcd.Response)
|
watchChan := make(chan *etcd.Response)
|
||||||
timeChan := make(chan time.Time)
|
|
||||||
go s.client.Watch(s.path, 0, true, watchChan, nil)
|
go s.client.Watch(s.path, 0, true, watchChan, nil)
|
||||||
go func() {
|
for _ = range watchChan {
|
||||||
for {
|
log.Debugf("[ETCD] Watch triggered")
|
||||||
<-watchChan
|
nodes, err := s.Fetch()
|
||||||
log.Debugf("[ETCD] Watch triggered")
|
if err == nil {
|
||||||
timeChan <- time.Now()
|
refresh(c, nodes)
|
||||||
}
|
}
|
||||||
}()
|
}
|
||||||
return timeChan
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *EtcdDiscoveryService) Register(addr string) error {
|
func (s *EtcdDiscoveryService) Register(addr string) error {
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/docker/swarm/cluster"
|
||||||
"github.com/docker/swarm/discovery"
|
"github.com/docker/swarm/discovery"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -44,8 +45,13 @@ func (s *FileDiscoveryService) Fetch() ([]*discovery.Node, error) {
|
||||||
return nodes, nil
|
return nodes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *FileDiscoveryService) Watch() <-chan time.Time {
|
func (s *FileDiscoveryService) Watch(c *cluster.Cluster, refresh func(c *cluster.Cluster, nodes []*discovery.Node)) {
|
||||||
return time.Tick(time.Duration(s.heartbeat) * time.Second)
|
for _ = range time.Tick(time.Duration(s.heartbeat) * time.Second) {
|
||||||
|
nodes, err := s.Fetch()
|
||||||
|
if err == nil {
|
||||||
|
refresh(c, nodes)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *FileDiscoveryService) Register(addr string) error {
|
func (s *FileDiscoveryService) Register(addr string) error {
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/docker/swarm/cluster"
|
||||||
"github.com/docker/swarm/discovery"
|
"github.com/docker/swarm/discovery"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -66,8 +67,13 @@ func (s *TokenDiscoveryService) Fetch() ([]*discovery.Node, error) {
|
||||||
return nodes, nil
|
return nodes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TokenDiscoveryService) Watch() <-chan time.Time {
|
func (s *TokenDiscoveryService) Watch(c *cluster.Cluster, refresh func(c *cluster.Cluster, nodes []*discovery.Node)) {
|
||||||
return time.Tick(time.Duration(s.heartbeat) * time.Second)
|
for _ = range time.Tick(time.Duration(s.heartbeat) * time.Second) {
|
||||||
|
nodes, err := s.Fetch()
|
||||||
|
if err == nil {
|
||||||
|
refresh(c, nodes)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterNode adds a new node identified by the into the discovery service
|
// RegisterNode adds a new node identified by the into the discovery service
|
||||||
|
|
|
||||||
|
|
@ -106,14 +106,7 @@ func manage(c *cli.Context) {
|
||||||
}
|
}
|
||||||
refresh(cluster, nodes)
|
refresh(cluster, nodes)
|
||||||
|
|
||||||
go func() {
|
go d.Watch(cluster, refresh)
|
||||||
for _ = range d.Watch() {
|
|
||||||
nodes, err = d.Fetch()
|
|
||||||
if err == nil {
|
|
||||||
refresh(cluster, nodes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
} else {
|
} else {
|
||||||
var nodes []*discovery.Node
|
var nodes []*discovery.Node
|
||||||
for _, arg := range c.Args() {
|
for _, arg := range c.Args() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue