consulWatch

package module
v0.0.0-...-6531970 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2020 License: MIT Imports: 5 Imported by: 0

README

consulwatch

consulwatch is a Go library for watching Consul KV values. 3 state change: create, update, delete with channel separation or without

Installation

Standard go get:

$ go get github.com/mitchellh/consulstructure

Usage & Example

package main

import (
	"fmt"
	"log"
	"os"
	"os/signal"
	"syscall"

	cw "github.com/SPROgster/consulwatch"
)

func main() {
	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, syscall.SIGTERM)

	cr := make(chan map[string]interface{})
	de := make(chan map[string]interface{})
	upd:= make(chan map[string]interface{})

	watched := cw.ConsulWatched{
		Prefix: "someprefix",
		CreateChan: cr,
		DeleteChan: de,
		UpdateChan: upd,
	}

	err := watched.Watch()
	if err != nil {
		log.Fatal(err)
	}
	defer watched.Stop()

	for {
		select {
		case <-sigs:
			fmt.Println("Stopping")
			return
		case a := <-watched.UpdateChan:
			fmt.Printf("updated: %v\n", a)
		case a := <-watched.CreateChan:
			fmt.Printf("created: %v\n", a)
		case a := <-watched.DeleteChan:
			fmt.Printf("deleted: %v\n", a)
		}
	}
}

If no different channels needed, consilwatch allows notifications via update channel. Deleted values has nil in interface{}. In this case, update chan will be created in inside Watch() function

Example:

watched := cw.ConsulWatched{
		Prefix: "someprefix",
	}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var WaitTimeout = 10 * time.Minute

Functions

This section is empty.

Types

type ConsulWatched

type ConsulWatched struct {
	Consul      *api.Client
	Prefix      string
	UpdateChan  chan map[string]interface{}
	CreateChan  chan map[string]interface{}
	DeleteChan  chan map[string]interface{}
	ErrChan     chan error
	WaitTimeout time.Duration
	// contains filtered or unexported fields
}

func (*ConsulWatched) Stop

func (w *ConsulWatched) Stop()

func (*ConsulWatched) Watch

func (w *ConsulWatched) Watch() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL