func notifyReload() { pid_b, err := utils.ReadFile(PID_FILE) if err != nil { log.Fatalln("Error reading the file of this process id:", err) } pid_s := string(pid_b) log.Debug("This process id is ", pid_s) if strings.TrimSpace(pid_s) == "" { log.Fatalln("The file of this process id is empty.") } pid, err := strconv.Atoi(pid_s) if err != nil { log.Fatalf("String to int error: %s", err) } _, err = utils.ExecCommand(false, "kill", "-HUP", pid_s) if err != nil { log.Fatalf("Execute commmand <kill -HUP %s> error: %s", pid_s, err) } err = syscall.Kill(pid, syscall.SYS_READ) if err != nil { log.Fatalf("Kill signal send failed. Pid: %s, error: %s", pid_s, err) } else { log.Info("Configuration file is reloading...") } }
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 }