Add a ping function
This commit is contained in:
		
							
								
								
									
										68
									
								
								cmd/cmd.go
									
									
									
									
									
								
							
							
						
						
									
										68
									
								
								cmd/cmd.go
									
									
									
									
									
								
							| @@ -1,13 +1,19 @@ | |||||||
| package cmd | package cmd | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
|  | 	"bytes" | ||||||
|  | 	"encoding/json" | ||||||
| 	"flag" | 	"flag" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io" | 	"io" | ||||||
|  | 	"math/rand" | ||||||
|  | 	"net/http" | ||||||
| 	"os" | 	"os" | ||||||
|  | 	"runtime" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"text/tabwriter" | 	"text/tabwriter" | ||||||
| 	"text/template" | 	"text/template" | ||||||
|  | 	"time" | ||||||
|  |  | ||||||
| 	"github.com/codegangsta/cli" | 	"github.com/codegangsta/cli" | ||||||
| 	"github.com/micro/go-micro/broker" | 	"github.com/micro/go-micro/broker" | ||||||
| @@ -15,6 +21,7 @@ import ( | |||||||
| 	"github.com/micro/go-micro/registry" | 	"github.com/micro/go-micro/registry" | ||||||
| 	"github.com/micro/go-micro/server" | 	"github.com/micro/go-micro/server" | ||||||
| 	"github.com/micro/go-micro/transport" | 	"github.com/micro/go-micro/transport" | ||||||
|  | 	"github.com/pborman/uuid" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| var ( | var ( | ||||||
| @@ -85,6 +92,12 @@ var ( | |||||||
| 			Usage:  "Comma-separated list of transport addresses", | 			Usage:  "Comma-separated list of transport addresses", | ||||||
| 		}, | 		}, | ||||||
|  |  | ||||||
|  | 		cli.BoolFlag{ | ||||||
|  | 			Name:   "disable_ping", | ||||||
|  | 			EnvVar: "MICRO_DISABLE_PING", | ||||||
|  | 			Usage:  "Disable ping", | ||||||
|  | 		}, | ||||||
|  |  | ||||||
| 		// logging flags | 		// logging flags | ||||||
| 		cli.BoolFlag{ | 		cli.BoolFlag{ | ||||||
| 			Name:  "logtostderr", | 			Name:  "logtostderr", | ||||||
| @@ -129,6 +142,57 @@ var ( | |||||||
| 	} | 	} | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | func init() { | ||||||
|  | 	rand.Seed(time.Now().Unix()) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // ping informs micro-services about this thing | ||||||
|  | func ping() { | ||||||
|  | 	type Ping struct { | ||||||
|  | 		Id        string | ||||||
|  | 		Timestamp int64 | ||||||
|  | 		Product   string | ||||||
|  | 		Version   string | ||||||
|  | 		Arch      string | ||||||
|  | 		Os        string | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	p := Ping{ | ||||||
|  | 		Id:      uuid.NewUUID().String(), | ||||||
|  | 		Product: "go-micro", | ||||||
|  | 		Version: "latest", | ||||||
|  | 		Arch:    runtime.GOARCH, | ||||||
|  | 		Os:      runtime.GOOS, | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	buf := bytes.NewBuffer(nil) | ||||||
|  | 	cl := &http.Client{} | ||||||
|  |  | ||||||
|  | 	fn := func() { | ||||||
|  | 		p.Timestamp = time.Now().Unix() | ||||||
|  | 		b, err := json.Marshal(p) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return | ||||||
|  | 		} | ||||||
|  | 		buf.Reset() | ||||||
|  | 		buf.Write(b) | ||||||
|  | 		rsp, err := cl.Post("https://micro-services.co/_ping", "application/json", buf) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return | ||||||
|  | 		} | ||||||
|  | 		rsp.Body.Close() | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// don't ping unless this thing has lived for 30 seconds | ||||||
|  | 	time.Sleep(time.Second * 30) | ||||||
|  |  | ||||||
|  | 	// only ping every 24 hours, be non invasive | ||||||
|  | 	for { | ||||||
|  | 		fn() | ||||||
|  | 		time.Sleep(time.Hour * 24) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
| func Setup(c *cli.Context) error { | func Setup(c *cli.Context) error { | ||||||
| 	os.Args = os.Args[:1] | 	os.Args = os.Args[:1] | ||||||
|  |  | ||||||
| @@ -176,6 +240,10 @@ func Setup(c *cli.Context) error { | |||||||
|  |  | ||||||
| 	client.DefaultClient = client.NewClient() | 	client.DefaultClient = client.NewClient() | ||||||
|  |  | ||||||
|  | 	if !c.Bool("disable_ping") { | ||||||
|  | 		go ping() | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user