// publish func publish(ccmd *cobra.Command, args []string) error { // missing tags if tags == nil { fmt.Println("Unable to publish - Missing tags") return fmt.Errorf("") } // missing data if data == "" { fmt.Println("Unable to publish - Missing data") return fmt.Errorf("") } client, err := clients.New(host, viper.GetString("token")) if err != nil { fmt.Printf("Failed to connect to '%v' - %v\n", host, err) return err } err = client.Publish(tags, data) if err != nil { fmt.Printf("Failed to publish message - %v\n", err) return err } fmt.Println("success") return nil }
func startPulse(ccmd *cobra.Command, args []string) error { // re-initialize logger lumber.Level(lumber.LvlInt(viper.GetString("log-level"))) plex := plexer.NewPlexer() if viper.GetString("mist-address") != "" { mist, err := mist.New(viper.GetString("mist-address"), viper.GetString("mist-token")) if err != nil { return fmt.Errorf("Mist failed to start - %s", err.Error()) } plex.AddObserver("mist", mist.Publish) defer mist.Close() } plex.AddBatcher("influx", influx.Insert) err := pulse.Listen(viper.GetString("server-listen-address"), plex.Publish) if err != nil { return fmt.Errorf("Pulse failed to start - %s", err.Error()) } // begin polling the connected servers pollSec := viper.GetInt("poll-interval") if pollSec == 0 { pollSec = 60 } go pulse.StartPolling(nil, nil, time.Duration(pollSec)*time.Second, nil) queries := []string{ "CREATE DATABASE statistics", "CREATE RETENTION POLICY one_day ON statistics DURATION 8h REPLICATION 1 DEFAULT", fmt.Sprintf("CREATE RETENTION POLICY one_week ON statistics DURATION %dw REPLICATION 1", viper.GetInt("retention")), // todo: ALTER as well? } for _, query := range queries { _, err := influx.Query(query) if err != nil { return fmt.Errorf("Failed to query influx - %s", err.Error()) } } go influx.KeepContinuousQueriesUpToDate() if viper.GetString("kapacitor-address") != "" { err := kapacitor.Init() if err != nil { return fmt.Errorf("Kapacitor failed to start - %s", err.Error()) } } err = api.Start() if err != nil { return fmt.Errorf("Api failed to start - %s", err.Error()) } return nil }
func (m *Mist) retryPublish(tags []string, data string) error { c, err := mistCore.New(m.address, config.PubAuth) if err != nil { return fmt.Errorf("Failed to redial mist - %v", err) } m.mist = c return m.mist.Publish(tags, data) }
// TestBadTCPClientConnect tests to ensure a client can connect to a running server func TestBadTCPClientConnect(t *testing.T) { client, err := clients.New("321321321", "") if err == nil { t.Fatalf("Client succeeded to connect") } client, err = clients.New(testAddr, "hil") if err != nil { t.Fatalf("Client failed to connect - %v", err.Error()) t.FailNow() } defer client.Close() // if err := client.Ping(); err != nil { t.Fatalf("ping failed") } if msg := <-client.Messages(); msg.Data != "pong" { t.Fatalf("Unexpected data: Expecting 'pong' got %s", msg.Data) } client.Ping() }
// NewMistClient creates a new mist publisher func NewMistClient(address string) (*Mist, error) { c, err := mistCore.New(address, config.PubAuth) if err != nil { return nil, err } m := Mist{ address: address, mist: c, } return &m, nil }
// ping func ping(ccmd *cobra.Command, args []string) error { client, err := clients.New(host, viper.GetString("token")) if err != nil { fmt.Printf("Failed to connect to '%v' - %v\n", host, err) return err } err = client.Ping() if err != nil { fmt.Printf("Failed to ping - %v\n", err) return err } msg := <-client.Messages() fmt.Println(msg.Data) return nil }
// who gets connection stats for a mist server func who(ccmd *cobra.Command, args []string) error { // create new mist client client, err := clients.New(host, viper.GetString("token")) if err != nil { fmt.Printf("Failed to connect to '%v' - %v\n", host, err) return err } // who related err = client.Who() if err != nil { fmt.Printf("Failed to who - %v\n", err) return err } msg := <-client.Messages() fmt.Println(msg.Data) return nil }
// subscribe func subscribe(ccmd *cobra.Command, args []string) error { // missing tags if tags == nil { fmt.Println("Unable to subscribe - Missing tags") return fmt.Errorf("") } client, err := clients.New(host, viper.GetString("token")) if err != nil { fmt.Printf("Failed to connect to '%v' - %v\n", host, err) return err } // if err := client.Subscribe(tags); err != nil { fmt.Printf("Unable to subscribe - %v\n", err.Error()) return fmt.Errorf("") } // listen for messages on tags fmt.Printf("Listening on tags '%v'\n", tags) for msg := range client.Messages() { // skip handler messages if msg.Data != "success" { if viper.GetString("log-level") == "DEBUG" { fmt.Printf("Message: %#v\n", msg) } else { fmt.Println(msg.Data) } } } return nil }
// TestTCPClient tests to ensure a client can run all of its expected commands; // we don't have to actually test any of the results of the commands since those // are already tested in other tests (proxy_test and subscriptions_test in the // core package) func TestTCPClient(t *testing.T) { // client, err := clients.New(testAddr, "") if err != nil { t.Fatalf("failed to connect - %v", err.Error()) t.FailNow() } defer client.Close() // subscribe should fail with no tags if err := client.Subscribe([]string{}); err == nil { t.Fatalf("Subscription succeeded with missing tags!") } // test ability to subscribe if err := client.Subscribe([]string{"a"}); err != nil { t.Fatalf("client subscriptions failed %v", err.Error()) } // test ability to list (subscriptions) if err := client.List(); err != nil { t.Fatalf("listing subscriptions failed %v", err.Error()) } if msg := <-client.Messages(); msg.Data == "\"a\"" { t.Fatalf("Failed to 'list' - '%v' '%#v'", msg.Error, msg.Data) } // test publish if err := client.Publish([]string{"a"}, "testpublish"); err != nil { t.Fatalf("publishing failed %v", err.Error()) } if err := client.Publish([]string{}, "nopublish"); err == nil { t.Fatalf("publishing no tags succeeded %v", err.Error()) } if err := client.Publish([]string{"a"}, ""); err == nil { t.Fatalf("publishing no data succeeded %v", err.Error()) } // test PublishAfter if err := client.PublishAfter([]string{"a"}, "testpublish", time.Second); err != nil { t.Fatalf("publishing failed %v", err.Error()) } time.Sleep(time.Millisecond * 1500) // test ability to unsubscribe if err := client.Unsubscribe([]string{"a"}); err != nil { t.Fatalf("client unsubscriptions failed %v", err.Error()) } if err := client.Unsubscribe([]string{}); err == nil { t.Fatalf("client unsubscriptions no tags succeeded %v", err.Error()) } // test ability to list (no subscriptions) if err := client.List(); err != nil { t.Fatalf("listing subscriptions failed %v", err.Error()) } if msg := <-client.Messages(); msg.Data != "" { t.Fatalf("Failed to 'list' - %v", msg.Error) } }