forked from mongodb/mongo-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TOOLS-1341 consistent signal handling
- Loading branch information
Showing
13 changed files
with
81 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package signals | ||
|
||
import ( | ||
"github.com/mongodb/mongo-tools/common/log" | ||
"github.com/mongodb/mongo-tools/common/util" | ||
|
||
"os" | ||
"os/signal" | ||
"syscall" | ||
) | ||
|
||
// Handle is like HandleWithInterrupt but it doesn't take a finalizer and will | ||
// exit immediately after the first signal is received. | ||
func Handle() chan struct{} { | ||
return HandleWithInterrupt(nil) | ||
} | ||
|
||
// HandleWithInterrupt starts a goroutine which listens for SIGTERM, SIGINT, | ||
// SIGKILL, and SIGPIPE. It calls the finalizer function when the first signal | ||
// is received and forcibly terminates the program after the second. If a nil | ||
// function is provided, the program will exit after the first signal. | ||
func HandleWithInterrupt(finalizer func()) chan struct{} { | ||
finishedChan := make(chan struct{}) | ||
go handleSignals(finalizer, finishedChan) | ||
return finishedChan | ||
} | ||
|
||
func handleSignals(finalizer func(), finishedChan chan struct{}) { | ||
log.Logv(log.DebugLow, "will listen for SIGTERM, SIGINT, SIGKILL, and SIGPIPE") | ||
sigChan := make(chan os.Signal, 2) | ||
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL, syscall.SIGPIPE) | ||
defer signal.Stop(sigChan) | ||
if finalizer != nil { | ||
select { | ||
case <-sigChan: | ||
// first signal use finalizer to terminate cleanly | ||
log.Logv(log.Always, "signal received; attempting to shut down") | ||
finalizer() | ||
case <-finishedChan: | ||
return | ||
} | ||
} | ||
select { | ||
case <-sigChan: | ||
// second signal exits immediately | ||
log.Logv(log.Always, "signal received; forcefully terminating") | ||
os.Exit(util.ExitKill) | ||
case <-finishedChan: | ||
return | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
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
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
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