func init() { parseFlags() if options.showVersion { engine.ShowVersionAndExit() } 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) } signal.RegisterSignalHandler(syscall.SIGINT, func(sig os.Signal) { shutdown() }) }
func main() { defer func() { cleanup() if err := recover(); err != nil { fmt.Println(err) debug.PrintStack() } }() if options.cpuprof || options.memprof { cf := &profiler.Config{ Quiet: true, ProfilePath: "prof", CPUProfile: options.cpuprof, MemProfile: options.memprof, BlockProfile: options.blockprof, } defer profiler.Start(cf).Stop() } log.Info("%s", ` ____ __ ____ ( ___) /__\ ( ___) )__) /(__)\ )__) (__) (__)(__) (____)`) s := server.NewServer("fae") s.LoadConfig(options.configFile) s.Launch() go server.RunSysStats(time.Now(), options.tick) engineRunner = engine.NewEngine() signal.RegisterSignalHandler(syscall.SIGINT, func(sig os.Signal) { shutdown() engineRunner.StopRpcServe() }) engineRunner.LoadConfig(s.Conf). ServeForever() }
func (this *Engine) ServeForever() { this.StartedAt = time.Now() signal.RegisterSignalHandler(syscall.SIGUSR1, func(sig os.Signal) { // graceful shutdown this.StopRpcServe() }) var ( totalCpus int maxProcs int ) totalCpus = runtime.NumCPU() cpuNumConfig := config.Engine.String("cpu_num", "auto") if cpuNumConfig == "auto" { maxProcs = totalCpus/2 + 1 } else if cpuNumConfig == "max" { maxProcs = totalCpus } else { maxProcs, _ = strconv.Atoi(cpuNumConfig) } runtime.GOMAXPROCS(maxProcs) log.Info("Launching Engine with %d/%d CPUs...", maxProcs, totalCpus) go plugin.Start() this.launchHttpServ() defer this.stopHttpServ() select { case <-this.launchRpcServe(): case <-this.stopChan: } log.Info("Engine terminated") }