aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@corp.mail.ru>2019-04-04 11:54:24 +0300
committerAlexander Kiryukhin <a.kiryukhin@corp.mail.ru>2019-04-04 11:54:24 +0300
commit08cbc9b6c05427fd3864bab55a4b40146c54639d (patch)
treec44ab6f2b976a974c20894b29ee0401b5c073676
parentc86dd5f3d70d310a26da73ee4ef2897121c18909 (diff)
Changed:
- Refactored errors chan Fixed: - Small fixes
-rwxr-xr-xREADME.md19
-rw-r--r--example/policies.go9
-rwxr-xr-xmixins.go15
-rw-r--r--options.go1
-rwxr-xr-xrutina.go14
5 files changed, 41 insertions, 17 deletions
diff --git a/README.md b/README.md
index 6d922eb..b3c5ceb 100755
--- a/README.md
+++ b/README.md
@@ -1,5 +1,7 @@
# rutina
+[![GoDoc](https://godoc.org/github.com/neonxp/rutina?status.svg)](https://godoc.org/github.com/neonxp/rutina)
+
Package Rutina (russian "рутина" - ordinary boring everyday work) is routine orchestrator for your application.
It seems like https://godoc.org/golang.org/x/sync/errgroup with some different:
@@ -86,6 +88,14 @@ err := r.Wait()
Here err = error that shutdowns all routines (may be will be changed at future)
+### Get errors channel
+
+```go
+err := <- r.Errors()
+```
+
+Disabled by default. Use `r.With(rutina.WithErrChan())` to turn on.
+
## Mixins
### Usage
@@ -119,14 +129,15 @@ r = r.With(rutina.WithContext(ctx context.Context))
Propagates your own context to Rutina. By default it use own context.
-### Errors channel
+### Enable errors channel
```go
-errChan := make(chan error)
-r = r.With(rutina.WithErrChan(errChan))
+r = r.With(rutina.WithErrChan())
+...
+err := <- r.Errors()
```
-This channel will receive all errors from all routines with any `...Fail` run policy.
+Turn on errors channel
## Example
diff --git a/example/policies.go b/example/policies.go
index 50a4e07..7a81359 100644
--- a/example/policies.go
+++ b/example/policies.go
@@ -5,18 +5,17 @@ package main
import (
"context"
"errors"
- "github.com/neonxp/rutina"
"log"
"time"
+
+ "github.com/neonxp/rutina"
)
func main() {
// New instance with builtin context
r := rutina.New()
- errsChan := make(chan error, 1)
-
- r = r.With(rutina.WithErrChan(errsChan))
+ r = r.With(rutina.WithErrChan())
r.Go(func(ctx context.Context) error {
<-time.After(1 * time.Second)
@@ -54,7 +53,7 @@ func main() {
case <-ctx.Done():
log.Println("Shutdown chan listener")
return nil
- case err := <-errsChan:
+ case err := <-r.Errors():
log.Printf("Error in chan: %v", err)
}
}
diff --git a/mixins.go b/mixins.go
index a037561..1cc2c76 100755
--- a/mixins.go
+++ b/mixins.go
@@ -6,14 +6,17 @@ import (
"os"
)
+// Mixin interface
type Mixin interface {
apply(*Rutina)
}
+// MixinContext propagates user defined context to rutina
type MixinContext struct {
Context context.Context
}
+// WithContext propagates user defined context to rutina
func WithContext(context context.Context) *MixinContext {
return &MixinContext{Context: context}
}
@@ -24,14 +27,17 @@ func (o MixinContext) apply(r *Rutina) {
r.Cancel = cancel
}
+// MixinLogger adds logger to rutina
type MixinLogger struct {
Logger *log.Logger
}
+// WithLogger adds custom logger to rutina
func WithLogger(logger *log.Logger) *MixinLogger {
return &MixinLogger{Logger: logger}
}
+// WithStdLogger adds standard logger to rutina
func WithStdLogger() *MixinLogger {
return &MixinLogger{Logger: log.New(os.Stdout, "rutina", log.LstdFlags)}
}
@@ -40,14 +46,15 @@ func (o MixinLogger) apply(r *Rutina) {
r.logger = o.Logger
}
+// MixinErrChan turns on errors channel on rutina
type MixinErrChan struct {
- errCh chan error
}
-func WithErrChan(errCh chan error) *MixinErrChan {
- return &MixinErrChan{errCh: errCh}
+// WithErrChan turns on errors channel on rutina
+func WithErrChan() *MixinErrChan {
+ return &MixinErrChan{}
}
func (o MixinErrChan) apply(r *Rutina) {
- r.errCh = o.errCh
+ r.errCh = make(chan error, 1)
}
diff --git a/options.go b/options.go
index d1030ba..16c72e8 100644
--- a/options.go
+++ b/options.go
@@ -1,5 +1,6 @@
package rutina
+// Options sets custom run policies
type Options int
const (
diff --git a/rutina.go b/rutina.go
index f04ff55..a38c384 100755
--- a/rutina.go
+++ b/rutina.go
@@ -30,12 +30,12 @@ func New(mixins ...Mixin) *Rutina {
return r.With(mixins...)
}
+// With applies mixins
func (r *Rutina) With(mixins ...Mixin) *Rutina {
- nr := *r
for _, m := range mixins {
- m.apply(&nr)
+ m.apply(r)
}
- return &nr
+ return r
}
// Go routine
@@ -112,7 +112,13 @@ func (r *Rutina) Go(doer func(ctx context.Context) error, opts ...Options) {
}()
}
-// OS signals handler
+// Errors returns chan for all errors, event if DoNothingIfFail or RestartIfFail set.
+// By default it nil. Use MixinErrChan to turn it on
+func (r *Rutina) Errors() <-chan error {
+ return r.errCh
+}
+
+// ListenOsSignals is simple OS signals handler. By default listen syscall.SIGINT and syscall.SIGTERM
func (r *Rutina) ListenOsSignals(signals ...os.Signal) {
if len(signals) == 0 {
signals = []os.Signal{syscall.SIGINT, syscall.SIGTERM}