Skip to content

Commit

Permalink
refactor: refactor signal handling across platforms
Browse files Browse the repository at this point in the history
- Refactor signal handling in `manager.go` to use a slice of signals instead of individual signal constants.
- Remove handling of `SIGTSTP` signal and associated logging in `manager.go`.
- Update signal sending in tests within `manager_test.go` to use `os.FindProcess` and `process.Signal` instead of `syscall.Kill`.
- Add `os` package import in `manager_test.go`.
- Create new file `signals_unix.go` with a slice of signals for Unix-based systems including `SIGINT`, `SIGTERM`, and `SIGTSTP`.
- Create new file `signals_windows.go` with a slice of signals for Windows systems including `SIGINT` and `SIGTERM`.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Mar 13, 2024
1 parent e32a590 commit 6379155
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
6 changes: 1 addition & 5 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ func (g *Manager) handleSignals(ctx context.Context) {

signal.Notify(
c,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGTSTP,
signals...,
)
defer signal.Stop(c)

Expand All @@ -87,8 +85,6 @@ func (g *Manager) handleSignals(ctx context.Context) {
g.logger.Infof("PID %d. Received SIGTERM. Shutting down...", pid)
g.doGracefulShutdown()
return
case syscall.SIGTSTP:
g.logger.Info("PID %d. Received SIGTSTP.", pid)
default:
g.logger.Infof("PID %d. Received %v.", pid, sig)
}
Expand Down
17 changes: 13 additions & 4 deletions manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package graceful
import (
"context"
"errors"
"os"
"sync"
"sync/atomic"
"syscall"
Expand Down Expand Up @@ -240,8 +241,12 @@ func TestWithSignalSIGINT(t *testing.T) {

go func() {
time.Sleep(50 * time.Millisecond)
if err := syscall.Kill(syscall.Getpid(), syscall.SIGINT); err != nil {
t.Errorf("syscall error: %v", err)
process, err := os.FindProcess(syscall.Getpid())
if err != nil {
t.Errorf("os.FindProcess error: %v", err)
}
if err := process.Signal(syscall.SIGINT); err != nil {
t.Errorf("process.Signal error: %v", err)
}
}()

Expand Down Expand Up @@ -270,8 +275,12 @@ func TestWithSignalSIGTERM(t *testing.T) {

go func() {
time.Sleep(50 * time.Millisecond)
if err := syscall.Kill(syscall.Getpid(), syscall.SIGTERM); err != nil {
t.Errorf("syscall error: %v", err)
process, err := os.FindProcess(syscall.Getpid())
if err != nil {
t.Errorf("os.FindProcess error: %v", err)
}
if err := process.Signal(syscall.SIGTERM); err != nil {
t.Errorf("process.Signal error: %v", err)
}
}()

Expand Down
11 changes: 11 additions & 0 deletions signals_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build linux || bsd || darwin
// +build linux bsd darwin

package graceful

import (
"os"
"syscall"
)

var signals = []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGTSTP}
11 changes: 11 additions & 0 deletions signals_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build windows
// +build windows

package graceful

import (
"os"
"syscall"
)

var signals = []os.Signal{syscall.SIGINT, syscall.SIGTERM}

0 comments on commit 6379155

Please sign in to comment.