Esempio n. 1
0
func (s *WebServer) HandleNSQLogLevel(w http.ResponseWriter, r *http.Request) {
	level := r.URL.Query().Get("level")
	s.nc.Lock()
	defer s.nc.Unlock()

	if len(level) < 1 {
		info := fmt.Sprintf("NSQ Consumer log level is \"%s\"\n", strings.ToUpper(s.nc.loglevel))
		writeResponseWithErr(w, info)
		return
	}

	loglevel, err := config.GetNSQLogLevel(strings.ToLower(level))
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	//log.Println()
	s.nc.consumer.SetLogger(logger, loglevel)
	s.nc.loglevel = level

	info := fmt.Sprintf("Set NSQ Consumer log level to \"%s\"\n", strings.ToUpper(level))
	log.Println(info)
	writeResponseWithErr(w, info)
}
Esempio n. 2
0
// InitHubWithConfig create *Hub struct based on config and default values
func (h *Hub) InitWithConfig(cfg config.TomlConfig) {
	var (
		err       error
		errorsCnt int
	)

	connections := make(map[string]*apns.GatewayClient)
	for nick, appCfg := range cfg.APNSapp {
		_ = nick
		gateway := apns.ProductionGateway
		if appCfg.Sandbox {
			gateway = apns.SandboxGateway
		}
		//testAPNS(name, addr, open, private string) (client *GatewayClient, err error) {
		client := apns.NewGatewayClient(appCfg.Name, gateway, appCfg.KeyOpen, appCfg.KeyPrivate)
		clientLog := log15.New("host", h.logctx.hostname, "app", appCfg.Name)
		//clientLog.SetHandler(log15.LvlFilterHandler(h.logctx.logLvl, h.logctx.handler))
		clientLog.SetHandler(h.logctx.handler)

		client.L = clientLog

		connections[appCfg.Name] = client
		err = testAPNS(client)
		hubStat.Set(appCfg.Name, client.Stat)
		if err != nil {
			h.L.Error("APNS client test failed"+err.Error(), "app", appCfg.Name)
			errorsCnt++
			continue
		}
		clientLog.Info("connection OK")
	}
	if *onlyTestAPNS {
		os.Exit(errorsCnt)
	}

	// TODO: move source ini to separate func
	concurrency := cfg.NSQ.Concurrency
	if concurrency <= 0 {
		concurrency = 100 // FIXME: move to const
	}
	h.L.Debug("set Nsq consumer concurrency", "n", concurrency)

	source := &NSQSource{
		Topic:       cfg.NSQ.Topic,
		Channel:     cfg.NSQ.Channel,
		LookupAddrs: cfg.NSQ.LookupAddrs,
		NsqdAddrs:   cfg.NSQ.NsqdAddrs,
		MaxInFlight: cfg.NSQ.MaxInFlight,
		Concurrency: cfg.NSQ.Concurrency,
	}

	if len(cfg.NSQ.LogLevel) < 1 {
		cfg.NSQ.LogLevel = "info" // FIXME: move to const
	}
	source.LogLevel, err = config.GetNSQLogLevel(cfg.NSQ.LogLevel)
	if err != nil {
		LogAndDieShort(h.L, err)
	}
	h.L.Debug("Init Nsq producer", "config", spew.Sdump(&source))

	h.Consumers = connections
	h.Producer = source
	h.MessagesStat = &HubMessagesStat{}

	h.Config = &cfg
	h.ProducerTTL = parseTTLtoSeconds(cfg.NSQ.TTL)
	h.ConsumerTTL = parseTTLtoSeconds(cfg.APNS.TTL)
}