Exemplo n.º 1
0
Arquivo: main.go Projeto: houcy/push
func main() {
	var (
		configFile    = flag.String("c", "./conf/conf.json", "Config file")
		logConfigFile = flag.String("l", "./conf/log.xml", "Log config file")
	)

	flag.Parse()

	err := conf.LoadConfig(*configFile)
	if err != nil {
		fmt.Printf("LoadConfig (%s) failed: (%s)\n", *configFile, err)
		os.Exit(1)
	}

	err = log.RegisterCustomFormatter("Ms", utils.CreateMsFormatter)
	if err != nil {
		fmt.Printf("Failed to create custom formatter: (%s)\n", err)
		os.Exit(1)
	}

	logger, err := log.LoggerFromConfigAsFile(*logConfigFile)
	if err != nil {
		fmt.Printf("Load log config failed: (%s)\n", err)
		os.Exit(1)
	}

	log.ReplaceLogger(logger)

	storage.NewInstance(&conf.Config)
	auth.NewInstance(&conf.Config)

	setMsgID()
	setPappID()
	mqProducer, err := mq.NewProducer(
		conf.Config.Rabbit.Uri,
		conf.Config.Rabbit.Exchange,
		conf.Config.Rabbit.ExchangeType,
		conf.Config.Rabbit.Key,
		false)
	if err != nil {
		log.Warnf("new mq produccer failed: %s", err)
		os.Exit(1)
	}
	err = zk.InitWatcher(
		conf.Config.ZooKeeper.Addr,
		conf.Config.ZooKeeper.Timeout*time.Second,
		conf.Config.ZooKeeper.Path)
	if err != nil {
		log.Warnf("init zk watcher failed: %s", err)
		os.Exit(1)
	}
	rpcClient, err = mq.NewRpcClient(conf.Config.Rabbit.Uri, "gibbon_rpc_exchange")
	if err != nil {
		log.Criticalf("Create RPC client failed: %s", err)
		os.Exit(1)
	}

	wg := &sync.WaitGroup{}
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
	go func() {
		sig := <-c
		log.Infof("Received signal '%v', exiting\n", sig)
		close(msgBox)
		mqProducer.Shutdown()
		rpcClient.Close()
		wg.Done()
	}()

	wg.Add(1)
	go startHttp(conf.Config.PushAPI, conf.Config.Control.CommandTimeout)

	for {
		select {
		case m, ok := <-msgBox:
			if !ok {
				os.Exit(0)
			}

			v, err := json.Marshal(m)
			if err != nil {
				log.Infof("failed to encode with Msg:", err)
				continue
			}

			if _, err := storage.Instance.HashSet(
				"db_msg_"+m.AppId,
				strconv.FormatInt(m.MsgId, 10), v); err != nil {
				log.Infof("failed to put Msg into redis:", err)
				continue
			}

			if m.Options.TTL > 0 {
				_, err = storage.Instance.HashSet(
					"db_offline_msg_"+m.AppId,
					fmt.Sprintf("%v_%v_%v", m.MsgId, m.CTime, m.Options.TTL+m.CTime), v)
				if err != nil {
					log.Infof("failed to put offline Msg into redis:", err)
					continue
				}
			}

			d := map[string]interface{}{
				"appid": m.AppId,
				"msgid": m.MsgId,
			}
			data, err := json.Marshal(d)
			if err != nil {
				log.Infof("failed to jsonencode with data:", err)
				continue
			}

			if err := mqProducer.Publish(data); err != nil {
				log.Infof("failed to publish data:", err)
				continue
			}
		}
	}
	wg.Wait()
}
func init() {
	// Errors are omitted here for simplicity. Do not omit them in production.
	_ = log.RegisterCustomFormatter("CustomSyslogHeader", createSyslogHeaderFormatter)
	_ = log.RegisterCustomFormatter("MyLevel", createMyLevelFormatter)
}
Exemplo n.º 3
0
func main() {
	var (
		configFile    = flag.String("c", "./conf/conf.json", "Config file")
		logConfigFile = flag.String("l", "./conf/log.xml", "Log config file")
	)
	flag.Parse()

	err := conf.LoadConfig(*configFile)
	if err != nil {
		fmt.Printf("LoadConfig (%s) failed: (%s)\n", *configFile, err)
		os.Exit(1)
	}

	err = log.RegisterCustomFormatter("Ms", utils.CreateMsFormatter)
	if err != nil {
		fmt.Printf("Failed to create custom formatter: (%s)\n", err)
		os.Exit(1)
	}

	logger, err := log.LoggerFromConfigAsFile(*logConfigFile)
	if err != nil {
		fmt.Printf("Load log config failed: (%s)\n", err)
		os.Exit(1)
	}

	log.ReplaceLogger(logger)

	wg := &sync.WaitGroup{}
	c := make(chan os.Signal, 1)
	ctrl := make(chan bool, 1)
	signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)

	wg.Add(1)
	go func() {
		sig := <-c
		log.Infof("Received signal '%v', exiting\n", sig)
		close(ctrl)
		wg.Done()
	}()

	go startHttp(conf.Config.Sync.Url)
	waitingMsgs := make(map[string]*SyncMsg)
	go func() {
		log.Infof("message handler routine start")
		wg.Add(1)
		for {
			select {
			case _ = <-ctrl:
				log.Infof("message handler routine stop")
				wg.Done()
				return
			//case msg, ok := <-msgBox:
			case msg := <-msgBox:
				key := fmt.Sprintf("%s_%s_%s_%s", msg.AppId, msg.UserId, msg.SendId, msg.RegId)
				if _, ok := waitingMsgs[key]; !ok {
					waitingMsgs[key] = msg
				}
			case <-time.After(conf.Config.Sync.Interval * time.Second):
				for _, msg := range waitingMsgs {
					push(msg)
				}
				waitingMsgs = map[string]*SyncMsg{}
			}
		}
	}()
	wg.Wait()
}