-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- to ensure that nix-cli processes get SIGTERM when morph receives SIGTERM or SIGINT - to prepare for better cleanup of tmp-directories in morph in the future
- Loading branch information
1 parent
e4f497f
commit 00fa9fa
Showing
3 changed files
with
70 additions
and
3 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
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,52 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
) | ||
|
||
type finalizer struct { | ||
function FinalizerFunc | ||
executed bool | ||
} | ||
type FinalizerFunc func() | ||
|
||
var finalizers []*finalizer | ||
|
||
/* | ||
Finalizers run sequentially at morph shutdown - both at clean shutdown and on errors. | ||
Finalizers should be quick, simple and they need to ignore errors and don't panic. | ||
Each finalizer will only run once and will _never_ be re-invoked. | ||
*/ | ||
func (f *finalizer) Run() { | ||
if !f.executed { | ||
f.executed = true | ||
f.function() | ||
} | ||
} | ||
|
||
func RunFinalizers() { | ||
for _, f := range finalizers { | ||
f.Run() | ||
} | ||
} | ||
|
||
func AddFinalizer(f FinalizerFunc) { | ||
finalizers = append(finalizers, &finalizer{ | ||
function: f, | ||
executed: false, | ||
}) | ||
} | ||
|
||
func SignalHandler() { | ||
sigs := make(chan os.Signal, 1) | ||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) | ||
go func() { | ||
sig := <-sigs | ||
fmt.Fprintf(os.Stderr, "Received signal: %s\n", sig.String()) | ||
RunFinalizers() | ||
os.Exit(130) // reserved exit code for "Interrupted" | ||
}() | ||
} |