-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mage: cancel context on SIGINT (#313)
* mage: cancel context on SIGINT On receiving an interrupt signal, mage cancels the context allowing the magefile to perform any cleanup before exiting. A second interrupt signal will kill the magefile process without delay. The behaviour for a timeout remains unchanged (context is cancelled and the magefile exits). * mage: add cleanup timeout to cancel Co-authored-by: Nate Finch <nate.finch@gmail.com>
- Loading branch information
1 parent
300bbc8
commit a920604
Showing
6 changed files
with
203 additions
and
14 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 |
---|---|---|
|
@@ -9,6 +9,7 @@ jobs: | |
fail-fast: false | ||
matrix: | ||
go-version: | ||
- 1.18.x | ||
- 1.17.x | ||
- 1.16.x | ||
- 1.15.x | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//+build mage | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
) | ||
|
||
// Exits after receiving SIGHUP | ||
func ExitsAfterSighup(ctx context.Context) { | ||
sigC := make(chan os.Signal, 1) | ||
signal.Notify(sigC, syscall.SIGHUP) | ||
<-sigC | ||
fmt.Println("received sighup") | ||
} | ||
|
||
// Exits after SIGINT and wait | ||
func ExitsAfterSigint(ctx context.Context) { | ||
sigC := make(chan os.Signal, 1) | ||
signal.Notify(sigC, syscall.SIGINT) | ||
<-sigC | ||
fmt.Printf("exiting...") | ||
time.Sleep(200 * time.Millisecond) | ||
fmt.Println("done") | ||
} | ||
|
||
// Exits after ctx cancel and wait | ||
func ExitsAfterCancel(ctx context.Context) { | ||
defer func() { | ||
fmt.Println("deferred cleanup") | ||
}() | ||
<-ctx.Done() | ||
fmt.Printf("exiting...") | ||
time.Sleep(200 * time.Millisecond) | ||
fmt.Println("done") | ||
} | ||
|
||
// Ignores all signals, requires killing via timeout or second SIGINT | ||
func IgnoresSignals(ctx context.Context) { | ||
sigC := make(chan os.Signal, 1) | ||
signal.Notify(sigC, syscall.SIGINT) | ||
for { | ||
<-sigC | ||
} | ||
} |
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