Skip to content

Commit

Permalink
better cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
brentp committed Nov 5, 2016
1 parent d6c6d6c commit 341987a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
+ read `GARGS_PROCESS_BUFFER` to let user set size of data before a tempfile is used.
+ read `GARGS_WAIT_MULTIPLIER` to determine how many finished processes will wait for single slow processes
higher values improve concurrency at the expense of memory.
+ better cleanup of tmp files in case of process halt.

0.3.4
=====
Expand Down
43 changes: 43 additions & 0 deletions process/cleanup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package process

import (
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"syscall"
)

// Cleanup is a best-effort to remove all temporary files
// created by process. Users can call it manually to remove them.
func Cleanup() {
matches, err := filepath.Glob(filepath.Join(os.TempDir(), fmt.Sprintf("%s.*", prefix)))
if err != nil {
log.Fatal(err)
}
for _, f := range matches {
os.Remove(f)
}
}

func init() {
c := make(chan os.Signal, 1)
signal.Notify(c,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGHUP,
syscall.SIGQUIT)
go func() {
s := <-c
Cleanup()
fmt.Fprintln(os.Stderr, s)
os.Exit(2)
}()
go func() {
if err := recover(); err != nil {
Cleanup()
panic(err)
}
}()
}

0 comments on commit 341987a

Please sign in to comment.