Example #1
0
func watch(c config.Config, key ...string) {
	fmt.Println("Subscribing to changes for", key)

	w, err := c.Watch(key...)
	if err != nil {
		fmt.Println(err)
		return
	}

	go func() {
		for {
			v, err := w.Next()
			if err != nil {
				fmt.Println(err)
				return
			}
			fmt.Println("Received value for key:", v.String("default"))
		}
	}()

	for i := 0; i < 10; i++ {
		fmt.Println("Writing change to file")
		writeFile(i + 1)
		time.Sleep(time.Second)
	}

	fmt.Println("Stopping subscriber")
	w.Stop()
}
Example #2
0
func (r *router) run(c config.Config) {
	var routes Routes

	// load routes immediately if possible
	if err := c.Get(DefaultPath...).Scan(&routes); err != nil {
		DefaultLogger.Error("[router] Failed to get routes", err)
	} else {
		r.update(routes)
	}

	var watcher config.Watcher

	// try to get a watch
	for i := 0; i < 100; i++ {
		w, err := c.Watch(DefaultPath...)
		if err != nil {
			DefaultLogger.Error("[router] Failed to get watcher", err)
			time.Sleep(time.Second)
			continue
		}
		watcher = w
		break
	}

	// if the watch is nil we exit
	if watcher == nil {
		DefaultLogger.Fatal("[router] Failed to get watcher in 100 attempts")
	}

	// watch and update routes
	for {
		// get next
		v, err := watcher.Next()
		if err != nil {
			DefaultLogger.Error("[router] Watcher Next() Error", err)
			time.Sleep(time.Second)
			continue
		}

		var routes Routes

		// scan into routes
		if err := v.Scan(&routes); err != nil {
			DefaultLogger.Error("[router] Failed to scan routes... skipping update", err)
			continue
		}

		// update the routes
		r.update(routes)
	}
}