func main() { defer exit.Recover() defer logutil.Flush() dbconfigs.RegisterFlags() flag.Parse() mycnf := mysqlctl.NewMycnf(uint32(*tabletUid), *mysqlPort) if *mysqlSocket != "" { mycnf.SocketFile = *mysqlSocket } dbcfgs, err := dbconfigs.Init(mycnf.SocketFile) if err != nil { log.Errorf("%v", err) exit.Return(255) } mysqld = mysqlctl.NewMysqld("Dba", mycnf, &dbcfgs.Dba, &dbcfgs.Repl) // Register OnTerm handler before mysqld starts, so we get notified if mysqld // dies on its own without us (or our RPC client) telling it to. mysqldTerminated := make(chan struct{}) mysqld.OnTerm(func() { close(mysqldTerminated) }) // Start or Init mysqld as needed. if _, err = os.Stat(mycnf.DataDir); os.IsNotExist(err) { log.Infof("mysql data dir (%s) doesn't exist, initializing", mycnf.DataDir) mysqld.Init(*waitTime, *bootstrapArchive, *skipSchema) } else { log.Infof("mysql data dir (%s) already exists, starting without init", mycnf.DataDir) mysqld.Start(*waitTime) } servenv.Init() defer servenv.Close() // Take mysqld down with us on SIGTERM before entering lame duck. servenv.OnTerm(func() { log.Infof("mysqlctl received SIGTERM, shutting down mysqld first") mysqld.Shutdown(false, 0) }) // Start RPC server and wait for SIGTERM. mysqlctldTerminated := make(chan struct{}) go func() { servenv.RunDefault() close(mysqlctldTerminated) }() select { case <-mysqldTerminated: log.Infof("mysqld shut down on its own, exiting mysqlctld") case <-mysqlctldTerminated: log.Infof("mysqlctld shut down gracefully") } }
func main() { defer logutil.Flush() flag.Usage = func() { fmt.Fprintf(os.Stderr, "Usage: %s [global parameters] command [command parameters]\n", os.Args[0]) fmt.Fprintf(os.Stderr, "\nThe global optional parameters are:\n") flag.PrintDefaults() fmt.Fprintf(os.Stderr, "\nThe commands are listed below. Use '%s <command> -h' for more help.\n\n", os.Args[0]) for _, cmd := range commands { fmt.Fprintf(os.Stderr, " %s", cmd.name) if cmd.params != "" { fmt.Fprintf(os.Stderr, " %s", cmd.params) } fmt.Fprintf(os.Stderr, "\n") } fmt.Fprintf(os.Stderr, "\n") } dbconfigs.RegisterFlags() flag.Parse() tabletAddr = fmt.Sprintf("%v:%v", "localhost", *port) mycnf := mysqlctl.NewMycnf(uint32(*tabletUid), *mysqlPort) if *mysqlSocket != "" { mycnf.SocketFile = *mysqlSocket } dbcfgs, err := dbconfigs.Init(mycnf.SocketFile) if err != nil { log.Fatalf("%v", err) } mysqld := mysqlctl.NewMysqld("Dba", mycnf, &dbcfgs.Dba, &dbcfgs.Repl) defer mysqld.Close() action := flag.Arg(0) for _, cmd := range commands { if cmd.name == action { subFlags := flag.NewFlagSet(action, flag.ExitOnError) subFlags.Usage = func() { fmt.Fprintf(os.Stderr, "Usage: %s %s %s\n\n", os.Args[0], cmd.name, cmd.params) fmt.Fprintf(os.Stderr, "%s\n\n", cmd.help) subFlags.PrintDefaults() } cmd.method(mysqld, subFlags, flag.Args()[1:]) return } } log.Fatalf("invalid action: %v", action) }