- Refactored errors chan

Fixed:
- Small fixes
This commit is contained in:
Alexander Kiryukhin 2019-04-04 11:54:24 +03:00
parent c86dd5f3d7
commit 08cbc9b6c0
5 changed files with 41 additions and 17 deletions

View file

@ -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

View file

@ -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)
}
}

View file

@ -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)
}

View file

@ -1,5 +1,6 @@
package rutina
// Options sets custom run policies
type Options int
const (

View file

@ -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}