Ejemplo n.º 1
0
func NewHandlerProvider(settings boshsettings.Service, logger boshlog.Logger) (p mbusHandlerProvider) {
	p.settings = settings
	p.handlers = map[string]Handler{
		"nats": newNatsHandler(settings, logger, yagnats.NewClient()),
	}
	return
}
Ejemplo n.º 2
0
func main() {
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)

	log.Printf("Sending messages...\n")

	client := yagnats.NewClient()
	err := client.Connect(&yagnats.ConnectionInfo{"127.0.0.1:4222", "nats", "nats"})
	if err != nil {
		log.Fatalf("Error connecting: %s\n", err)
	}

	seen := 0

	client.Subscribe("foo", func(msg *yagnats.Message) {
		seen += 1
	})

	go func() {
		for {
			client.Publish("foo", []byte("hi"))
		}
	}()

	<-c
	log.Printf("Messages processed: %d\n", seen)
}
Ejemplo n.º 3
0
func connectToMessageBus(l logger.Logger, conf *config.Config) yagnats.NATSClient {
	members := []yagnats.ConnectionProvider{}

	for _, natsConf := range conf.NATS {
		members = append(members, &yagnats.ConnectionInfo{
			Addr: fmt.Sprintf("%s:%d", natsConf.Host, natsConf.Port),

			Username: natsConf.User,
			Password: natsConf.Password,
		})
	}

	connectionInfo := &yagnats.ConnectionCluster{
		Members: members,
	}

	natsClient := yagnats.NewClient()

	err := natsClient.Connect(connectionInfo)

	if err != nil {
		l.Error("Failed to connect to the message bus", err)
		os.Exit(1)
	}

	return natsClient
}
Ejemplo n.º 4
0
func (runner *NATSRunner) Start() {
	if runner.natsSession != nil {
		panic("starting an already started NATS runner!!!")
	}

	_, err := exec.LookPath("gnatsd")
	if err != nil {
		fmt.Println("You need gnatsd installed!")
		os.Exit(1)
	}

	cmd := exec.Command("gnatsd", "-p", strconv.Itoa(runner.port))
	sess, err := gexec.Start(
		cmd,
		gexec.NewPrefixedWriter("\x1b[32m[o]\x1b[34m[gnatsd]\x1b[0m ", ginkgo.GinkgoWriter),
		gexec.NewPrefixedWriter("\x1b[91m[e]\x1b[34m[gnatsd]\x1b[0m ", ginkgo.GinkgoWriter),
	)
	Ω(err).ShouldNot(HaveOccurred(), "Make sure to have gnatsd on your path")

	runner.natsSession = sess

	connectionInfo := &yagnats.ConnectionInfo{
		Addr: fmt.Sprintf("127.0.0.1:%d", runner.port),
	}

	messageBus := yagnats.NewClient()

	Eventually(func() error {
		return messageBus.Connect(connectionInfo)
	}, 5, 0.1).ShouldNot(HaveOccurred())

	runner.MessageBus = messageBus
}
Ejemplo n.º 5
0
func TestHandlerProviderGetReturnsNatsHandler(t *testing.T) {
	deps, provider := buildProvider("nats://0.0.0.0")
	handler, err := provider.Get(deps.platform, deps.dirProvider)

	assert.NoError(t, err)
	assert.IsType(t, NewNatsHandler(deps.settings, deps.logger, yagnats.NewClient()), handler)
}
Ejemplo n.º 6
0
func (p mbusHandlerProvider) Get() (handler Handler, err error) {
	if p.handler != nil {
		handler = p.handler
		return
	}

	mbusUrl, err := url.Parse(p.settings.GetMbusUrl())
	if err != nil {
		err = bosherr.WrapError(err, "Parsing handler URL")
		return
	}

	switch mbusUrl.Scheme {
	case "nats":
		handler = newNatsHandler(p.settings, p.logger, yagnats.NewClient())
	case "https":
		handler = newHttpsHandler(mbusUrl, p.logger)
	default:
		err = bosherr.New("Message Bus Handler with scheme %s could not be found", mbusUrl.Scheme)
	}

	p.handler = handler

	return
}
Ejemplo n.º 7
0
func main() {
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)

	log.Printf("Receiving messages...\n")

	client := yagnats.NewClient()
	err := client.Connect(&yagnats.ConnectionInfo{"127.0.0.1:4222", "nats", "nats"})
	if err != nil {
		log.Fatalf("Error connecting: %s\n", err)
	}

	seen := 0

	client.Subscribe("foo", func(msg *yagnats.Message) {
		for i := 0; i < 1000000; i++ {
			fmt.Printf("")
		}
		seen += 1
		fmt.Printf("got it! %d\n", seen)
	})

	<-c
	log.Printf("Messages processed: %d\n", seen)
}
Ejemplo n.º 8
0
func (p MbusHandlerProvider) Get(
	platform boshplatform.Platform,
	dirProvider boshdir.DirectoriesProvider,
) (handler boshhandler.Handler, err error) {
	if p.handler != nil {
		handler = p.handler
		return
	}

	mbusURL, err := url.Parse(p.settings.GetMbusURL())
	if err != nil {
		err = bosherr.WrapError(err, "Parsing handler URL")
		return
	}

	switch mbusURL.Scheme {
	case "nats":
		handler = NewNatsHandler(p.settings, p.logger, yagnats.NewClient())
	case "https":
		handler = micro.NewHTTPSHandler(mbusURL, p.logger, platform.GetFs(), dirProvider)
	default:
		err = bosherr.New("Message Bus Handler with scheme %s could not be found", mbusURL.Scheme)
	}

	p.handler = handler

	return
}
Ejemplo n.º 9
0
func init() {
	Describe("Testing with Ginkgo", func() {
		It("handler provider get returns nats handler", func() {
			deps, provider := buildProvider("nats://0.0.0.0")
			handler, err := provider.Get(deps.platform, deps.dirProvider)

			Expect(err).ToNot(HaveOccurred())
			assert.IsType(GinkgoT(), NewNatsHandler(deps.settings, deps.logger, yagnats.NewClient()), handler)
		})
		It("handler provider get returns https handler", func() {

			deps, provider := buildProvider("https://0.0.0.0")
			handler, err := provider.Get(deps.platform, deps.dirProvider)

			Expect(err).ToNot(HaveOccurred())
			assert.IsType(GinkgoT(), micro.HttpsHandler{}, handler)
		})
		It("handler provider get returns an error if not supported", func() {

			deps, provider := buildProvider("foo://0.0.0.0")
			_, err := provider.Get(deps.platform, deps.dirProvider)

			Expect(err).To(HaveOccurred())
		})
	})
}
Ejemplo n.º 10
0
func NewHandlerProvider(settings boshsettings.Settings) (p mbusHandlerProvider) {
	p.settings = settings
	p.handlers = map[string]Handler{
		"nats": newNatsHandler(yagnats.NewClient(), settings),
	}
	return
}
Ejemplo n.º 11
0
func (adapter *NatsAdapter) connect() error {
	addr := fmt.Sprintf("%s:%d", adapter.host, adapter.port)

	client := nats.NewClient()

	client.ConnectedCallback = func() {
		if adapter.connectedCallback != nil {
			adapter.connectedCallback()
		}
	}

	if adapter.logger != nil {
		client.Logger = adapter.logger
	}

	err := client.Connect(&nats.ConnectionInfo{
		Addr:     addr,
		Username: adapter.user,
		Password: adapter.password,
	})

	if err != nil {
		return err
	}

	adapter.client = client
	adapter.rand = rand.New(rand.NewSource(time.Now().UnixNano()))

	for _, sub := range adapter.subscriptions {
		subscribeInNats(adapter, sub)
	}

	return nil
}
Ejemplo n.º 12
0
func main() {
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)

	log.Printf("Sending messages...\n")

	client := yagnats.NewClient()
	err := client.Connect(&yagnats.ConnectionInfo{
		Addr:     "127.0.0.1:4222",
		Username: "******",
		Password: "******",
	})
	if err != nil {
		log.Fatalf("Error connecting: %s\n", err)
	}

	bigbyte := make([]byte, 512000)
	go func() {
		for {
			client.Publish("foo", bigbyte)
		}
	}()

	<-c
	log.Printf("Bye!\n")
}
Ejemplo n.º 13
0
func buildMessageBus(registrar *Registrar) (messageBus yagnats.NATSClient) {

	messageBus = yagnats.NewClient()
	natsServers := []yagnats.ConnectionProvider{}

	for _, server := range registrar.Config.MessageBusServers {
		registrar.logger.Info(
			"Adding NATS server",
			lager.Data{"server": server},
		)
		natsServers = append(natsServers, &yagnats.ConnectionInfo{
			server.Host,
			server.User,
			server.Password,
			nil,
		})
	}

	natsInfo := &yagnats.ConnectionCluster{natsServers}

	err := messageBus.Connect(natsInfo)

	if err != nil {
		registrar.logger.Info(
			"Error connecting to NATS",
			lager.Data{"error": err.Error()},
		)
		panic("Failed to connect to NATS bus.")
	}

	registrar.logger.Info("Successfully connected to NATS.")

	return
}
Ejemplo n.º 14
0
func main() {
	flag.Parse()

	nats := yagnats.NewClient()

	natsMembers := []yagnats.ConnectionProvider{}

	if *natsAddresses == "" {
		log.Fatalln("must specify at least one nats address (-natsAddresses=1.2.3.4:5678)")
	}

	if *ip == "" {
		log.Fatalln("must specify IP to route to (-ip=X)")
	}

	for _, addr := range strings.Split(*natsAddresses, ",") {
		log.Println("configuring nats server:", addr)
		natsMembers = append(natsMembers, &yagnats.ConnectionInfo{
			Addr:     addr,
			Username: *natsUsername,
			Password: *natsPassword,
		})
	}

	if len(natsMembers) == 0 {
		log.Fatalln("must specify at least one nats address")
	}

	natsInfo := &yagnats.ConnectionCluster{natsMembers}

	for {
		err := nats.Connect(natsInfo)
		if err == nil {
			break
		}

		log.Println("failed to connect to NATS:", err)
		time.Sleep(1 * time.Second)
	}

	client := gibson.NewCFRouterClient(*ip, nats)

	client.Greet()

	for _, route := range strings.Split(*routes, ",") {
		routePair := strings.Split(route, ":")
		if len(routePair) != 2 {
			log.Fatalln("invalid route configuration:", *routes)
		}

		port, err := strconv.Atoi(routePair[0])
		if err != nil {
			log.Fatalln("invalid route port:", err)
		}

		client.Register(port, routePair[1])
	}

	select {}
}
Ejemplo n.º 15
0
func NewRouter(c *config.Config) *Router {
	router := &Router{
		config: c,
	}

	// setup number of procs
	if router.config.GoMaxProcs != 0 {
		runtime.GOMAXPROCS(router.config.GoMaxProcs)
	}

	router.mbusClient = yagnats.NewClient()

	router.registry = registry.NewCFRegistry(router.config, router.mbusClient)
	router.registry.StartPruningCycle()

	router.varz = varz.NewVarz(router.registry)
	args := proxy.ProxyArgs{
		EndpointTimeout: router.config.EndpointTimeout,
		Ip:              router.config.Ip,
		TraceKey:        router.config.TraceKey,
		Registry:        router.registry,
		Reporter:        router.varz,
		Logger:          access_log.CreateRunningAccessLogger(router.config),
	}
	router.proxy = proxy.NewProxy(args)

	var host string
	if router.config.Status.Port != 0 {
		host = fmt.Sprintf("%s:%d", router.config.Ip, router.config.Status.Port)
	}

	varz := &vcap.Varz{
		UniqueVarz: router.varz,
	}
	varz.LogCounts = log.Counter

	healthz := &vcap.Healthz{
		LockableObject: router.registry,
	}

	router.component = &vcap.VcapComponent{
		Type:        "Router",
		Index:       router.config.Index,
		Host:        host,
		Credentials: []string{router.config.Status.User, router.config.Status.Pass},
		Config:      router.config,
		Varz:        varz,
		Healthz:     healthz,
		InfoRoutes: map[string]json.Marshaler{
			"/routes": router.registry,
		},
	}

	vcap.StartComponent(router.component)

	return router
}
Ejemplo n.º 16
0
Archivo: main.go Proyecto: vito/auction
func main() {
	flag.Parse()

	if *natsAddrs == "" {
		panic("need either nats addr")
	}

	client := yagnats.NewClient()

	clusterInfo := &yagnats.ConnectionCluster{}

	for _, addr := range strings.Split(*natsAddrs, ",") {
		clusterInfo.Members = append(clusterInfo.Members, &yagnats.ConnectionInfo{
			Addr: addr,
		})
	}

	err := client.Connect(clusterInfo)

	if err != nil {
		log.Fatalln("no nats:", err)
	}

	semaphore := make(chan bool, *maxConcurrent)

	repclient := repnatsclient.New(client, *timeout)

	client.SubscribeWithQueue("diego.auction", "auction-channel", func(msg *yagnats.Message) {
		semaphore <- true
		defer func() {
			<-semaphore
		}()

		var auctionRequest types.AuctionRequest
		err := json.Unmarshal(msg.Payload, &auctionRequest)
		if err != nil {
			client.Publish(msg.ReplyTo, errorResponse)
			return
		}

		auctionResult := auctioneer.Auction(repclient, auctionRequest)
		payload, _ := json.Marshal(auctionResult)

		client.Publish(msg.ReplyTo, payload)
	})

	fmt.Println("auctioneering")

	select {}
}
Ejemplo n.º 17
0
func newMessageBus(c *config.Config) (yagnats.NATSClient, error) {
	natsClient := yagnats.NewClient()
	natsMembers := []yagnats.ConnectionProvider{}

	for _, info := range c.Nats {
		natsMembers = append(natsMembers, &yagnats.ConnectionInfo{
			Addr:     fmt.Sprintf("%s:%d", info.Host, info.Port),
			Username: info.User,
			Password: info.Pass,
		})
	}

	err := natsClient.Connect(&yagnats.ConnectionCluster{
		Members: natsMembers,
	})

	return natsClient, err
}
Ejemplo n.º 18
0
func main() {
	cfg := flag.String("c", "cfg.json", "configuration file")
	flag.Parse()
	err := config.ParseConfig(*cfg)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	agent := agent.New("Huawei", 3, yagnats.NewClient())

	err = agent.Setup()
	if err != nil {
		fmt.Println(err.Error())
	}

	agent.Hearbeat()
}
Ejemplo n.º 19
0
func main() {
	flag.Parse()

	store := etcd.NewClient(strings.Split(*etcdCluster, ","))

	nats := yagnats.NewClient()

	natsInfo := &yagnats.ConnectionInfo{
		Addr:     *natsAddr,
		Username: *natsUser,
		Password: *natsPass,
	}

	err := nats.Connect(natsInfo)
	if err != nil {
		log.Fatalln(err)
	}

	node := executor.NewNode(store, time.Duration(*heartbeatInterval)*time.Second)

	starter := starter.NewStarter(node)

	go hero.SaveLives(store, starter)

	_, err = nats.Subscribe("app.start", func(msg *yagnats.Message) {
		var startMsg messages.AppStart
		err := json.Unmarshal([]byte(msg.Payload), &startMsg)
		if err != nil {
			log.Println("failed to unmarshal", msg.Payload)
			return
		}

		starter.Start(startMsg.Guid, startMsg.Index)
	})

	if err != nil {
		log.Fatalln(err)
	}

	for {
		time.Sleep(1 * time.Second)
		node.LogRegistry()
	}
}
Ejemplo n.º 20
0
func (runner *NATSRunner) Start() {
	_, err := exec.LookPath("gnatsd")
	if err != nil {
		fmt.Println("You need gnatsd installed!")
		os.Exit(1)
	}

	runner.natsCommand = exec.Command("gnatsd", "-p", strconv.Itoa(runner.port))
	err = runner.natsCommand.Start()
	Ω(err).ShouldNot(HaveOccurred(), "Make sure to have gnatsd on your path")

	connectionInfo := &yagnats.ConnectionInfo{
		Addr: fmt.Sprintf("127.0.0.1:%d", runner.port),
	}

	messageBus := yagnats.NewClient()

	Eventually(func() error {
		return messageBus.Connect(connectionInfo)
	}, 5, 0.1).ShouldNot(HaveOccurred())

	runner.MessageBus = messageBus
}
Ejemplo n.º 21
0
func main() {
	flag.Parse()

	nats := yagnats.NewClient()

	natsInfo := &yagnats.ConnectionInfo{
		Addr:     *natsAddr,
		Username: *natsUser,
		Password: *natsPass,
	}

	err := nats.Connect(natsInfo)
	if err != nil {
		log.Fatalln(err)
	}

	if *app == "" {
		guid, err := uuid.NewV4()
		if err != nil {
			log.Fatalln(err)
		}

		*app = guid.String()
	}

	store := etcd.NewClient(strings.Split(*etcdCluster, ","))

	start := time.Now()

	existingApps := 0
	allApps, err := store.Get(fmt.Sprintf("/apps/%s", *app), false, false)
	if err == nil {
		existingApps = allApps.Node.Nodes.Len()
	}

	go func() {
		for i := existingApps; i < *instances; i++ {
			start := messages.AppStart{
				Guid:  *app,
				Index: i,
			}

			msg, err := json.Marshal(start)
			if err != nil {
				log.Fatalln(err)
			}

			nats.Publish("app.start", msg)
		}
	}()

	for {
		res, err := store.Get(fmt.Sprintf("/apps/%s", *app), false, false)
		if err != nil {
			log.Println(err)
			continue
		}

		log.Println("entries:", res.Node.Nodes.Len())

		time.Sleep(1 * time.Second)

		if res.Node.Nodes.Len() == *instances {
			break
		}
	}

	log.Println("took:", time.Since(start))
}
	BeforeEach(func() {
		settingsService = &fakesettings.FakeSettingsService{}
		logger = boshlog.NewLogger(boshlog.LevelNone)
		platform = fakeplatform.NewFakePlatform()
		dirProvider = boshdir.NewProvider("/var/vcap")
		provider = NewHandlerProvider(settingsService, logger)
	})

	Describe("Get", func() {
		It("returns nats handler", func() {
			settingsService.Settings.Mbus = "nats://lol"
			handler, err := provider.Get(platform, dirProvider)
			Expect(err).ToNot(HaveOccurred())

			// yagnats.NewClient returns new object every time
			expectedHandler := NewNatsHandler(settingsService, yagnats.NewClient(), logger)
			Expect(reflect.TypeOf(handler)).To(Equal(reflect.TypeOf(expectedHandler)))
		})

		It("returns https handler", func() {
			url, err := gourl.Parse("https://lol")
			Expect(err).ToNot(HaveOccurred())

			settingsService.Settings.Mbus = "https://lol"
			handler, err := provider.Get(platform, dirProvider)
			Expect(err).ToNot(HaveOccurred())
			Expect(handler).To(Equal(micro.NewHTTPSHandler(url, logger, platform.GetFs(), dirProvider)))
		})

		It("returns an error if not supported", func() {
			settingsService.Settings.Mbus = "unknown-scheme://lol"
Ejemplo n.º 23
0
func main() {
	fmt.Println("Waiting for connections...")

	sea := taskmaster.NewSEA("/tmp/warden.sock")

	nats := yagnats.NewClient()

	nats.Connect(&yagnats.ConnectionInfo{
		Addr: "127.0.0.1:4222",
	})

	nats.Subscribe("ssh.start", func(msg *yagnats.Message) {
		var start sshStartMessage

		err := json.Unmarshal([]byte(msg.Payload), &start)
		if err != nil {
			fmt.Println("Error unmarshalling:", err)
			return
		}

		err = sea.Start(start.Session, start.PublicKey)
		if err != nil {
			fmt.Println("Failed to start SSH session:", err)
			return
		}
	})

	nats.Subscribe("ssh.stop", func(msg *yagnats.Message) {
		var stop sshStopMessage

		err := json.Unmarshal([]byte(msg.Payload), &stop)
		if err != nil {
			fmt.Println("Error unmarshalling:", err)
			return
		}

		err = sea.Stop(stop.Session)
		if err != nil {
			fmt.Println("Tailed to stop SSH session:", err)
			return
		}
	})

	//go func() {
	//for {
	//for id, session := range sessions {
	//fmt.Println(id, session.Port)
	//}

	//time.Sleep(1 * time.Second)
	//}
	//}()

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	<-c

	//for _, session := range sessions {
	//session.Container.Destroy()
	//}
}
	healthCheckerConfig := HealthCheckerConf{
		Name:     "a_useful_health_checker",
		Interval: 1,
	}

	config = Config{
		[]MessageBusServer{messageBusServer, messageBusServer}, // doesn't matter if these are the same, just want to send a slice
		"riakcs.vcap.me",
		"127.0.0.1",
		8080,
		&healthCheckerConfig,
	}

	BeforeEach(func() {
		logger = lagertest.NewTestLogger("Registrar test")
		testSpyClient = yagnats.NewClient()
		connectionInfo := yagnats.ConnectionInfo{
			messageBusServer.Host,
			messageBusServer.User,
			messageBusServer.Password,
			nil,
		}

		err := testSpyClient.Connect(&connectionInfo)
		Expect(err).NotTo(HaveOccurred())
	})

	AfterEach(func() {
		testSpyClient.Disconnect()
	})
Ejemplo n.º 25
0
func Start(natsAddrs []string, rep *representative.Representative) {
	client := yagnats.NewClient()

	clusterInfo := &yagnats.ConnectionCluster{}

	for _, addr := range natsAddrs {
		clusterInfo.Members = append(clusterInfo.Members, &yagnats.ConnectionInfo{
			Addr: addr,
		})
	}

	err := client.Connect(clusterInfo)
	if err != nil {
		log.Fatalln("no nats:", err)
	}

	guid := rep.Guid()

	client.Subscribe(guid+".guid", func(msg *yagnats.Message) {
		jguid, _ := json.Marshal(rep.Guid())
		client.Publish(msg.ReplyTo, jguid)
	})

	client.Subscribe(guid+".total_resources", func(msg *yagnats.Message) {
		jresources, _ := json.Marshal(rep.TotalResources())
		client.Publish(msg.ReplyTo, jresources)
	})

	client.Subscribe(guid+".reset", func(msg *yagnats.Message) {
		rep.Reset()
		client.Publish(msg.ReplyTo, successResponse)
	})

	client.Subscribe(guid+".set_instances", func(msg *yagnats.Message) {
		var instances []instance.Instance

		err := json.Unmarshal(msg.Payload, &instances)
		if err != nil {
			client.Publish(msg.ReplyTo, errorResponse)
		}

		rep.SetInstances(instances)
		client.Publish(msg.ReplyTo, successResponse)
	})

	client.Subscribe(guid+".instances", func(msg *yagnats.Message) {
		jinstances, _ := json.Marshal(rep.Instances())
		client.Publish(msg.ReplyTo, jinstances)
	})

	client.Subscribe(guid+".vote", func(msg *yagnats.Message) {
		var inst instance.Instance

		err := json.Unmarshal(msg.Payload, &inst)
		if err != nil {
			panic(err)
		}

		response := types.VoteResult{
			Rep: guid,
		}

		defer func() {
			payload, _ := json.Marshal(response)
			client.Publish(msg.ReplyTo, payload)
		}()

		score, err := rep.Vote(inst)
		if err != nil {
			// log.Println(guid, "failed to vote:", err)
			response.Error = err.Error()
			return
		}

		response.Score = score
	})

	client.Subscribe(guid+".reserve_and_recast_vote", func(msg *yagnats.Message) {
		var inst instance.Instance

		responsePayload := errorResponse
		defer func() {
			client.Publish(msg.ReplyTo, responsePayload)
		}()

		err := json.Unmarshal(msg.Payload, &inst)
		if err != nil {
			// log.Println(guid, "invalid reserve_and_recast_vote request:", err)
			return
		}

		score, err := rep.ReserveAndRecastVote(inst)
		if err != nil {
			// log.Println(guid, "failed to reserve_and_recast_vote:", err)
			return
		}

		responsePayload, _ = json.Marshal(score)
	})

	client.Subscribe(guid+".release", func(msg *yagnats.Message) {
		var inst instance.Instance

		responsePayload := errorResponse
		defer func() {
			client.Publish(msg.ReplyTo, responsePayload)
		}()

		err := json.Unmarshal(msg.Payload, &inst)
		if err != nil {
			log.Println(guid, "invalid reserve_and_recast_vote request:", err)
			return
		}

		rep.Release(inst)

		responsePayload = successResponse
	})

	client.Subscribe(guid+".claim", func(msg *yagnats.Message) {
		var inst instance.Instance

		responsePayload := errorResponse
		defer func() {
			client.Publish(msg.ReplyTo, responsePayload)
		}()

		err := json.Unmarshal(msg.Payload, &inst)
		if err != nil {
			log.Println(guid, "invalid reserve_and_recast_vote request:", err)
			return
		}

		rep.Claim(inst)

		responsePayload = successResponse
	})

	fmt.Printf("[%s] listening for nats\n", guid)

	select {}
}
Ejemplo n.º 26
0
func main() {
	c := config.DefaultConfig()
	logCounter := vcap.NewLogCounter()
	InitLoggerFromConfig(c, logCounter)

	if configFile != "" {
		c = config.InitConfigFromFile(configFile)
	}

	// setup number of procs
	if c.GoMaxProcs != 0 {
		runtime.GOMAXPROCS(c.GoMaxProcs)
	}

	InitLoggerFromConfig(c, logCounter)
	logger := steno.NewLogger("router.main")

	natsClient := yagnats.NewClient()
	natsMembers := []yagnats.ConnectionProvider{}

	for _, info := range c.Nats {
		natsMembers = append(natsMembers, &yagnats.ConnectionInfo{
			Addr:     fmt.Sprintf("%s:%d", info.Host, info.Port),
			Username: info.User,
			Password: info.Pass,
		})
	}

	err := natsClient.Connect(&yagnats.ConnectionCluster{
		Members: natsMembers,
	})

	if err != nil {
		logger.Fatalf("Error connecting to NATS: %s\n", err)
	}

	registry := rregistry.NewRouteRegistry(c, natsClient)

	varz := rvarz.NewVarz(registry)

	accessLogger, err := access_log.CreateRunningAccessLogger(c)
	if err != nil {
		logger.Fatalf("Error creating access logger: %s\n", err)
	}

	args := proxy.ProxyArgs{
		EndpointTimeout: c.EndpointTimeout,
		Ip:              c.Ip,
		TraceKey:        c.TraceKey,
		Registry:        registry,
		Reporter:        varz,
		AccessLogger:    accessLogger,
	}
	p := proxy.NewProxy(args)

	router, err := router.NewRouter(c, p, natsClient, registry, varz, logCounter)
	if err != nil {
		logger.Errorf("An error occurred: %s", err.Error())
		os.Exit(1)
	}

	signals := make(chan os.Signal, 1)
	signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT, syscall.SIGUSR1)

	errChan := router.Run()

	logger.Info("gorouter.started")

	select {
	case err := <-errChan:
		if err != nil {
			logger.Errorf("Error occurred: %s", err.Error())
			os.Exit(1)
		}
	case sig := <-signals:
		go func() {
			for sig := range signals {
				logger.Infod(
					map[string]interface{}{
						"signal": sig.String(),
					},
					"gorouter.signal.ignored",
				)
			}
		}()

		if sig == syscall.SIGUSR1 {
			logger.Infod(
				map[string]interface{}{
					"timeout": (c.DrainTimeout).String(),
				},
				"gorouter.draining",
			)

			router.Drain(c.DrainTimeout)
		}

		stoppingAt := time.Now()

		logger.Info("gorouter.stopping")

		router.Stop()

		logger.Infod(
			map[string]interface{}{
				"took": time.Since(stoppingAt).String(),
			},
			"gorouter.stopped",
		)
	}

	os.Exit(0)
}
Ejemplo n.º 27
0
func main() {
	flag.Parse()

	runtime.GOMAXPROCS(runtime.NumCPU())

	//make the out dir
	logger.Component = "SIMULATOR"
	if outDir == "" {
		logger.Fatal("out.dir.unspecified")
	}
	err := os.MkdirAll(outDir, 0777)
	if err != nil {
		logger.Fatal("out.dir.creation.failed", err)
	}

	//set up logging
	outputFile, err := os.Create(filepath.Join(outDir, "simulator.log"))
	if err != nil {
		logger.Fatal("failed.to.create.simulator.log", err)
	}

	logger.Writer = io.MultiWriter(os.Stdout, outputFile)

	cleanup.Register(func() {
		outputFile.Sync()
	})

	//start etcd
	natsClient := yagnats.NewClient()

	natsMembers := []yagnats.ConnectionProvider{}

	for _, addr := range strings.Split(*natsAddresses, ",") {
		natsMembers = append(
			natsMembers,
			&yagnats.ConnectionInfo{addr, *natsUsername, *natsPassword},
		)
	}

	natsInfo := &yagnats.ConnectionCluster{Members: natsMembers}

	err = natsClient.Connect(natsInfo)
	if err != nil {
		logger.Fatal("could not connect to nats:", err)
	}

	logger.Component = "simulator"

	etcdAdapter := etcdstoreadapter.NewETCDStoreAdapter(
		strings.Split(*etcdCluster, ","),
		workerpool.NewWorkerPool(10),
	)

	err = etcdAdapter.Connect()
	if err != nil {
		logger.Fatal("etcd.connect-failed", map[string]interface{}{
			"error": err.Error(),
		})
	}

	//write info to the output dir
	writeInfo()

	//monitor etcd
	monitorETCD(etcdAdapter)

	//run the simulator
	runSimulation(natsClient)

	cleanup.Exit(0)
}
Ejemplo n.º 28
0
	natsAddrs = []string{
		"127.0.0.1:20011",
		"127.0.0.1:20021",
	}

	numReps = len(guids)
	repResources = 100

	fmt.Printf("Running in %s auctioneerMode\n", auctioneerMode)

	//parse flags to set up rules
	timeout = 500 * time.Millisecond

	rules = auctioneer.DefaultRules

	natsClient = yagnats.NewClient()
	clusterInfo := &yagnats.ConnectionCluster{}

	for _, addr := range natsAddrs {
		clusterInfo.Members = append(clusterInfo.Members, &yagnats.ConnectionInfo{
			Addr: addr,
		})
	}

	err := natsClient.Connect(clusterInfo)
	Ω(err).ShouldNot(HaveOccurred())

	client = repnatsclient.New(natsClient, timeout)

	if auctioneerMode == "inprocess" {
		communicator = func(auctionRequest types.AuctionRequest) types.AuctionResult {
Ejemplo n.º 29
0
func main() {
	var err error

	runtime.GOMAXPROCS(runtime.NumCPU())

	rand.Seed(time.Now().UnixNano())

	flag.Parse()

	executorUUID, err := uuid.NewV4()
	if err != nil {
		log.Fatalln("could not generate guid:", err)
	}

	executorID = executorUUID.String()

	cleanup.Register(func() {
		once.Do(func() {
			logger.Info("shutting-down", map[string]interface{}{})
			close(stop)
			tasks.Wait()
			logger.Info("shutdown", map[string]interface{}{})
		})
	})

	natsClient := yagnats.NewClient()

	natsMembers := []yagnats.ConnectionProvider{}

	for _, addr := range strings.Split(*natsAddresses, ",") {
		natsMembers = append(
			natsMembers,
			&yagnats.ConnectionInfo{addr, *natsUsername, *natsPassword},
		)
	}

	natsInfo := &yagnats.ConnectionCluster{Members: natsMembers}

	err = logger.Connect(natsInfo)
	if err != nil {
		log.Fatalln("could not connect logger:", err)
	}

	err = natsClient.Connect(natsInfo)
	if err != nil {
		log.Fatalln("could not connect to nats:", err)
	}

	logger.Component = fmt.Sprintf("executor.%s", executorID)

	etcdAdapter := etcdstoreadapter.NewETCDStoreAdapter(
		strings.Split(*etcdCluster, ","),
		workerpool.NewWorkerPool(10),
	)
	err = etcdAdapter.Connect()
	if err != nil {
		logger.Fatal("etcd.connect-failed", map[string]interface{}{
			"error": err.Error(),
		})
	}

	bbs := bbs.New(bbs.NewHurlerKicker(*hurlerAddress), etcdAdapter, timeprovider.NewTimeProvider())

	ready := make(chan bool, 1)

	err = maintainPresence(bbs, ready)
	if err != nil {
		logger.Fatal("initializing-presence", map[string]interface{}{
			"error": err.Error(),
		})
	}

	err = registerHandler(etcdAdapter, *listenAddr, ready)
	if err != nil {
		logger.Fatal("initializing-route", map[string]interface{}{
			"error": err.Error(),
		})
	}

	go handleTasks(bbs, *listenAddr)
	go convergeTasks(bbs)

	<-ready
	<-ready

	logger.Info("up", map[string]interface{}{
		"executor": executorID,
	})

	select {}
}