aboutsummaryrefslogtreecommitdiff
path: root/rutina.go
diff options
context:
space:
mode:
Diffstat (limited to 'rutina.go')
-rwxr-xr-xrutina.go17
1 files changed, 17 insertions, 0 deletions
diff --git a/rutina.go b/rutina.go
index 68bf048..a3df532 100755
--- a/rutina.go
+++ b/rutina.go
@@ -2,7 +2,10 @@ package rutina
import (
"context"
+ "os"
+ "os/signal"
"sync"
+ "syscall"
)
//Rutina is routine manager
@@ -44,6 +47,20 @@ func (r *Rutina) Go(doer func(ctx context.Context) error) {
}()
}
+// OS signals handler
+func (r *Rutina) ListenTermSignals() {
+ r.Go(func(ctx context.Context) error {
+ sig := make(chan os.Signal, 1)
+ signal.Notify(sig, syscall.SIGTERM, syscall.SIGINT)
+ select {
+ case <-sig:
+ r.cancel()
+ case <-ctx.Done():
+ }
+ return nil
+ })
+}
+
// Wait all routines and returns first error or nil if all routines completes without errors
func (r *Rutina) Wait() error {
r.wg.Wait()