diff options
author | Alexander Kiryukhin <a.kiryukhin@corp.mail.ru> | 2019-01-11 17:22:06 +0300 |
---|---|---|
committer | Alexander Kiryukhin <a.kiryukhin@corp.mail.ru> | 2019-01-11 17:22:06 +0300 |
commit | a4e62510cf1c06831c9c6e4c3a21ff986ebad14c (patch) | |
tree | 80ca7af46604e47f4833a4466e9ca9d7e82ad17b /rutina.go | |
parent | c582001e89c0b81df1b9eb1e8f1857d1c075a805 (diff) |
Added OS signals handler
Added go.mod
Diffstat (limited to 'rutina.go')
-rwxr-xr-x | rutina.go | 17 |
1 files changed, 17 insertions, 0 deletions
@@ -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() |