Пример #1
0
func (s *Store) Watch() error {
	watcherOpts := client.WatcherOptions{AfterIndex: 0, Recursive: true}
	w := s.kv.Watcher(s.config.Namespace, &watcherOpts)

	s.Init()

	for {
		r, err := w.Next(context.Background())
		if err != nil {
			printer.LogErrf("Error occurred: %e", err)
			time.Sleep(ReconnectTime)
			continue
		}

		switch r.Action {
		case "set", "update", "create":
			s.Updated(r.Node)
		case "delete":
			resp, err := s.kv.Get(context.Background(), s.config.Namespace, nil)
			if err != nil {
				printer.LogErrf("Error occurred: %e", err)
			}

			s.Updated(resp.Node)
		}

	}
}
Пример #2
0
func (s *Store) UpdateKeys() {
	kvbs, err := s.List(s.cfg.Namespace)

	if err != nil {
		printer.LogErrf("pubsub error: %v", err)
	}

	s.Updated(kvbs)
}
Пример #3
0
func (s *Store) messageHandler() {
	for {
		switch n := s.psc.Receive().(type) {
		case redis.PMessage:
			s.UpdateKeys()
		case error:
			printer.LogErrf("watch error: %v", n.Error())
			return
		}
	}
}
Пример #4
0
func (cs *Store) Updated(kvs interface{}) {
	kvp := kvs.(api.KVPairs)
	kvb, err := KvPairsToKvBytes(kvp)

	if err != nil {
		printer.LogErrf("%v", err)
		return
	}

	cs.cb(kvb)
}
Пример #5
0
func (cc *Controller) Serve(ctx climax.Context) int {
	c, err := client.New(cc.Config)

	if err != nil {
		printer.LogErrf("%v", err)
	}

	s := server.New(cc.Config, c)

	printer.Logf("pid: %d serving %s on %s", os.Getpid(),
		cc.Config.Server.Endpoint, cc.Config.Server.Host)

	err = s.Serve()

	if err != nil {
		printer.LogErrf("%v", err)
		return 1
	}

	return 0
}
Пример #6
0
// New initializes a Watcher and verifies that `path` exists.
func New(path string) (w *Watcher) {
	_, err := os.Stat(path)

	if err != nil {
		printer.LogErrf("could not start watcher: %v", err)
		return nil
	}

	w = &Watcher{
		path: path,
	}

	return
}
Пример #7
0
func (cs *Store) Watch() error {
	params := map[string]interface{}{
		"type":   "keyprefix",
		"prefix": cs.cfg.Namespace,
	}

	wp, err := watch.Parse(params)
	defer wp.Stop()

	if err != nil {
		printer.LogErrf("%v", err)
	}

	wp.Handler = func(idx uint64, data interface{}) {
		cs.Updated(data)
	}

	if err := wp.Run(cs.cfg.Consul.Address); err != nil {
		printer.LogErrf("Error querying Consul agent: %s", err)
	}

	return nil
}
Пример #8
0
func (c *Client) WriteOutputFile(kvb stores.KVBytes) {
	fts, err := c.KVsToFeatureMap(kvb)

	if err != nil {
		printer.LogErrf("parse features error: %v", err)
		os.Exit(1)
	}

	bts, err := json.MarshalIndent(fts, "", "  ")

	if err != nil {
		printer.LogErrf("%v", err)
		os.Exit(1)
	}

	err = ioutil.WriteFile(c.config.Watcher.OutputPath, bts, 0644)

	if err != nil {
		printer.LogErrf("%v", err)
		os.Exit(1)
	}

	printer.Logf("wrote changes to: %s", c.config.Watcher.OutputPath)
}
Пример #9
0
// Init etcd watches do not fire an initial event. This method triggers
// a write to the file systems of the entire keyspace.
func (s *Store) Init() {
	opts := &client.GetOptions{
		Recursive: true,
		Sort:      true,
		Quorum:    true,
	}

	resp, err := s.kv.Get(context.Background(), s.config.Namespace, opts)

	if etcdError(err) != nil {
		printer.LogErrf("Error occurred: %e", err)
	}

	if resp != nil {
		s.Updated(resp.Node)
	}
}
Пример #10
0
// Watch observes WRITE events, forwarding them to `Updated`
func (w *Watcher) Watch() {
	done := make(chan bool)
	go func() {
		for {
			w.mu.Lock()
			select {
			case event := <-w.watcher.Events:
				if event.Op&fsnotify.Write == fsnotify.Write {
					w.UpdateBytes()
				}
			case err := <-w.watcher.Errors:
				printer.LogErrf("[dcdr] watch error: %v", err)
			}
			w.mu.Unlock()
		}
	}()

	defer w.Close()

	<-done
}