package main import ( "gd_crontab/impl" "gd_crontab/impl/crontab" "gd_crontab/impl/warning" "gd_crontab/rpc_apis" "flag" "fmt" "log" "os" "os/signal" "strings" "syscall" "time" "gd_crontab/common.in/clinit" "gd_crontab/common.in/config" "gd_crontab/common.in/logger" "gd_crontab/common.in/utils" "github.com/astaxie/beego/orm" _ "github.com/go-sql-driver/mysql" //"github.com/gomodule/redigo/redis" "github.com/smallnest/rpcx/server" "github.com/smallnest/rpcx/serverplugin" gconfig "gd_crontab/common.in/config" "github.com/rcrowley/go-metrics" "gopkg.in/ini.v1" ) var ( // 这里可以改默认值 configFile = flag.String("config", "/etc/gd_crontab/app.conf", "config file location") version = flag.Bool("version", false, "config file location") GitCommit = "library-import" Version = "library-import" localReportAddr = flag.String("rh", "", "") localReportDebug = flag.Bool("rdebug", false, "") localReportUser = flag.String("ru", "", "") localReportPass = flag.String("rp", "", "") localReportDates = flag.String("rd", "", "") localReportMerchantId = flag.Int64("rmid", 0, "") localReportApiId = flag.Int64("raid", 0, "") localReportProviderApiId = flag.Int64("rpid", 0, "") localReportApi = flag.Bool("rapi", false, "") localReportProvider = flag.Bool("rprovider", false, "") localReportStart = flag.Int64("rstart", 0, "") localReportEnd = flag.Int64("rend", 0, "") tec = flag.Bool("tec", false, "") tes = flag.Bool("tes", false, "") tem = flag.Bool("tem", false, "") ) func showVersion() { fmt.Println("Version: ", Version) fmt.Println("GitCommit:", GitCommit) } func prepare(projectName, runmode, key, logDir string, etcdAddrs []string, discoveryType string) { // 先行于读配置 var conf *config.Configure if discoveryType == "k8s" { conf = config.GetConfigForK8s() if conf == nil { fmt.Printf("get conf failed\n\n") os.Exit(1) } rpc_apis.InitForK8s(conf) } else { // 先行于读配置 clinit.InitEtcd(etcdAddrs) conf = config.GetConfig(projectName+"/"+runmode, key, clinit.GetEtcdClient()) if conf == nil { fmt.Printf("get conf failed\n\n") os.Exit(1) } rpc_apis.Init(etcdAddrs, conf) } appname := conf.Rpc.Crontab.Name // 指定mysql数据库,若无则使用默认数据库 mysqldb := conf.Rpc.Crontab.MysqlDB if mysqldb == "" { mysqldb = conf.LogMysql.DefaultDB } // 连接数据库服务器 clinit.InitMySQL(&clinit.MysqlConfig{ User: conf.LogMysql.User, Password: conf.LogMysql.Password, Addr: conf.LogMysql.Addr, DB: mysqldb, Charset: conf.LogMysql.Charset, MaxIdle: conf.LogMysql.MaxIdle, MaxConn: conf.LogMysql.MaxConn, }) // 初始化logger /*ms, _ := conf.Rpc.AccessLog.Log.MaxSize.Int64() mb, _ := conf.Rpc.AccessLog.Log.MaxBackups.Int64() ma, _ := conf.Rpc.AccessLog.Log.MaxAge.Int64() maxSize := int(ms) maxBackups := int(mb) maxAge := int(ma) disableStacktrace := (conf.Rpc.AccessLog.Log.DisableStacktrace == "true") commonLogger := logger.InitLogger(runmode, fmt.Sprintf("%s/%s.log", logDir, appname), maxSize, maxBackups, maxAge, disableStacktrace) accessLogger := logger.NewInfoLogger(runmode, fmt.Sprintf("%s/%s-access.log", logDir, appname), maxSize, maxBackups, maxAge) thirdpartyLogger := logger.NewInfoLogger(runmode, fmt.Sprintf("%s/%s-thirdparty.log", logDir, appname), maxSize, maxBackups, maxAge) */ /*if err := mongo.MgoInit(conf.Mongo.Addr, conf.Mongo.User, conf.Mongo.Password); err != nil { fmt.Printf("init mongo failed:%v, conf.Mongo.Uuser:%s\n", err, conf.Mongo.User) os.Exit(-1) }*/ // 设置需要使用logger的地方 ms, _ := conf.Rpc.Crontab.Log.MaxSize.Int64() mb, _ := conf.Rpc.Crontab.Log.MaxBackups.Int64() ma, _ := conf.Rpc.Crontab.Log.MaxAge.Int64() maxSize := int(ms) maxBackups := int(mb) maxAge := int(ma) disableStacktrace := (conf.Rpc.Crontab.Log.DisableStacktrace == "true") // 通用logger commonLogger := logger.InitLogger(runmode, fmt.Sprintf("%s/%s.log", logDir, appname), appname, conf.Rpc.Crontab.Log.Level, maxSize, maxBackups, maxAge, disableStacktrace) crontab.SetLogger(commonLogger) impl.RegisterOrmModel() // common日志 if runmode != "prod" { orm.Debug = true //redis.Debug = false } } func start(conf *config.Configure, serveAddr string, etcdAddrs []string) { s := server.NewServer() r := &serverplugin.EtcdRegisterPlugin{ ServiceAddress: fmt.Sprintf("tcp@%s", serveAddr), EtcdServers: etcdAddrs, BasePath: conf.Rpc.BasePath, Metrics: metrics.NewRegistry(), UpdateInterval: time.Minute, } if err := r.Start(); err != nil { fmt.Printf("start failed. error:%s\n\n", err) os.Exit(1) } s.Plugins.Add(r) s.RegisterName(conf.Rpc.Crontab.Name, new(impl.Rcvr), "") go func() { if err := s.Serve("tcp", serveAddr); err != nil { log.Fatalf("HTTP server listen failed. err: %s\n", err.Error()) } }() // 捕获信号 sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL) sigValue := <-sigChan log.Printf("Got a signal:%v", sigValue) r.Stop() time.Sleep(5 * time.Second) log.Println("Shutdown server finished.") } func startPeerToPeer(conf *config.Configure, serveAddr string) { s := server.NewServer() s.RegisterName(conf.Rpc.Crontab.Name, new(impl.Rcvr), "") s.Serve("tcp", serveAddr) } func main() { flag.Parse() if *version { showVersion() } cfg, err := ini.Load(*configFile) if err != nil { fmt.Printf("Fail to read file: %v\n\n", err) os.Exit(1) } crontab.TestExceptionCompute = *tec crontab.TestExceptionSql = *tes crontab.TestExceptionMail = *tem //appname := cfg.Section("").Key("appname").String() logDir := cfg.Section("").Key("log_dir").String() runmode := cfg.Section("").Key("runmode").String() //serveAddr := cfg.Section("").Key("serve_addr").String() etcdAddrs := strings.Split(cfg.Section("").Key("etcd_addrs").String(), ",") encryptKey := cfg.Section("").Key("encrypt_key").String() discoveryType := cfg.Section("").Key("discovery_type").String() projectName := cfg.Section("").Key("project_name").String() prepare( projectName, runmode, encryptKey, logDir, etcdAddrs, discoveryType) if *localReportAddr != "" { db := crontab.GetLocalDb(*localReportUser, *localReportPass, *localReportAddr) //crontab.GernerateHourReport(*localReportStart, *localReportEnd, *localReportApi, *localReportProvider, db) crontab.AutoExportTest(db) os.Exit(0) } go utils.Free() warning.ParseThreshold() crontab.DeleteLogDayTask() crontab.AutoExport() crontab.LogHourTask() //warning.LoopCheck() impl.TaskInit() serveAddr := fmt.Sprintf("%s:%s",gconfig.Conf.Rpc.Crontab.ServiceName,gconfig.Conf.Rpc.Crontab.ServicePort.String()) if discoveryType == "etcd" { fmt.Println(gconfig.Conf, serveAddr, etcdAddrs) start(gconfig.Conf, serveAddr, etcdAddrs) } else { startPeerToPeer(gconfig.Conf, serveAddr) } return }