...

Source file src/github.com/sassoftware/relic/cmdline/servecmd/signals_unix.go

Documentation: github.com/sassoftware/relic/cmdline/servecmd

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

View as plain text