Shutdown on Ctrl+C

Changed code on Windows to not use svc code if running in interactive
mode. The original code was running svc.debug.Run() which uses service
code (Execute()) but from the command line. We don't need that.

Also reduced salt on bcrypt password for a config file that started
to cause failures due to test taking too long to finish.

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
This commit is contained in:
Ivan Kozlovic
2019-11-14 18:47:02 -07:00
parent c2d2307670
commit bdf5cf63b3
10 changed files with 11 additions and 110 deletions

View File

@@ -1,5 +1,6 @@
language: go
go:
- 1.13.x
- 1.12.x
addons:
apt:

View File

@@ -17,6 +17,7 @@ import (
"flag"
"fmt"
"os"
"runtime"
"github.com/nats-io/nats-server/v2/server"
)
@@ -109,4 +110,5 @@ func main() {
if err := server.Run(s); err != nil {
server.PrintAndDie(err.Error())
}
runtime.Goexit()
}

View File

@@ -1168,7 +1168,7 @@ func (s *Server) Shutdown() {
s.mu.Unlock()
return
}
s.Noticef("Server Exiting..")
s.Noticef("Initiating Shutdown...")
opts := s.getOpts()
@@ -1271,6 +1271,7 @@ func (s *Server) Shutdown() {
s.deletePortsFile(opts.PortsFileDir)
}
s.Noticef("Server Exiting..")
// Close logger if applicable. It allows tests on Windows
// to be able to do proper cleanup (delete log file).
s.logging.RLock()

View File

@@ -18,7 +18,6 @@ import (
"time"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/debug"
)
const (
@@ -110,15 +109,15 @@ func Run(server *Server) error {
server.Start()
return nil
}
run := svc.Run
isInteractive, err := svc.IsAnInteractiveSession()
if err != nil {
return err
}
if isInteractive {
run = debug.Run
server.Start()
return nil
}
return run(serviceName, &winServiceWrapper{server})
return svc.Run(serviceName, &winServiceWrapper{server})
}
// isWindowsService indicates if NATS is running as a Windows service.

View File

@@ -49,7 +49,7 @@ func (s *Server) handleSignals() {
s.Debugf("Trapped %q signal", sig)
switch sig {
case syscall.SIGINT:
s.Noticef("Server Exiting..")
s.Shutdown()
os.Exit(0)
case syscall.SIGUSR1:
// File log re-open for rotating file logs.

View File

@@ -35,7 +35,7 @@ func (s *Server) handleSignals() {
go func() {
for sig := range c {
s.Debugf("Trapped %q signal", sig)
s.Noticef("Server Exiting..")
s.Shutdown()
os.Exit(0)
}
}()

View File

@@ -5,7 +5,7 @@ authorization {
include "auths.conf"
# Just foo for testing
PASS: $2a$10$UHR6GhotWhpLsKtVP0/i6.Nh9.fuY73cWjLoJjb2sKT8KISBcUW5q
PASS: $2a$04$P/.bd.7unw9Ew7yWJqXsl.f4oNRLQGvadEL2YnqQXbbb.IVQajRdK
# Users listed with permissions.
users = [

View File

@@ -1,56 +0,0 @@
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build windows
package debug
import (
"os"
"strconv"
)
// Log interface allows different log implementations to be used.
type Log interface {
Close() error
Info(eid uint32, msg string) error
Warning(eid uint32, msg string) error
Error(eid uint32, msg string) error
}
// ConsoleLog provides access to the console.
type ConsoleLog struct {
Name string
}
// New creates new ConsoleLog.
func New(source string) *ConsoleLog {
return &ConsoleLog{Name: source}
}
// Close closes console log l.
func (l *ConsoleLog) Close() error {
return nil
}
func (l *ConsoleLog) report(kind string, eid uint32, msg string) error {
s := l.Name + "." + kind + "(" + strconv.Itoa(int(eid)) + "): " + msg + "\n"
_, err := os.Stdout.Write([]byte(s))
return err
}
// Info writes an information event msg with event id eid to the console l.
func (l *ConsoleLog) Info(eid uint32, msg string) error {
return l.report("info", eid, msg)
}
// Warning writes an warning event msg with event id eid to the console l.
func (l *ConsoleLog) Warning(eid uint32, msg string) error {
return l.report("warn", eid, msg)
}
// Error writes an error event msg with event id eid to the console l.
func (l *ConsoleLog) Error(eid uint32, msg string) error {
return l.report("error", eid, msg)
}

View File

@@ -1,45 +0,0 @@
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build windows
// Package debug provides facilities to execute svc.Handler on console.
//
package debug
import (
"os"
"os/signal"
"syscall"
"golang.org/x/sys/windows/svc"
)
// Run executes service name by calling appropriate handler function.
// The process is running on console, unlike real service. Use Ctrl+C to
// send "Stop" command to your service.
func Run(name string, handler svc.Handler) error {
cmds := make(chan svc.ChangeRequest)
changes := make(chan svc.Status)
sig := make(chan os.Signal)
signal.Notify(sig)
go func() {
status := svc.Status{State: svc.Stopped}
for {
select {
case <-sig:
cmds <- svc.ChangeRequest{Cmd: svc.Stop, CurrentStatus: status}
case status = <-changes:
}
}
}()
_, errno := handler.Execute([]string{name}, cmds, changes)
if errno != 0 {
return syscall.Errno(errno)
}
return nil
}

1
vendor/modules.txt vendored
View File

@@ -16,7 +16,6 @@ golang.org/x/crypto/ed25519/internal/edwards25519
# golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e
golang.org/x/sys/windows/svc/eventlog
golang.org/x/sys/windows/svc
golang.org/x/sys/windows/svc/debug
golang.org/x/sys/windows/svc/mgr
golang.org/x/sys/windows
golang.org/x/sys/windows/registry