Repository: hashicorp/nomad
File: command/agent/command.go (Go)

Code before:
		config.Client.ServerJoin != nil &&
		len(config.Client.ServerJoin.RetryJoin) != 0 {
		joiner := retryJoiner{
			autoDiscover: autoDiscover{goDiscover: &discover.Discover{}, netAddrs: &netAddrs{}},
			errCh:        c.retryJoinErrCh,
			joinCfg:      config.Client.ServerJoin,
			joinFunc:     c.agent.client.SetServers,
			logger:       c.agent.logger.Named("joiner").With("agent_mode", "client"),
		}

		if err := joiner.Validate(config); err != nil {
			return err
		}

		go joiner.RetryJoin()
	}

	return nil
}

// These constants are for readiness signalling via the systemd notify protocol.
// The functions we send these messages to are no-op on non-Linux systems. See
// also https://www.man7.org/linux/man-pages/man3/sd_notify.3.html
const (
	sdReady     = "READY=1"
	sdReloading = "RELOADING=1\nMONOTONIC_USEC=%d"
	sdStopping  = "STOPPING=1"
)

// handleSignals blocks until we get an exit-causing signal
func (c *Command) handleSignals() int {
	signalCh := make(chan os.Signal, 4)
	signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGPIPE)

	// Signal readiness only once signal handlers are setup
	sdSock, err := openNotify()
	if err != nil {
		c.agent.logger.Debug("notify socket could not be accessed", "error", err)
	}
	if sdSock != nil {
		defer sdSock.Close()
	}
	sdNotify(sdSock, sdReady)

	// Wait for a signal
WAIT:
	var sig os.Signal
	select {
	case s := <-signalCh:
		sig = s
	case <-winsvc.ShutdownChannel():

Review comment (suggestion):
The python-equivalent from the systemd docs at [sd_notify](https://www.freedesktop.org/software/systemd/man/latest/sd_notify.html) is:

```python
def notify_reloading():
    microsecs = time.clock_gettime_ns(time.CLOCK_MONOTONIC) // 1000
    notify(f"RELOADING=1\nMONOTONIC_USEC={microsecs}".encode())
```

Code after:
		len(config.Client.ServerJoin.RetryJoin) != 0 {
		joiner := retryJoiner{
			autoDiscover: autoDiscover{goDiscover: &discover.Discover{}, netAddrs: &netAddrs{}},
			errCh:        c.retryJoinErrCh,
			joinCfg:      config.Client.ServerJoin,
			joinFunc:     c.agent.client.SetServers,
			logger:       c.agent.logger.Named("joiner").With("agent_mode", "client"),
		}

		if err := joiner.Validate(config); err != nil {
			return err
		}

		go joiner.RetryJoin()
	}

	return nil
}

// handleSignals blocks until we get an exit-causing signal
func (c *Command) handleSignals() int {
	signalCh := make(chan os.Signal, 4)
	signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGPIPE)

	// Signal readiness only once signal handlers are setup
	sdSock, err := openNotify()
	if err != nil {
		c.agent.logger.Debug("notify socket could not be accessed", "error", err)
	}
	if sdSock != nil {
		defer sdSock.Close()
	}
	sdNotify(sdSock, sdReady)

	// Wait for a signal
WAIT:
	var sig os.Signal
	select {
	case s := <-signalCh:
		sig = s
	case <-winsvc.ShutdownChannel():
		sig = os.Interrupt
	case <-c.ShutdownCh:
		sig = os.Interrupt
	case <-c.retryJoinErrCh:
		return 1
	}

	// Skip any SIGPIPE signal and don't try to log it (See issues #1798, #3554)
	if sig == syscall.SIGPIPE {
		goto WAIT