Skip to content

Commit

Permalink
Update service and syslog code to use variable instead of constant
Browse files Browse the repository at this point in the history
for the process name and service name. This allows the reuse of this
code in NATS Streaming Server by invoking the new setters to
change the service and process names.

Signed-off-by: Ivan Kozlovic <[email protected]>
  • Loading branch information
kozlovic committed Aug 16, 2018
1 parent a2b5acf commit 2153a45
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
4 changes: 4 additions & 0 deletions logger/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ type SysLogger struct {
trace bool
}

// SetSyslogName sets the name to use for the syslog.
// Currently used only on Windows.
func SetSyslogName(name string) {}

// GetSysLoggerTag generates the tag name for use in syslog statements. If
// the executable is linked, the name of the link will be used as the tag,
// otherwise, the name of the executable is used. "gnatsd" is the default
Expand Down
9 changes: 6 additions & 3 deletions logger/syslog_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ import (
"golang.org/x/sys/windows/svc/eventlog"
)

const (
natsEventSource = "NATS-Server"
)
var natsEventSource = "NATS-Server"

// SetSyslogName sets the name to use for the system log event source
func SetSyslogName(name string) {
natsEventSource = name
}

// SysLogger logs to the windows event logger
type SysLogger struct {
Expand Down
2 changes: 1 addition & 1 deletion server/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var (

const (
// VERSION is the current version for the server.
VERSION = "1.2.0"
VERSION = "1.3.0"

// PROTO is the currently supported protocol.
// 0 was the original
Expand Down
8 changes: 7 additions & 1 deletion server/service_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ import (
)

const (
serviceName = "gnatsd"
reopenLogCode = 128
reopenLogCmd = svc.Cmd(reopenLogCode)
acceptReopenLog = svc.Accepted(reopenLogCode)
)

var serviceName = "gnatsd"

// SetServiceName allows setting a different service name
func SetServiceName(name string) {
serviceName = name
}

// winServiceWrapper implements the svc.Handler interface for implementing
// gnatsd as a Windows service.
type winServiceWrapper struct {
Expand Down
11 changes: 8 additions & 3 deletions server/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ import (
"syscall"
)

const processName = "gnatsd"
var processName = "gnatsd"

// SetProcessName allows to change the expected name of the process.
func SetProcessName(name string) {
processName = name
}

// Signal Handling
func (s *Server) handleSignals() {
Expand Down Expand Up @@ -76,10 +81,10 @@ func ProcessSignal(command Command, pidStr string) error {
return err
}
if len(pids) == 0 {
return errors.New("no gnatsd processes running")
return fmt.Errorf("no %s processes running", processName)
}
if len(pids) > 1 {
errStr := "multiple gnatsd processes running:\n"
errStr := fmt.Sprintf("multiple %s processes running:\n", processName)
prefix := ""
for _, p := range pids {
errStr += fmt.Sprintf("%s%d", prefix, p)
Expand Down

0 comments on commit 2153a45

Please sign in to comment.