123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- package main
- import (
- "flag"
- "fmt"
- "os"
- "path/filepath"
- "sync"
- "syscall"
- "github.com/BurntSushi/toml"
- "github.com/judwhite/go-svc"
- "github.com/mreiferson/go-options"
- "github.com/nsqio/nsq/internal/lg"
- "github.com/nsqio/nsq/internal/version"
- "github.com/nsqio/nsq/nsqlookupd"
- )
- func nsqlookupdFlagSet(opts *nsqlookupd.Options) *flag.FlagSet {
- flagSet := flag.NewFlagSet("nsqlookupd", flag.ExitOnError)
- flagSet.String("config", "", "path to config file")
- flagSet.Bool("version", false, "print version string")
- logLevel := opts.LogLevel
- flagSet.Var(&logLevel, "log-level", "set log verbosity: debug, info, warn, error, or fatal")
- flagSet.String("log-prefix", "[smqlookupd] ", "log message prefix")
- flagSet.Bool("verbose", false, "[deprecated] has no effect, use --log-level")
- flagSet.String("tcp-address", opts.TCPAddress, "<addr>:<port> to listen on for TCP clients")
- flagSet.String("http-address", opts.HTTPAddress, "<addr>:<port> to listen on for HTTP clients")
- flagSet.String("broadcast-address", opts.BroadcastAddress, "address of this lookupd node, (default to the OS hostname)")
- flagSet.Duration("inactive-producer-timeout", opts.InactiveProducerTimeout, "duration of time a producer will remain in the active list since its last ping")
- flagSet.Duration("tombstone-lifetime", opts.TombstoneLifetime, "duration of time a producer will remain tombstoned if registration remains")
- return flagSet
- }
- type program struct {
- once sync.Once
- nsqlookupd *nsqlookupd.NSQLookupd
- }
- func main() {
- prg := &program{}
- if err := svc.Run(prg, syscall.SIGINT, syscall.SIGTERM); err != nil {
- logFatal("%s", err)
- }
- }
- func (p *program) Init(env svc.Environment) error {
- if env.IsWindowsService() {
- dir := filepath.Dir(os.Args[0])
- return os.Chdir(dir)
- }
- return nil
- }
- func (p *program) Start() error {
- opts := nsqlookupd.NewOptions()
- flagSet := nsqlookupdFlagSet(opts)
- flagSet.Parse(os.Args[1:])
- if flagSet.Lookup("version").Value.(flag.Getter).Get().(bool) {
- fmt.Println(version.String("smqlookupd"))
- os.Exit(0)
- }
- var cfg config
- configFile := flagSet.Lookup("config").Value.String()
- if configFile != "" {
- _, err := toml.DecodeFile(configFile, &cfg)
- if err != nil {
- logFatal("failed to load config file %s - %s", configFile, err)
- }
- }
- cfg.Validate()
- options.Resolve(opts, flagSet, cfg)
- nsqlookupd, err := nsqlookupd.New(opts)
- if err != nil {
- logFatal("failed to instantiate smqlookupd", err)
- }
- p.nsqlookupd = nsqlookupd
- go func() {
- err := p.nsqlookupd.Main()
- if err != nil {
- p.Stop()
- os.Exit(1)
- }
- }()
- return nil
- }
- func (p *program) Stop() error {
- p.once.Do(func() {
- p.nsqlookupd.Exit()
- })
- return nil
- }
- func logFatal(f string, args ...interface{}) {
- lg.LogFatal("[smqlookupd] ", f, args...)
- }
|