8028: when a signal is received, terminate all running commands and wait in a WaitGroup.
authorradhika <radhika@curoverse.com>
Wed, 20 Jan 2016 12:16:05 +0000 (07:16 -0500)
committerradhika <radhika@curoverse.com>
Wed, 20 Jan 2016 17:36:02 +0000 (12:36 -0500)
services/crunch-dispatch-local/crunch-dispatch-local.go

index e5758390403591c03d52a8fbf60313891b7f4d94..eb8550eceb8ebcaca25ace2ad2771a4bd587a227 100644 (file)
@@ -7,6 +7,7 @@ import (
        "os"
        "os/exec"
        "os/signal"
+       "sync"
        "syscall"
        "time"
 )
@@ -52,16 +53,22 @@ func doMain() error {
        sigChan = make(chan os.Signal, 1)
        signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
        go func(sig <-chan os.Signal) {
+               var wg sync.WaitGroup
                for sig := range sig {
                        doneProcessing <- true
                        caught := sig
                        for uuid, cmd := range runningCmds {
-                               cmd.Process.Signal(caught)
-                               if _, err := cmd.Process.Wait(); err != nil {
-                                       log.Printf("Error while waiting for process to finish for %v: %q", uuid, err)
-                               }
+                               go func(uuid string) {
+                                       wg.Add(1)
+                                       defer wg.Done()
+                                       cmd.Process.Signal(caught)
+                                       if _, err := cmd.Process.Wait(); err != nil {
+                                               log.Printf("Error while waiting for process to finish for %v: %q", uuid, err)
+                                       }
+                               }(uuid)
                        }
                }
+               wg.Wait()
        }(sigChan)
 
        // channel to terminate