tracker/main.go

92 lines
1.9 KiB
Go
Raw Normal View History

2013-06-22 01:31:32 +02:00
// Copyright 2013 The Chihaya Authors. All rights reserved.
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
package main
import (
"flag"
"os"
"os/signal"
"runtime"
"runtime/pprof"
log "github.com/golang/glog"
2014-06-19 18:34:47 +02:00
"github.com/chihaya/chihaya/config"
2014-06-24 04:47:43 +02:00
_ "github.com/chihaya/chihaya/drivers/backend/mock"
_ "github.com/chihaya/chihaya/drivers/tracker/mock"
"github.com/chihaya/chihaya/server"
2013-06-22 01:31:32 +02:00
)
var (
profile bool
2013-06-23 09:56:28 +02:00
configPath string
2013-06-22 01:31:32 +02:00
)
func init() {
flag.BoolVar(&profile, "profile", false, "Generate profiling data for pprof into ./chihaya.cpu")
flag.StringVar(&configPath, "config", "", "Provide the filesystem path of a valid configuration file.")
2013-06-22 01:31:32 +02:00
}
func main() {
flag.Parse()
runtime.GOMAXPROCS(runtime.NumCPU())
2014-05-08 12:48:32 +02:00
// Enable the profile if flagged.
2013-06-22 01:31:32 +02:00
if profile {
f, err := os.Create("chihaya.cpu")
if err != nil {
2014-06-26 23:10:39 +02:00
log.Fatalf("failed to create profile file: %s\n", err)
2013-06-22 01:31:32 +02:00
}
defer f.Close()
2013-06-22 01:31:32 +02:00
pprof.StartCPUProfile(f)
2014-06-26 23:10:39 +02:00
log.V(1).Info("started profiling")
2013-06-22 01:31:32 +02:00
}
2014-05-08 12:48:32 +02:00
// Load the config file.
conf, err := config.Open(configPath)
2013-06-23 09:56:28 +02:00
if err != nil {
2014-06-26 23:10:39 +02:00
log.Fatalf("failed to parse configuration file: %s\n", err)
2013-06-23 09:56:28 +02:00
}
2014-05-08 12:48:32 +02:00
// Create a new server.
2013-06-24 06:18:59 +02:00
s, err := server.New(conf)
if err != nil {
2014-06-26 23:10:39 +02:00
log.Fatalf("failed to create server: %s\n", err)
2013-06-24 06:18:59 +02:00
}
2013-06-22 01:31:32 +02:00
2014-05-08 12:48:32 +02:00
// Spawn a goroutine to handle interrupts and safely shut down.
2013-06-22 01:31:32 +02:00
go func() {
interrupts := make(chan os.Signal, 1)
signal.Notify(interrupts, os.Interrupt)
<-interrupts
2014-06-26 23:10:39 +02:00
log.V(1).Info("caught interrupt, shutting down...")
2013-06-22 01:31:32 +02:00
if profile {
pprof.StopCPUProfile()
2014-06-26 23:10:39 +02:00
log.V(1).Info("stopped profiling")
2013-06-22 01:31:32 +02:00
}
err := s.Stop()
if err != nil {
2014-06-26 23:10:39 +02:00
log.Fatalf("failed to shutdown cleanly: %s", err)
2013-06-22 01:31:32 +02:00
}
2014-06-26 23:10:39 +02:00
log.V(1).Info("shutdown cleanly")
<-interrupts
log.Flush()
2013-06-22 01:31:32 +02:00
os.Exit(0)
}()
2014-05-08 12:48:32 +02:00
// Start the server listening and handling requests.
2013-06-23 09:56:28 +02:00
err = s.ListenAndServe()
2013-06-22 01:31:32 +02:00
if err != nil {
2014-06-26 23:10:39 +02:00
log.Fatalf("failed to start server: %s\n", err)
2013-06-22 01:31:32 +02:00
}
}