Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nsqd/nsqlookupd: support running as windows service #718

Merged
merged 1 commit into from
Feb 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Godeps
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ github.com/mreiferson/go-snappystream 028eae7ab5c4c9e2d1cb4c4ca1e53259bbe7e504
github.com/bitly/timer_metrics afad1794bb13e2a094720aeb27c088aa64564895
github.com/blang/semver 9bf7bff48b0388cb75991e58c6df7d13e982f1f2
github.com/julienschmidt/httprouter 6aacfd5ab513e34f7e64ea9627ab9670371b34e7
github.com/judwhite/go-svc/svc 53bd3020e68399b23994ce23d1130801aa674226
40 changes: 32 additions & 8 deletions apps/nsqd/nsqd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"log"
"math/rand"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"

"github.com/BurntSushi/toml"
"github.com/judwhite/go-svc/svc"
"github.com/mreiferson/go-options"
"github.com/nsqio/nsq/internal/app"
"github.com/nsqio/nsq/internal/version"
Expand Down Expand Up @@ -174,20 +174,36 @@ func (cfg config) Validate() {
}
}

type program struct {
nsqd *nsqd.NSQD
}

func main() {
prg := &program{}
if err := svc.Run(prg); err != nil {
log.Fatal(err)
}
}

func (p *program) Init(env svc.Environment) error {
if env.IsWindowsService() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine for now, but perhaps users of go-svc want to specify what signals they want to handle? Right now it's hardcoded https://github.com/judwhite/go-svc/blob/master/svc/svc_other.go#L19

dir := filepath.Dir(os.Args[0])
return os.Chdir(dir)
}
return nil
}

func (p *program) Start() error {
flagSet := nsqFlagset()
flagSet.Parse(os.Args[1:])

rand.Seed(time.Now().UTC().UnixNano())

if flagSet.Lookup("version").Value.(flag.Getter).Get().(bool) {
fmt.Println(version.String("nsqd"))
return
os.Exit(0)
}

signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)

var cfg config
configFile := flagSet.Lookup("config").Value.String()
if configFile != "" {
Expand All @@ -208,6 +224,14 @@ func main() {
log.Fatalf("ERROR: failed to persist metadata - %s", err.Error())
}
nsqd.Main()
<-signalChan
nsqd.Exit()

p.nsqd = nsqd
return nil
}

func (p *program) Stop() error {
if p.nsqd != nil {
p.nsqd.Exit()
}
return nil
}
39 changes: 31 additions & 8 deletions apps/nsqlookupd/nsqlookupd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"path/filepath"
"time"

"github.com/BurntSushi/toml"
"github.com/judwhite/go-svc/svc"
"github.com/mreiferson/go-options"
"github.com/nsqio/nsq/internal/version"
"github.com/nsqio/nsq/nsqlookupd"
Expand All @@ -30,17 +30,33 @@ var (
tombstoneLifetime = flagSet.Duration("tombstone-lifetime", 45*time.Second, "duration of time a producer will remain tombstoned if registration remains")
)

type program struct {
nsqlookupd *nsqlookupd.NSQLookupd
}

func main() {
prg := &program{}
if err := svc.Run(prg); err != nil {
log.Fatal(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 {
flagSet.Parse(os.Args[1:])

if *showVersion {
fmt.Println(version.String("nsqlookupd"))
return
os.Exit(0)
}

signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)

var cfg map[string]interface{}
if *config != "" {
_, err := toml.DecodeFile(*config, &cfg)
Expand All @@ -54,6 +70,13 @@ func main() {
daemon := nsqlookupd.New(opts)

daemon.Main()
<-signalChan
daemon.Exit()
p.nsqlookupd = daemon
return nil
}

func (p *program) Stop() error {
if p.nsqlookupd != nil {
p.nsqlookupd.Exit()
}
return nil
}