ledisdb/cmd/ledis-server/main.go

120 lines
2.6 KiB
Go
Raw Normal View History

2014-05-05 07:37:44 +04:00
package main
import (
"flag"
2014-09-18 06:25:15 +04:00
"fmt"
"github.com/siddontang/ledisdb/config"
"github.com/siddontang/ledisdb/server"
2014-08-05 04:43:04 +04:00
"log"
"net/http"
_ "net/http/pprof"
2014-05-05 07:37:44 +04:00
"os"
"os/signal"
2014-05-09 05:17:28 +04:00
"runtime"
"strings"
2014-05-05 07:37:44 +04:00
"syscall"
)
var configFile = flag.String("config", "", "ledisdb config file")
2014-10-22 05:11:14 +04:00
var addr = flag.String("addr", "", "ledisdb listen address")
var dataDir = flag.String("data_dir", "", "ledisdb base data dir")
2014-07-26 14:39:54 +04:00
var dbName = flag.String("db_name", "", "select a db to use, it will overwrite the config's db name")
2014-09-18 06:25:15 +04:00
var usePprof = flag.Bool("pprof", false, "enable pprof")
var pprofPort = flag.Int("pprof_port", 6060, "pprof http port")
2014-10-10 05:49:16 +04:00
var slaveof = flag.String("slaveof", "", "make the server a slave of another instance")
2014-12-23 03:47:28 +03:00
var readonly = flag.Bool("readonly", false, "set readonly mode, slave server is always readonly")
2014-10-11 12:00:59 +04:00
var rpl = flag.Bool("rpl", false, "enable replication or not, slave server is always enabled")
2014-10-22 05:11:14 +04:00
var rplSync = flag.Bool("rpl_sync", false, "enable sync replication or not")
2014-10-30 04:00:19 +03:00
var ttlCheck = flag.Int("ttl_check", 0, "TTL check interval")
2015-03-16 08:39:15 +03:00
var databases = flag.Int("databases", 0, "ledisdb maximum database number")
2014-05-05 07:37:44 +04:00
func main() {
2014-05-09 05:17:28 +04:00
runtime.GOMAXPROCS(runtime.NumCPU())
2014-05-05 07:37:44 +04:00
flag.Parse()
var cfg *config.Config
var err error
2014-05-05 07:37:44 +04:00
if len(*configFile) == 0 {
println("no config set, using default config")
cfg = config.NewConfigDefault()
} else {
cfg, err = config.NewConfigWithFile(*configFile)
2014-05-05 07:37:44 +04:00
}
if err != nil {
2014-07-02 06:13:10 +04:00
println(err.Error())
return
2014-05-05 07:37:44 +04:00
}
2014-10-22 05:11:14 +04:00
if len(*addr) > 0 {
cfg.Addr = *addr
}
if len(*dataDir) > 0 {
cfg.DataDir = *dataDir
}
2014-07-26 14:39:54 +04:00
if len(*dbName) > 0 {
cfg.DBName = *dbName
2014-07-25 20:46:03 +04:00
}
2015-03-16 08:39:15 +03:00
if *databases > 0 {
cfg.Databases = *databases
}
2015-03-21 15:31:43 +03:00
// check bool flag, use it.
for _, arg := range os.Args {
arg := strings.ToLower(arg)
switch arg {
case "-rpl", "-rpl=true", "-rpl=false":
cfg.UseReplication = *rpl
case "-readonly", "-readonly=true", "-readonly=false":
cfg.Readonly = *readonly
case "-rpl_sync", "-rpl_sync=true", "-rpl_sync=false":
cfg.Replication.Sync = *rplSync
}
}
2014-10-10 05:49:16 +04:00
if len(*slaveof) > 0 {
cfg.SlaveOf = *slaveof
cfg.Readonly = true
2014-10-11 12:00:59 +04:00
cfg.UseReplication = true
2014-10-10 05:49:16 +04:00
}
2014-10-30 04:00:19 +03:00
if *ttlCheck > 0 {
cfg.TTLCheckInterval = *ttlCheck
}
var app *server.App
app, err = server.NewApp(cfg)
2014-05-05 07:37:44 +04:00
if err != nil {
2014-07-02 06:13:10 +04:00
println(err.Error())
return
2014-05-05 07:37:44 +04:00
}
sc := make(chan os.Signal, 1)
signal.Notify(sc,
2014-11-11 06:09:32 +03:00
os.Kill,
os.Interrupt,
2014-05-05 07:37:44 +04:00
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
2014-09-18 06:25:15 +04:00
if *usePprof {
go func() {
log.Println(http.ListenAndServe(fmt.Sprintf(":%d", *pprofPort), nil))
}()
}
2014-08-05 04:43:04 +04:00
go app.Run()
<-sc
2015-05-03 06:19:31 +03:00
println("ledis-server is closing")
app.Close()
2015-05-03 06:19:31 +03:00
println("ledis-server is closed")
2014-05-05 07:37:44 +04:00
}