github.com/wfusion/gofusion@v1.1.14/common/infra/asynq/signals_unix.go (about)

     1  //go:build linux || bsd || darwin
     2  // +build linux bsd darwin
     3  
     4  package asynq
     5  
     6  import (
     7  	"os"
     8  	"os/signal"
     9  
    10  	"golang.org/x/sys/unix"
    11  )
    12  
    13  // waitForSignals waits for signals and handles them.
    14  // It handles SIGTERM, SIGINT, and SIGTSTP.
    15  // SIGTERM and SIGINT will signal the process to exit.
    16  // SIGTSTP will signal the process to stop processing new tasks.
    17  func (srv *Server) waitForSignals() {
    18  	srv.logger.Info("[Common] asynq send signal TSTP to stop processing new tasks")
    19  	srv.logger.Info("[Common] asynq send signal TERM or INT to terminate the process")
    20  
    21  	sigs := make(chan os.Signal, 1)
    22  	signal.Notify(sigs, unix.SIGTERM, unix.SIGINT, unix.SIGTSTP)
    23  	for {
    24  		sig := <-sigs
    25  		if sig == unix.SIGTSTP {
    26  			srv.Stop()
    27  			continue
    28  		}
    29  		break
    30  	}
    31  }
    32  
    33  func (s *Scheduler) waitForSignals() {
    34  	s.logger.Info("[Common] asynq send signal TERM or INT to stop the scheduler")
    35  	sigs := make(chan os.Signal, 1)
    36  	signal.Notify(sigs, unix.SIGTERM, unix.SIGINT)
    37  	<-sigs
    38  }