aboutsummaryrefslogtreecommitdiff
path: root/rutina.go
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@mail.ru>2019-06-29 01:19:21 +0300
committerAlexander Kiryukhin <a.kiryukhin@mail.ru>2019-06-29 01:19:21 +0300
commit01eeeaf5e136928abe75f95d58f3f9cce11c6fe6 (patch)
tree700f9a5106a51ce1f8b35475c84a7bafac162313 /rutina.go
parent741cf397a9150a8f76b8f74289b1dde8aaa43d02 (diff)
Release.v1.0.0
Auto listen OS signal (with mixin) Policies: ...Fail -> ...Error
Diffstat (limited to 'rutina.go')
-rwxr-xr-xrutina.go30
1 files changed, 17 insertions, 13 deletions
diff --git a/rutina.go b/rutina.go
index dccce9e..34d7723 100755
--- a/rutina.go
+++ b/rutina.go
@@ -20,8 +20,9 @@ type Rutina struct {
err error // First error that shutdowns all routines
logger *log.Logger // Optional logger
counter *uint64 // Optional counter that names routines with increment ids for debug purposes at logger
- errCh chan error // Optional channel for errors when RestartIfFail and DoNothingIfFail
+ errCh chan error // Optional channel for errors when RestartIfError and DoNothingIfError
lifecycleListener LifecycleListener // Optional listener for events
+ autoListenSignals []os.Signal // Optional listening os signals, default disabled
}
// New instance with builtin context
@@ -37,6 +38,9 @@ func (r *Rutina) With(mixins ...Mixin) *Rutina {
for _, m := range mixins {
m.apply(r)
}
+ if r.autoListenSignals != nil {
+ r.ListenOsSignals(r.autoListenSignals...)
+ }
return r
}
@@ -46,15 +50,15 @@ func (r *Rutina) Go(doer func(ctx context.Context) error, opts ...Options) {
if r.ctx.Err() != nil {
return
}
- onFail := ShutdownIfFail
+ onFail := ShutdownIfError
for _, o := range opts {
switch o {
- case ShutdownIfFail:
- onFail = ShutdownIfFail
- case RestartIfFail:
- onFail = RestartIfFail
- case DoNothingIfFail:
- onFail = DoNothingIfFail
+ case ShutdownIfError:
+ onFail = ShutdownIfError
+ case RestartIfError:
+ onFail = RestartIfError
+ case DoNothingIfError:
+ onFail = DoNothingIfError
}
}
onDone := ShutdownIfDone
@@ -75,7 +79,7 @@ func (r *Rutina) Go(doer func(ctx context.Context) error, opts ...Options) {
id := atomic.AddUint64(r.counter, 1)
r.lifecycleEvent(EventRoutineStart, int(id))
if err := doer(r.ctx); err != nil {
- r.lifecycleEvent(EventRoutineFail, int(id))
+ r.lifecycleEvent(EventRoutineError, int(id))
r.lifecycleEvent(EventRoutineStop, int(id))
// errors history
if r.errCh != nil {
@@ -83,13 +87,13 @@ func (r *Rutina) Go(doer func(ctx context.Context) error, opts ...Options) {
}
// region routine failed
switch onFail {
- case ShutdownIfFail:
+ case ShutdownIfError:
// Save error only if shutdown all routines
r.onceErr.Do(func() {
r.err = err
})
r.Cancel()
- case RestartIfFail:
+ case RestartIfError:
r.Go(doer, opts...)
}
// endregion
@@ -108,7 +112,7 @@ func (r *Rutina) Go(doer func(ctx context.Context) error, opts ...Options) {
}()
}
-// Errors returns chan for all errors, event if DoNothingIfFail or RestartIfFail set.
+// Errors returns chan for all errors, event if DoNothingIfError or RestartIfError set.
// By default it nil. Use MixinErrChan to turn it on
func (r *Rutina) Errors() <-chan error {
return r.errCh
@@ -140,7 +144,7 @@ func (r *Rutina) Wait() error {
if r.err == nil {
r.lifecycleEvent(EventAppComplete, 0)
} else {
- r.lifecycleEvent(EventAppFail, 0)
+ r.lifecycleEvent(EventAppError, 0)
}
if r.errCh != nil {
close(r.errCh)