Ejemplo n.º 1
0
// Consumes messages from NSQ and publishes to ElasticSearch
func NSQToES() error {
	nc, err := nsq.NewConsumer(Topic, Channel, nsq.NewConfig())
	if err != nil {
		return err
	}
	tn := &nsqToES{
		conn: el.NewConn(),
	}
	tn.conn.Domain = ESAddress

	nc.AddConcurrentHandlers(tn, NumHandlers)
	return nc.ConnectToNSQD(NSQAddress)
}
Ejemplo n.º 2
0
// RunWithHandler configure and start NSQ handler
func (s *NSQSource) RunWithHandler(h nsq.Handler) *nsq.Consumer {
	var (
		err      error
		consumer *nsq.Consumer
	)
	hub, _ := h.(*Hub)
	// https://godoc.org/github.com/nsqio/go-nsq#Config
	cfg := nsq.NewConfig()

	cfg.UserAgent = fmt.Sprintf("mpush-apns-agent/%s", VERSION)
	cfg.DefaultRequeueDelay = time.Second * 5
	if s.MaxInFlight > 0 {
		cfg.MaxInFlight = s.MaxInFlight
	}

	consumer, err = nsq.NewConsumer(s.Topic, s.Channel, cfg)
	if err != nil {
		LogAndDie(hub.L, "NSQ consumer creation error", err, []interface{}{})
	}

	consumer.SetLogger(logger, s.LogLevel)

	// set NSQ handler
	consumer.AddConcurrentHandlers(h, s.Concurrency)
	hub.L.Debug("Add *hub to NSQ handlers", "config", spew.Sdump(cfg))

	var addrs []string
	if s.LookupAddrs != nil {
		addrs = s.LookupAddrs
		err = consumer.ConnectToNSQLookupds(addrs)
	} else if s.NsqdAddrs != nil {
		addrs = s.NsqdAddrs
		err = consumer.ConnectToNSQDs(addrs)
	} else {
		err = errors.New("You should set at least one nsqd or nsqlookupd address")
	}

	logCtx := []interface{}{"addrs", addrs}
	if err != nil {
		LogAndDie(hub.L, "NSQ connection error", err, logCtx)
	}
	hub.L.Debug("NSQ connected to", logCtx...)

	return consumer
}