Esempio n. 1
0
func init() {
	parseFlags()

	if options.showVersion {
		server.ShowVersionAndExit()
	}

	server.SetupLogging(options.logFile, options.logLevel,
		options.crashLogFile, options.alarmLogSock, options.alarmTag)

	// thrift lib use "log", so we also need to customize its behavior
	_log.SetFlags(_log.Ldate | _log.Ltime | _log.Lshortfile)

	if options.kill {
		s := server.NewServer("fae")
		s.LoadConfig(options.configFile)
		s.Launch()

		// stop new requests
		engine.NewEngine().
			LoadConfig(s.Conf).
			UnregisterEtcd()

		// finish all outstanding RPC sessions
		if err := signal.SignalProcessByPidFile(options.lockFile,
			syscall.SIGUSR1); err != nil {
			fmt.Fprintf(os.Stderr, "stop failed: %s\n", err)
			os.Exit(1)
		}

		cleanup() // TODO wait till that faed process terminates, who will do the cleanup

		fmt.Println("faed killed")

		os.Exit(0)
	}

	if options.lockFile != "" {
		if locking.InstanceLocked(options.lockFile) {
			fmt.Fprintf(os.Stderr, "Another instance is running, exit...\n")
			os.Exit(1)
		}

		locking.LockInstance(options.lockFile)
	}

}
Esempio n. 2
0
func main() {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println(err)
			debug.PrintStack()
		}
	}()

	if gateway.Options.Debug {
		log.SetFlags(log.LstdFlags | log.Llongfile) // TODO zk sdk uses this
		log.SetPrefix(color.Magenta("[log]"))
	} else {
		log.SetOutput(ioutil.Discard)
	}

	if gateway.Options.KillFile != "" {
		if err := signal.SignalProcessByPidFile(gateway.Options.KillFile, syscall.SIGUSR2); err != nil {
			panic(err)
		}

		fmt.Println("kateway killed")
		os.Exit(0)
	}

	if gateway.Options.GolangTrace {
		// go tool trace kateway xxx.pprof
		f, err := os.Create(time.Now().Format("2006-01-02T150405.pprof"))
		if err != nil {
			panic(err)
		}
		defer f.Close()

		if err = trace.Start(f); err != nil {
			panic(err)
		}
		defer trace.Stop()
	}

	fmt.Fprintln(os.Stderr, strings.TrimSpace(logo))

	if gateway.Options.PidFile != "" {
		pid := os.Getpid()
		if err := ioutil.WriteFile(gateway.Options.PidFile, []byte(fmt.Sprintf("%d", pid)), 0644); err != nil {
			panic(err)
		}
	}

	gateway.SetupLogging(gateway.Options.LogFile, gateway.Options.LogLevel, gateway.Options.CrashLogFile)

	// load config
	if gateway.Options.ConfigFile == "" {
		ctx.LoadFromHome()
	} else {
		_, err := os.Stat(gateway.Options.ConfigFile)
		if err != nil {
			panic(err)
		}
		ctx.LoadConfig(gateway.Options.ConfigFile)
	}

	gw := gateway.New(gateway.Options.Id)
	if err := gw.Start(); err != nil {
		glog.Error(err.Error())
		glog.Info("kateway[%s@%s] bye!", gafka.BuildId, gafka.BuiltAt)
		glog.Close()
		os.Exit(1)
	}

	t0 := time.Now()
	gw.ServeForever()
	glog.Info("kateway[%s@%s] %s, bye!", gafka.BuildId, gafka.BuiltAt, time.Since(t0))
	glog.Close()

	if gateway.Options.PidFile != "" {
		syscall.Unlink(gateway.Options.PidFile)
	}
}