aboutsummaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-10-27 11:46:10 +0200
committerGitHub <noreply@github.com>2018-10-27 11:46:10 +0200
commit8be1078dd6400765a9b9202a81c40ede3ee01981 (patch)
tree7d2d08b01bac008fcd3f6af8ff1e83c6593640e5 /util
parente79476a9627226de3dee80e9a19b7f66c4ef68ff (diff)
parent31cbddd35b15519a2f8ffa8d93e6456323ea80ee (diff)
downloadgit-bug-8be1078dd6400765a9b9202a81c40ede3ee01981.tar.gz
Merge pull request #71 from auyer/master
Cleaning lock files on Interruption
Diffstat (limited to 'util')
-rw-r--r--util/interrupt/cleaner.go49
-rw-r--r--util/interrupt/cleaner_test.go50
2 files changed, 99 insertions, 0 deletions
diff --git a/util/interrupt/cleaner.go b/util/interrupt/cleaner.go
new file mode 100644
index 00000000..76c9d04d
--- /dev/null
+++ b/util/interrupt/cleaner.go
@@ -0,0 +1,49 @@
+package interrupt
+
+import (
+ "fmt"
+ "os"
+ "os/signal"
+ "syscall"
+)
+
+// Cleaner type referes to a function with no inputs that returns an error
+type Cleaner func() error
+
+var cleaners []Cleaner
+var active = false
+
+// RegisterCleaner is responsible for regisreting a cleaner function. When a function is registered, the Signal watcher is started in a goroutine.
+func RegisterCleaner(f ...Cleaner) {
+ for _, fn := range f {
+ cleaners = append([]Cleaner{fn}, cleaners...)
+ if !active {
+ active = true
+ go func() {
+ ch := make(chan os.Signal, 1)
+ signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
+ <-ch
+ // Prevent un-terminated ^C character in terminal
+ fmt.Println()
+ fmt.Println("Cleaning")
+ errl := Clean()
+ for _, err := range errl {
+ fmt.Println(err)
+ }
+ os.Exit(1)
+ }()
+ }
+ }
+}
+
+// Clean invokes all registered cleanup functions, and returns a list of errors, if they exist.
+func Clean() (errorlist []error) {
+ for _, f := range cleaners {
+ err := f()
+ if err != nil {
+ errorlist = append(errorlist, err)
+ }
+ }
+ cleaners = []Cleaner{}
+ return
+}
diff --git a/util/interrupt/cleaner_test.go b/util/interrupt/cleaner_test.go
new file mode 100644
index 00000000..c4e5c9b3
--- /dev/null
+++ b/util/interrupt/cleaner_test.go
@@ -0,0 +1,50 @@
+package interrupt
+
+import (
+ "errors"
+ "testing"
+)
+
+// TestRegisterAndErrorAtCleaning tests if the registered order was kept by checking the returned errors
+func TestRegisterAndErrorAtCleaning(t *testing.T) {
+ active = true // this prevents goroutine from being started during the tests
+
+ f := func() error {
+ return errors.New("X")
+ }
+ f2 := func() error {
+ return errors.New("Y")
+ }
+ f3 := func() error {
+ return nil
+ }
+ RegisterCleaner(f)
+ RegisterCleaner(f2, f3)
+ // count := 0
+
+ errl := Clean()
+ if len(errl) != 2 {
+ t.Fatalf("unexpected error count")
+ }
+ if errl[0].Error() != "Y" && errl[1].Error() != "X" {
+ t.Fatalf("unexpected error order")
+
+ }
+}
+
+func TestRegisterAndClean(t *testing.T) {
+ active = true // this prevents goroutine from being started during the tests
+
+ f := func() error {
+ return nil
+ }
+ f2 := func() error {
+ return nil
+ }
+ RegisterCleaner(f, f2)
+
+ errl := Clean()
+ if len(errl) != 0 {
+ t.Fatalf("unexpected error count")
+ }
+}