Ejemplo n.º 1
0
func reloadConfig(filename string, rls ...Reloadable) bool {
	log.Infof("Loading configuration file %s", filename)

	cfg, err := config.LoadConfig(filename)
	if err != nil {
		log.Errorf("Failed to load configuration file (-config.file=%s): %v", filename, err)
		return false
	}

	for _, rl := range rls {
		rl.ApplyConfig(cfg)
	}

	return true
}
Ejemplo n.º 2
0
func (tm *TaskManager) Run() {
	log.Info("Starting task manager...")

	identities := map[string]struct{}{}

	for taskCfg, task := range tm.tasks {
		log.Infof("taskCfg = %v", taskCfg)
		ch := make(chan *config.UnitConfig)
		// Every task has a batch of units.
		go tm.handleUnitUpdates(taskCfg, ch)

		for _, id := range task.Identity() {
			id = fullId(taskCfg, id)
			identities[id] = struct{}{}
		}

		defer func(t Task, c chan *config.UnitConfig) {
			go t.Run(c)
		}(task, ch)
	}

	log.Info("before removeUnits")

	tm.m.Lock()
	defer tm.m.Unlock()

	tm.removeUnits(func(id string) bool {
		if _, ok := identities[id]; ok {
			return false
		}
		return true
	})

	tm.running = true
	log.Info("Run finished.")
}
Ejemplo n.º 3
0
func main() {
	flag.Parse()

	if *versionFlag {
		fmt.Println(VERSION)
		os.Exit(0)
	}

	if *logstashFlag {
		log.ChangeToLogstashFormater(APP_NAME)
	}
	log.SetLogFile(LOG_FILE)

	if *reloadFlag {
		notifyReload()
		os.Exit(0)
	}

	pid := os.Getpid() // This process's pid.
	log.Infof("The process id is %d", pid)
	// Save the pid into the pid file.
	err := utils.WriteFile(PID_FILE, []byte(strconv.Itoa(pid)), 0644)
	if err != nil {
		log.Fatalln("Error writing this process id:", err)
	}
	defer os.Remove(PID_FILE)

	publisher := outputs.NewPublisherType()

	taskManager := fetch.NewTaskManager(publisher)

	if !reloadConfig(*cfgFileFlag, publisher, taskManager) {
		os.Exit(1)
	}

	// Wait for receive a singal to reload configuration file.
	hupCh := make(chan os.Signal)
	hupReady := make(chan bool)
	signal.Notify(hupCh, syscall.SIGHUP)
	go func() {
		<-hupReady
		for range hupCh {
			reloadConfig(*cfgFileFlag, publisher, taskManager)
		}
	}()

	go taskManager.Run()
	defer taskManager.Stop()

	defer publisher.StopPublish()

	close(hupReady)

	// Wait for quit signal to exit this process.
	sigCh := make(chan os.Signal)
	signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM, os.Kill, syscall.SIGKILL)
	select {
	case s := <-sigCh: // Block until a signal is received.
		log.Warnf("Caught Signal: %v, shuting down gracefully...", s)
	}
	close(hupCh)

	log.Info("See you again!")
	return
}