Example #1
0
// Consumes messages from slack and publishes to NSQ
func ToNSQ() error {
	url, err := authenticate()
	if err != nil {
		return err
	}

	np, err := nsq.NewProducer(NSQAddress, nsq.NewConfig())
	if err != nil {
		return err
	}

	tn := &toNSQ{
		topic: Topic,
		p:     np,
	}

	for {
		c, _, err := websocket.DefaultDialer.Dial(url, nil)
		if err != nil {
			return err
		}
		if err := tn.process(c); err != nil {
			return err
		}
		c.Close()
	}
}
Example #2
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)
}
Example #3
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
}