agent

package
v0.0.0-...-b801540 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2018 License: BSD-2-Clause Imports: 13 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Cmd = &command.Command{
	Name: cmdName,

	Cobra: &cobra.Command{
		Use:          cmdName,
		Short:        "Run " + buildtime.PROGNAME,
		SilenceUsage: true,

		PreRunE: func(cmd *cobra.Command, args []string) error {
			return nil
		},

		RunE: func(cmd *cobra.Command, args []string) error {
			log.Info().Str("command", "run").Msg("")

			// 1. Parse config and construct agent
			var config agent.Config
			err := viper.Unmarshal(&config)
			if err != nil {
				return errors.Wrapf(err, "unable to decode config into struct")
			}

			a, err := agent.New(config)
			if err != nil {
				return errors.Wrapf(err, "unable to create a new %s agent", buildtime.PROGNAME)
			}

			if err := a.Start(); err != nil {
				return errors.Wrapf(err, "unable to start agent")
			}

			signalCh := make(chan os.Signal, 10)
			signal.Notify(signalCh, os.Interrupt, unix.SIGTERM, unix.SIGPIPE)

			for {
				var sig os.Signal
				select {
				case s := <-signalCh:
					sig = s
				}

				switch sig {
				case syscall.SIGPIPE:
					continue

				default:
					log.Info().Str("signal", sig.String()).Msg("caught signal")

					log.Info().Msg("initiating graceful shutdown of agent")
					gracefulCh := make(chan struct{})
					go func() {
						if err := a.Shutdown(); err != nil {
							log.Fatal().Err(err).Msg("error during agent shutdown")
							return
						}
						close(gracefulCh)
					}()

					gracefulTimeout := 15 * time.Second
					select {
					case <-signalCh:
						log.Info().Str("signal", sig.String()).Msg("caught second signal, exiting")
						os.Exit(1)
					case <-time.After(gracefulTimeout):
						log.Info().Dur("timeout", gracefulTimeout).Msg("timeout on graceful shutdown, exiting")
						os.Exit(1)
					case <-gracefulCh:
						log.Info().Msg("graceful shutdown complete")
						os.Exit(0)
					}
				}
			}
		},
	},

	Setup: func(parent *command.Command) error {
		if err := db.SetDefaultViperOptions(); err != nil {
			return err
		}

		if err := setAgentDefaultViperOptions(); err != nil {
			return err
		}

		return nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL