-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now forwarding all signals to process group
By doing this, we make sure piping through zap-pretty will still send all signals to the process group ensuring they can do signal handling themselves removing some weird problem killing process can bring.
- Loading branch information
Matthieu Vachon
committed
Jun 15, 2019
1 parent
61cc8e5
commit b72f5a0
Showing
2 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
) | ||
|
||
type signaler struct { | ||
processGroupID int | ||
} | ||
|
||
func NewSignaler() *signaler { | ||
signaler := &signaler{} | ||
|
||
pgid, err := syscall.Getpgid(os.Getpid()) | ||
if err != nil { | ||
signaler.processGroupID = pgid | ||
} else { | ||
debug.Println("[Warning] unable to determine process group, signaling will be broken") | ||
} | ||
|
||
return signaler | ||
} | ||
|
||
func (s *signaler) forwardAllSignalsToProcessGroup() { | ||
signalChan := make(chan os.Signal, 1) | ||
signal.Notify(signalChan) | ||
|
||
for { | ||
signal := <-signalChan | ||
|
||
syscallSignal, isSyscallSignalType := signal.(syscall.Signal) | ||
if s.processGroupID != 0 && isSyscallSignalType { | ||
syscall.Kill(s.processGroupID, syscallSignal) | ||
} | ||
} | ||
} |