Beispiel #1
0
func bananas(addrs []string, lock chan bool) {
	cfg := nsq.NewConfig()
	cfg.UserAgent = fmt.Sprintf("redis-nom/0.1 go-nsq/%s", nsq.VERSION)

	producers = make(map[string]*nsq.Producer)
	for _, addr := range addrs {
		if producer, err := nsq.NewProducer(addr, cfg); err == nil {
			producers[addr] = producer
		} else {
			l2n.Printf("Failed to connect to NSQ @ %s", addr)
		}
	}

	if len(producers) == 0 {
		l2n.Println("No NSQ available")
		return
	}

	l2n.Println("NSQ Producer is on the go!")

	<-lock

	for _, producer := range producers {
		producer.Stop()
	}
}
Beispiel #2
0
func main() {
	log.Println("Starting Redis NOM NOM NOM...")

	if err := loadConfiguration(); err != nil {
		l2n.Printf("%v", err)
		os.Exit(-1)
	}

	logger2Nsq := l2n.NewLog2Nsq(&l2n.Options{
		AppName: "redis-nom",
		Addr:    cfg.Nsq.Address[0],
	})

	l2n.Println("Configuration loaded")
	l2n.Printf("%+v", cfg)

	commons.HoldMeTillICantGetEnough(cfg.Cache.Mastername, cfg.Cache.Sentinel, cfg.General.Env)

	if err := getSentinelClient(); err != nil {
		l2n.Printf("%v", err)
		os.Exit(-1)
	}

	if masterPodList = retrievePodList(); masterPodList == nil {
		l2n.Println("Failed to retrieve pod list")
		os.Exit(-1)
	}

	l2n.Printf("%+v", masterPodList)

	nsqLock := make(chan bool, 1)

	dispatcher.InitPagerDutyInterface(cfg.PagerDuty.Key)
	dispatcher.StartNSQProducer(cfg.Nsq.Topic, cfg.Nsq.Address, nsqLock)
	monitor.StartSentinelMonitoring(cfg.Redis.Sentinel)
	monitor.StartRedisMonitoring(cfg.Redis.Sentinel)

	lock := make(chan bool, 1)
	sigRcv := make(chan os.Signal, 1)
	signal.Notify(sigRcv, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)

	// Routine to catch and handle signals
	go func(sigRcv chan os.Signal) {
		for sig := range sigRcv {
			l2n.Printf("Signal caught: %s", sig.String())
			// TODO Clean all
			nsqLock <- true
			lock <- true
		}
	}(sigRcv)

	<-lock

	logger2Nsq.Close()
}
Beispiel #3
0
func iWillSurvive(env string) {
	l2n.Println("[HOLD] First I was afraid, then I was petrified...")
	for {
		cache.Set(env, "I Will Survive!", 4*time.Second)
		time.Sleep(3 * time.Second)
	}
}
Beispiel #4
0
// HoldMeTillICantGetEnough will hold the application here, until he finds out he was left out alone to do all the work
func HoldMeTillICantGetEnough(mastername string, sentinel []string, env string) {
	cache = redis.NewFailoverClient(&redis.FailoverOptions{
		MasterName:    mastername,
		SentinelAddrs: sentinel,
	})

	if _, err := cache.Ping().Result(); err != nil {
		l2n.Printf("[CACHE] %s", err.Error())
		os.Exit(1)
	}

	monkeyOutThere := false
	for cache.Get(env).Err() == nil {
		if !monkeyOutThere {
			l2n.Println("[HOLD] There's a monkey doing monkey's work...")
			monkeyOutThere = true
		}
		time.Sleep(5 * time.Second)
	}

	go iWillSurvive(env)
	go logImAlive()
}
Beispiel #5
0
func logImAlive() {
	for {
		time.Sleep(time.Minute)
		l2n.Println("[HOLD] STOP! Don't kill me, I'm listening quietly...")
	}
}