Start the RPC server with the agent

This commit is contained in:
Armon Dadgar 2013-12-30 15:27:41 -08:00
parent f4692b468f
commit 0aa4a54ada
1 changed files with 19 additions and 2 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/hashicorp/logutils" "github.com/hashicorp/logutils"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
"io" "io"
"net"
"os" "os"
"os/signal" "os/signal"
"strings" "strings"
@ -26,6 +27,7 @@ type Command struct {
args []string args []string
logFilter *logutils.LevelFilter logFilter *logutils.LevelFilter
agent *Agent agent *Agent
rpcServer *AgentRPC
httpServer *HTTPServer httpServer *HTTPServer
} }
@ -113,7 +115,7 @@ func (c *Command) setupLoggers(config *Config) (*GatedWriter, *logWriter, io.Wri
} }
// setupAgent is used to start the agent and various interfaces // setupAgent is used to start the agent and various interfaces
func (c *Command) setupAgent(config *Config, logOutput io.Writer) error { func (c *Command) setupAgent(config *Config, logOutput io.Writer, logWriter *logWriter) error {
c.Ui.Output("Starting Consul agent...") c.Ui.Output("Starting Consul agent...")
agent, err := Create(config, logOutput) agent, err := Create(config, logOutput)
if err != nil { if err != nil {
@ -122,6 +124,18 @@ func (c *Command) setupAgent(config *Config, logOutput io.Writer) error {
} }
c.agent = agent c.agent = agent
// Setup the RPC listener
rpcListener, err := net.Listen("tcp", config.RPCAddr)
if err != nil {
agent.Shutdown()
c.Ui.Error(fmt.Sprintf("Error starting RPC listener: %s", err))
return err
}
// Start the IPC layer
c.Ui.Output("Starting Serf agent RPC...")
c.rpcServer = NewAgentRPC(agent, rpcListener, logOutput, logWriter)
if config.HTTPAddr != "" { if config.HTTPAddr != "" {
server, err := NewServer(agent, logOutput, config.HTTPAddr) server, err := NewServer(agent, logOutput, config.HTTPAddr)
if err != nil { if err != nil {
@ -157,10 +171,13 @@ func (c *Command) Run(args []string) int {
} }
// Create the agent // Create the agent
if err := c.setupAgent(config, logOutput); err != nil { if err := c.setupAgent(config, logOutput, logWriter); err != nil {
return 1 return 1
} }
defer c.agent.Shutdown() defer c.agent.Shutdown()
if c.rpcServer != nil {
defer c.rpcServer.Shutdown()
}
if c.httpServer != nil { if c.httpServer != nil {
defer c.httpServer.Shutdown() defer c.httpServer.Shutdown()
} }