aboutsummaryrefslogtreecommitdiff
path: root/rutina.go
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@corp.mail.ru>2019-01-11 17:22:06 +0300
committerAlexander Kiryukhin <a.kiryukhin@corp.mail.ru>2019-01-11 17:22:06 +0300
commita4e62510cf1c06831c9c6e4c3a21ff986ebad14c (patch)
tree80ca7af46604e47f4833a4466e9ca9d7e82ad17b /rutina.go
parentc582001e89c0b81df1b9eb1e8f1857d1c075a805 (diff)
Added OS signals handler
Added go.mod
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()