Skip to content
Closed
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
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@
fs.Usage,
server.PrintTLSHelpAndDie)
if err != nil {
server.PrintAndDie(fmt.Sprintf("%s: %s", exe, err))
fmt.Fprintln(os.Stderr, fmt.Sprintf("%s: %s", exe, err))

Check failure on line 111 in main.go

View workflow job for this annotation

GitHub Actions / Lint

S1038: should use fmt.Fprintf instead of fmt.Fprintln(fmt.Sprintf(...)) (but don't forget the newline) (staticcheck)

Check failure on line 111 in main.go

View workflow job for this annotation

GitHub Actions / Lint

S1038: should use fmt.Fprintf instead of fmt.Fprintln(fmt.Sprintf(...)) (but don't forget the newline) (staticcheck)
os.Exit(1)
} else if opts.CheckConfig {
fmt.Fprintf(os.Stderr, "%s: configuration file %s is valid (%s)\n", exe, opts.ConfigFile, opts.ConfigDigest())
os.Exit(0)
Expand All @@ -117,15 +118,16 @@
// Create the server with appropriate options.
s, err := server.NewServer(opts)
if err != nil {
server.PrintAndDie(fmt.Sprintf("%s: %s", exe, err))
fmt.Fprintln(os.Stderr, fmt.Sprintf("%s: %s", exe, err))

Check failure on line 121 in main.go

View workflow job for this annotation

GitHub Actions / Lint

S1038: should use fmt.Fprintf instead of fmt.Fprintln(fmt.Sprintf(...)) (but don't forget the newline) (staticcheck)

Check failure on line 121 in main.go

View workflow job for this annotation

GitHub Actions / Lint

S1038: should use fmt.Fprintf instead of fmt.Fprintln(fmt.Sprintf(...)) (but don't forget the newline) (staticcheck)
os.Exit(1)
}

// Configure the logger based on the flags.
s.ConfigureLogger()

// Start things up. Block here until done.
if err := server.Run(s); err != nil {
server.PrintAndDie(err.Error())
s.PrintAndDie(err.Error())
}

// Adjust MAXPROCS if running under linux/cgroups quotas.
Expand Down
20 changes: 14 additions & 6 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
opts *Options
running atomic.Bool
shutdown atomic.Bool
shutdownSignal atomic.Int32
listener net.Listener
listenerErr error
gacc *Account
Expand Down Expand Up @@ -1544,6 +1545,19 @@
return true
}

// PrintAndDie print error message and exit
func (s *Server) PrintAndDie(msg string) {

sig := s.shutdownSignal.Load()
if sig == 0 {
fmt.Fprintln(os.Stderr, msg)
os.Exit(1)
} else {
fmt.Fprintln(os.Stderr, fmt.Sprintf("Shutting down due to signal: %d", sig))

Check failure on line 1556 in server/server.go

View workflow job for this annotation

GitHub Actions / Lint

S1038: should use fmt.Fprintf instead of fmt.Fprintln(fmt.Sprintf(...)) (but don't forget the newline) (staticcheck)

Check failure on line 1556 in server/server.go

View workflow job for this annotation

GitHub Actions / Lint

S1038: should use fmt.Fprintf instead of fmt.Fprintln(fmt.Sprintf(...)) (but don't forget the newline) (staticcheck)
os.Exit(int(sig + 128))
}
}

// checkTrustedKeyString will check that the string is a valid array
// of public operator nkeys.
func checkTrustedKeyString(keys string) []string {
Expand Down Expand Up @@ -1576,12 +1590,6 @@
return true
}

// PrintAndDie is exported for access in other packages.
func PrintAndDie(msg string) {
fmt.Fprintln(os.Stderr, msg)
os.Exit(1)
}

// PrintServerAndExit will print our version and exit.
func PrintServerAndExit() {
fmt.Printf("nats-server: v%s\n", VERSION)
Expand Down
6 changes: 4 additions & 2 deletions server/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ func (s *Server) handleSignals() {
s.Noticef("Trapped %q signal", sig)
switch sig {
case syscall.SIGINT:
s.shutdownSignal.CompareAndSwap(0, int32(syscall.SIGINT))
s.Shutdown()
s.WaitForShutdown()
os.Exit(0)
os.Exit(128 + int(syscall.SIGINT))
case syscall.SIGTERM:
s.shutdownSignal.CompareAndSwap(0, int32(syscall.SIGTERM))
// Shutdown unless graceful shutdown already in progress.
s.mu.Lock()
ldm := s.ldm
Expand All @@ -61,7 +63,7 @@ func (s *Server) handleSignals() {
if !ldm {
s.Shutdown()
s.WaitForShutdown()
os.Exit(0)
os.Exit(128 + int(syscall.SIGTERM))
}
case syscall.SIGUSR1:
// File log re-open for rotating file logs.
Expand Down
1 change: 1 addition & 0 deletions server/signal_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (s *Server) handleSignals() {
for {
select {
case sig := <-c:
s.shutdownSignal.CompareAndSwap(0, int32(syscall.SIGTERM))
s.Debugf("Trapped %q signal", sig)
s.Shutdown()
os.Exit(0)
Expand Down
Loading