Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

agent: log events and exit code to the log file #3124

Merged
merged 1 commit into from
Jun 8, 2017
Merged
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
33 changes: 22 additions & 11 deletions command/agent/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net"
"os"
"os/signal"
Expand Down Expand Up @@ -45,6 +46,7 @@ type Command struct {
args []string
logFilter *logutils.LevelFilter
logOutput io.Writer
logger *log.Logger
}

// readConfig is responsible for setup of our configuration using
Expand Down Expand Up @@ -664,6 +666,14 @@ func (cmd *Command) registerWatches(config *Config) error {
}

func (cmd *Command) Run(args []string) int {
code := cmd.run(args)
if cmd.logger != nil {
cmd.logger.Println("[INFO] Exit code: ", code)
}
return code
}

func (cmd *Command) run(args []string) int {
cmd.UI = &cli.PrefixedUi{
OutputPrefix: "==> ",
InfoPrefix: " ",
Expand All @@ -690,6 +700,7 @@ func (cmd *Command) Run(args []string) int {
}
cmd.logFilter = logFilter
cmd.logOutput = logOutput
cmd.logger = log.New(logOutput, "", log.LstdFlags)

if err := startupTelemetry(config); err != nil {
cmd.UI.Error(err.Error())
Expand Down Expand Up @@ -769,7 +780,7 @@ func (cmd *Command) Run(args []string) int {
case <-cmd.ShutdownCh:
sig = os.Interrupt
case err := <-agent.RetryJoinCh():
cmd.UI.Error(err.Error())
cmd.logger.Println("[ERR] Retry join failed: ", err)
return 1
case <-agent.ShutdownCh():
// Agent is already down!
Expand All @@ -781,34 +792,34 @@ func (cmd *Command) Run(args []string) int {
continue

case syscall.SIGHUP:
cmd.UI.Output(fmt.Sprintf("Caught signal: %v", sig))
cmd.logger.Println("[INFO] Caught signal: ", sig)

conf, err := cmd.handleReload(agent, config)
if conf != nil {
config = conf
}
if err != nil {
cmd.UI.Error(err.Error())
cmd.logger.Println("[ERR] Reload config failed: ", err)
}
// Send result back if reload was called via HTTP
if reloadErrCh != nil {
reloadErrCh <- err
}

default:
cmd.UI.Output(fmt.Sprintf("Caught signal: %v", sig))
cmd.logger.Println("[INFO] Caught signal: ", sig)

graceful := (sig == os.Interrupt && !(*config.SkipLeaveOnInt)) || (sig == syscall.SIGTERM && (*config.LeaveOnTerm))
if !graceful {
cmd.UI.Output("Graceful shutdown disabled. Exiting")
cmd.logger.Println("[INFO] Graceful shutdown disabled. Exiting")
return 1
}

cmd.UI.Output("Gracefully shutting down agent...")
cmd.logger.Println("[INFO] Gracefully shutting down agent...")
gracefulCh := make(chan struct{})
go func() {
if err := agent.Leave(); err != nil {
cmd.UI.Error(fmt.Sprintf("Error on leave: %s", err))
cmd.logger.Println("[ERR] Error on leave:", err)
return
}
close(gracefulCh)
Expand All @@ -817,13 +828,13 @@ func (cmd *Command) Run(args []string) int {
gracefulTimeout := 15 * time.Second
select {
case <-signalCh:
cmd.UI.Output(fmt.Sprintf("Caught second signal: %v. Exiting", sig))
cmd.logger.Printf("[INFO] Caught second signal %v. Exiting\n", sig)
return 1
case <-time.After(gracefulTimeout):
cmd.UI.Output("Timeout on graceful leave. Exiting")
cmd.logger.Println("[INFO] Timeout on graceful leave. Exiting")
return 1
case <-gracefulCh:
cmd.UI.Output("Graceful exit completed")
cmd.logger.Println("[INFO] Graceful exit completed")
return 0
}
}
Expand All @@ -832,7 +843,7 @@ func (cmd *Command) Run(args []string) int {

// handleReload is invoked when we should reload our configs, e.g. SIGHUP
func (cmd *Command) handleReload(agent *Agent, cfg *Config) (*Config, error) {
cmd.UI.Output("Reloading configuration...")
cmd.logger.Println("[INFO] Reloading configuration...")
var errs error
newCfg := cmd.readConfig()
if newCfg == nil {
Expand Down