Exemplo n.º 1
0
func (c *client) listen(ch chan bool) {
	c.Lock()
	defer c.Unlock()

	// check if we started listening while locked
	if c.listening {
		ch <- true
		return
	}

	if deliveries, err := raven.Consume(c.instanceID); err != nil {
		log.Criticalf("[Client] Failed to consume: %v", err)
		c.listening = false
		ch <- false
	} else {
		log.Debugf("[Client] Listening on %s", c.instanceID)
		c.listening = true
		ch <- true
		c.Unlock()
		for d := range deliveries {
			log.Tracef("[Client] Inbound message %s on %s", d.CorrelationId, c.instanceID)
			go c.getResponse(d)
		}
		c.Lock()
		log.Errorf("[Client] Stopping listening due to channel closing")
		c.listening = false
	}
}
Exemplo n.º 2
0
func doRun(opts *Options) {
	defer cleanupLogs()

	// check we have some endpoints
	if reg.size() == 0 {
		log.Critical("There are no endpoints for this service")
		os.Exit(3)
	}

	// start listening for incoming messages
	deliveries, err := raven.Consume(InstanceID)
	if err != nil {
		log.Critical("[Server] Failed to consume: %v", err)
		os.Exit(5)
	}

	if opts.SelfBind {
		// binding should come after you've started consuming
		if err := raven.BindService(Name, InstanceID); err != nil {
			log.Criticalf("[Server] Failed to bind itself: %v", err)
			os.Exit(7)
		}
	}

	// announce ourselves to the discovery service
	dsc = newDiscovery(opts)
	go dsc.connect()

	// run some post connect handlers
	for _, f := range postConnHdlrs {
		go f()
	}

	// register stats collector
	go registerStats()

	// listen for SIGQUIT
	go signalCatcher()

	// consume messages
	for d := range deliveries {
		req := NewRequestFromDelivery(d)
		go HandleRequest(req)
	}

	log.Critical("[Server] Stopping due to channel closing")
	os.Exit(6)
}