2014-05-05 07:37:44 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2014-09-18 06:25:15 +04:00
|
|
|
"fmt"
|
2014-08-07 12:49:48 +04:00
|
|
|
"github.com/siddontang/ledisdb/config"
|
2014-05-16 11:03:23 +04:00
|
|
|
"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"
|
2015-02-14 09:38:13 +03:00
|
|
|
"strings"
|
2014-05-05 07:37:44 +04:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2014-08-07 12:49:48 +04:00
|
|
|
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()
|
|
|
|
|
2014-08-07 12:49:48 +04:00
|
|
|
var cfg *config.Config
|
|
|
|
var err error
|
|
|
|
|
2014-05-05 07:37:44 +04:00
|
|
|
if len(*configFile) == 0 {
|
2014-08-07 12:49:48 +04:00
|
|
|
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 {
|
2014-08-07 12:49:48 +04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
} else {
|
|
|
|
cfg.Readonly = *readonly
|
2015-02-14 09:38:13 +03:00
|
|
|
|
|
|
|
// if rpl in command flag, use it.
|
|
|
|
for _, arg := range os.Args {
|
|
|
|
arg := strings.ToLower(arg)
|
|
|
|
if arg == "-rpl" || arg == "-rpl=true" || arg == "-rpl=false" {
|
|
|
|
cfg.UseReplication = *rpl
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-22 05:11:14 +04:00
|
|
|
cfg.Replication.Sync = *rplSync
|
2014-10-10 05:49:16 +04:00
|
|
|
}
|
|
|
|
|
2014-10-30 04:00:19 +03:00
|
|
|
if *ttlCheck > 0 {
|
|
|
|
cfg.TTLCheckInterval = *ttlCheck
|
|
|
|
}
|
2014-10-29 11:02:46 +03:00
|
|
|
|
2014-05-16 11:03:23 +04:00
|
|
|
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
|
|
|
|
2014-11-11 10:21:41 +03:00
|
|
|
go app.Run()
|
|
|
|
|
|
|
|
<-sc
|
|
|
|
|
|
|
|
app.Close()
|
2014-05-05 07:37:44 +04:00
|
|
|
}
|