| 1 | //go:build !windows |
| 2 | // +build !windows |
| 3 | |
| 4 | // |
| 5 | // Copyright (c) SAS Institute Inc. |
| 6 | // |
| 7 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | // you may not use this file except in compliance with the License. |
| 9 | // You may obtain a copy of the License at |
| 10 | // |
| 11 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | // |
| 13 | // Unless required by applicable law or agreed to in writing, software |
| 14 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | // See the License for the specific language governing permissions and |
| 17 | // limitations under the License. |
| 18 | // |
| 19 | |
| 20 | package servecmd |
| 21 | |
| 22 | import ( |
| 23 | "os" |
| 24 | "os/signal" |
| 25 | "syscall" |
| 26 | |
| 27 | "github.com/rs/zerolog/log" |
| 28 | "github.com/sassoftware/relic/v7/server/daemon" |
| 29 | ) |
| 30 | |
| 31 | func watchSignals(srv *daemon.Daemon) { |
| 32 | ch := make(chan os.Signal, 4) |
| 33 | signal.Notify( |
| 34 | ch, |
| 35 | syscall.SIGINT, |
| 36 | syscall.SIGTERM, |
| 37 | syscall.SIGQUIT, |
| 38 | syscall.SIGUSR1, |
| 39 | syscall.SIGUSR2, |
| 40 | ) |
| 41 | already := false |
| 42 | for { |
| 43 | sig := <-ch |
| 44 | switch { |
| 45 | case sig == syscall.SIGUSR1: |
| 46 | // no longer used |
| 47 | case !already: |
| 48 | log.Info().Stringer("signal", sig).Msg("initiating graceful shutdown") |
| 49 | go func() { |
| 50 | if err := srv.Close(); err != nil { |
| 51 | log.Err(err).Msg("failed to shutdown gracefully") |
| 52 | } |
| 53 | }() |
| 54 | already = true |
| 55 | default: |
| 56 | log.Warn().Stringer("signal", sig).Msg("shutting down immediately") |
| 57 | os.Exit(0) |
| 58 | } |
| 59 | } |
| 60 | } |