Skip to content

Commit

Permalink
Merge daba4cc into acfe198
Browse files Browse the repository at this point in the history
  • Loading branch information
kozlovic committed Aug 10, 2018
2 parents acfe198 + daba4cc commit e05c7e9
Show file tree
Hide file tree
Showing 61 changed files with 9,579 additions and 12 deletions.
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ NATS Streaming provides the following high-level feature set.
- [Getting Started](#getting-started)
* [Building](#building)
* [Running](#running)
* [Process Signaling](#process-signaling)
* [Windows Service](#windows-service)
* [Embedding NATS Streaming](#embedding-nats-streaming)
- [Configuring](#configuring)
* [Command line arguments](#command-line-arguments)
Expand Down Expand Up @@ -1118,6 +1120,64 @@ The server will be started and listening for client connections on port 4222 (th

Note that you do not need to start the embedded NATS Server. It is started automatically when you run the NATS Streaming Server. See below for details on how you secure the embedded NATS Server.

## Process Signaling

On Unix systems, the NATS Streaming Server responds to the following signals:

| Signal | Result |
| --------------- | ------------------------------------- |
| SIGKILL | Kills the process immediately |
| SIGINT, SIGTERM | Stops the server gracefully |
| SIGUSR1 | Reopens the log file for log rotation |

The `nats-streaming-server` binary can be used to send these signals to running NATS Streaming Servers using the `-sl` flag:

```sh
# Reopen log file for log rotation
nats-streaming-server -sl reopen

# Stop the server
nats-streaming-server -sl quit
```

If there are multiple `nats-streaming-server` processes running, specify a PID:

```sh
nats-streaming-server -sl quit=<pid>
```

See the [Windows Service](#windows-service) section for information on signaling the NATS Streaming Server on Windows.

## Windows Service

The NATS Streaming Server supports running as a Windows service. There is currently no installer and instead users should use `sc.exe` to install the service:

Here is how to create and start a NATS Streaming Server named `nats-streaming-server`. Note that the server flags should be passed in when creating the service.
```sh
sc.exe create nats-streaming-server binPath="\"<streaming server path>\nats-streaming-server.exe\" [NATS Streaming flags]"
sc.exe start nats-streaming-server
```

You can create several instances, giving it a unique name. For instance, this is how you would create two services, named `nss1` and `nss2`, each one with its own set of parameters.
```
sc.exe create nss1 binPath="\"c:\nats-io\nats-streaming\nats-streaming-server.exe\" --syslog --syslog_name=nss1 -p 4222"
sc.exe create nss2 binPath="\"c:\nats-io\nats-streaming\nats-streaming-server.exe\" --syslog --syslog_name=nss2 -p 4223"
```
By default, wif no logfile is specified, the server will use the system log. The default event source name is `NATS-Streaming-Server`.
However, you can specify the name you want, which is especially useful when installing more than one service as described above.

Once the service is running, it can be controlled using `sc.exe` or `nats-streaming-server.exe -sl`:

```batch
REM Stop the server
nats-streaming-server.exe -sl quit
```
The above commands will default to controlling the service named `nats-streaming-server`. If the service has another name, it can be specified like this:
```batch
nats-streaming-server.exe -sl quit=<service name>
```

## Embedding NATS Streaming

Embedding a NATS Streaming Server in your own code is easy. Simply import:
Expand Down
7 changes: 5 additions & 2 deletions nats-streaming-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Streaming Server Options:
-hbt, --hb_timeout <duration> How long server waits for a heartbeat response
-hbf, --hb_fail_count <int> Number of failed heartbeats before server closes the client connection
--ft_group <string> Name of the FT Group. A group can be 2 or more servers with a single active server and all sharing the same datastore.
-sl, --signal <signal>[=<pid>] Send signal to nats-streaming-server process (stop, quit, reopen)
Streaming Server Clustering Options:
--clustered <bool> Run the server in a clustered configuration (default: false)
Expand Down Expand Up @@ -94,6 +95,7 @@ Streaming Server Logging Options:
-SD, --stan_debug=<bool> Enable STAN debugging output
-SV, --stan_trace=<bool> Trace the raw STAN protocol
-SDV Debug and trace STAN
--syslog_name On Windows, when running several servers as a service, use this name for the event source
(See additional NATS logging options below)
Embedded NATS Server Options:
Expand Down Expand Up @@ -150,7 +152,8 @@ func main() {
nOpts.NoSigs = true
// Without this option set to true, the logger is not configured.
sOpts.EnableLogging = true
if _, err := stand.RunServerWithOpts(sOpts, nOpts); err != nil {
// This will invoke RunServerWithOpts but on Windows, may run it as a service.
if _, err := stand.Run(sOpts, nOpts); err != nil {
fmt.Println(err)
os.Exit(1)
}
Expand All @@ -169,7 +172,7 @@ func parseFlags() (*stand.Options, *natsd.Options) {
fs.Usage,
natsd.PrintTLSHelpAndDie)
if err != nil {
natsd.PrintAndDie(err.Error() + "\n" + usageStr)
natsd.PrintAndDie(err.Error())
}
return stanOpts, natsOpts
}
6 changes: 6 additions & 0 deletions server/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ func ProcessConfigFile(configFile string, opts *Options) error {
if err := parseCluster(v, opts); err != nil {
return err
}
case "syslog_name":
if err := checkType(k, reflect.String, v); err != nil {
return err
}
opts.SyslogName = v.(string)
}
}
return nil
Expand Down Expand Up @@ -570,6 +575,7 @@ func ConfigureOptions(fs *flag.FlagSet, args []string, printVersion, printHelp,
defSQLOpts := stores.DefaultSQLStoreOptions()
fs.BoolVar(&sopts.SQLStoreOpts.NoCaching, "sql_no_caching", defSQLOpts.NoCaching, "Enable/Disable caching")
fs.IntVar(&sopts.SQLStoreOpts.MaxOpenConns, "sql_max_open_conns", defSQLOpts.MaxOpenConns, "Max opened connections to the database")
fs.StringVar(&sopts.SyslogName, "syslog_name", "", "Syslog Name")

// First, we need to call NATS's ConfigureOptions() with above flag set.
// It will be augmented with NATS specific flags and call fs.Parse(args) for us.
Expand Down
1 change: 1 addition & 0 deletions server/conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ func TestParseWrongTypes(t *testing.T) {
expectFailureFor(t, "hb_fail_count: false", wrongTypeErr)
expectFailureFor(t, "ft_group: 123", wrongTypeErr)
expectFailureFor(t, "partitioning: 123", wrongTypeErr)
expectFailureFor(t, "syslog_name: 123", wrongTypeErr)
expectFailureFor(t, "store_limits:{max_channels:false}", wrongTypeErr)
expectFailureFor(t, "store_limits:{max_msgs:false}", wrongTypeErr)
expectFailureFor(t, "store_limits:{max_bytes:false}", wrongTypeErr)
Expand Down
17 changes: 15 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import (
// Server defaults.
const (
// VERSION is the current version for the NATS Streaming server.
VERSION = "0.10.3"
VERSION = "0.11.0"

DefaultClusterID = "test-cluster"
DefaultDiscoverPrefix = "_STAN.discover"
Expand Down Expand Up @@ -1117,6 +1117,7 @@ type Options struct {
ClientHBFailCount int // Number of failed heartbeats before server closes client connection.
FTGroupName string // Name of the FT Group. A group can be 2 or more servers with a single active server and all sharing the same datastore.
Partitioning bool // Specify if server only accepts messages/subscriptions on channels defined in StoreLimits.
SyslogName string // Optional name for the syslog (usueful on Windows when running several servers as a service)
Clustering ClusteringOptions
}

Expand Down Expand Up @@ -1547,11 +1548,23 @@ func (s *StanServer) configureLogger() {
enableDebug := nOpts.Debug || sOpts.Debug
enableTrace := nOpts.Trace || sOpts.Trace

syslog := nOpts.Syslog
if isWindowsService() && nOpts.LogFile == "" {
// Enable syslog if no log file is specified and we're running as a
// Windows service so that logs are written to the Windows event log.
syslog = true
}
// If we have a syslog name specified, make sure we will use this name.
// This is for syslog and remote syslogs running on Windows.
if sOpts.SyslogName != "" {
natsdLogger.SetSyslogName(sOpts.SyslogName)
}

if nOpts.LogFile != "" {
newLogger = natsdLogger.NewFileLogger(nOpts.LogFile, nOpts.Logtime, enableDebug, enableTrace, true)
} else if nOpts.RemoteSyslog != "" {
newLogger = natsdLogger.NewRemoteSysLogger(nOpts.RemoteSyslog, enableDebug, enableTrace)
} else if nOpts.Syslog {
} else if syslog {
newLogger = natsdLogger.NewSysLogger(enableDebug, enableTrace)
} else {
colors := true
Expand Down
31 changes: 31 additions & 0 deletions server/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2018 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build !windows

package server

import (
natsd "github.com/nats-io/gnatsd/server"
)

// Run starts the NATS Streaming server. This wrapper function allows Windows to add a
// hook for running NATS Streaming as a service.
func Run(sOpts *Options, nOpts *natsd.Options) (*StanServer, error) {
return RunServerWithOpts(sOpts, nOpts)
}

// isWindowsService indicates if NATS Streaming is running as a Windows service.
func isWindowsService() bool {
return false
}
49 changes: 49 additions & 0 deletions server/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2018 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build !windows

package server

import (
"testing"
"time"
)

func TestRun(t *testing.T) {
sOpts := GetDefaultOptions()
srvCh := make(chan *StanServer, 1)
errCh := make(chan error, 1)
go func() {
s, err := Run(sOpts, nil)
srvCh <- s
errCh <- err
}()
select {
case err := <-errCh:
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
case <-time.After(2 * time.Second):
t.Fatalf("Failed to start")
}

s := <-srvCh
if s == nil {
t.Fatalf("Server is nil")
}
defer s.Shutdown()
if s.State() != Standalone {
t.Fatalf("Unexpected sate: %v", s.State().String())
}
}

0 comments on commit e05c7e9

Please sign in to comment.