2
0
mirror of https://github.com/hibiken/asynq.git synced 2025-10-21 21:46:12 +08:00
Files
asynq/signals_unix.go
herb d1e5e4f2f1 https://github.com/hibiken/asynq/issues/439
Allow a server instance to be gracefully shutdown programmatically
2022-04-22 13:38:46 -07:00

47 lines
1.1 KiB
Go

//go:build linux || bsd || darwin
// +build linux bsd darwin
package asynq
import (
"os"
"os/signal"
"golang.org/x/sys/unix"
)
// waitForSignals waits for signals and handles them.
// It handles SIGTERM, SIGINT, and SIGTSTP.
// SIGTERM and SIGINT will signal the process to exit.
// SIGTSTP will signal the process to stop processing new tasks.
func (srv *Server) waitForSignals() {
srv.logger.Info("Send signal TSTP to stop processing new tasks")
srv.logger.Info("Send signal TERM or INT to terminate the process")
signal.Notify(srv.sigs, os.Interrupt, unix.SIGTERM, unix.SIGINT, unix.SIGTSTP)
for {
sig := <-sigs
if sig == unix.SIGTSTP {
srv.Stop()
continue
}
break
}
}
// SignalShutdown stops and shuts down the server.
func (srv *Server) SignalShutdown() {
srv.sigs <- os.Interrupt
}
func (s *Scheduler) waitForSignals() {
s.logger.Info("Send signal TERM or INT to stop the scheduler")
signal.Notify(s.sigs, os.Interrupt, unix.SIGTERM, unix.SIGINT)
<-s.sigs
}
// SignalShutdown stops and shuts down the scheduler.
func (s *Scheduler) SignalShutdown() {
s.sigs <- os.Interrupt
}