github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/pkg/lightning/sigusr1_unix.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  // +build linux darwin freebsd unix
    15  
    16  package lightning
    17  
    18  import (
    19  	"os"
    20  	"os/signal"
    21  	"syscall"
    22  
    23  	"go.uber.org/zap"
    24  
    25  	"github.com/pingcap/br/pkg/lightning/log"
    26  )
    27  
    28  // handleSigUsr1 listens for the SIGUSR1 signal and executes `handler()` every time it is received.
    29  func handleSigUsr1(handler func()) {
    30  	ch := make(chan os.Signal, 1)
    31  	signal.Notify(ch, syscall.SIGUSR1)
    32  	go func() {
    33  		for sig := range ch {
    34  			log.L().Debug("received signal", zap.Stringer("signal", sig))
    35  			handler()
    36  		}
    37  	}()
    38  }