rutina/README.md

136 lines
3.5 KiB
Markdown
Raw Normal View History

2018-12-05 00:48:33 +03:00
# rutina
2019-01-17 07:54:39 +03:00
Package Rutina (russian "рутина" - ordinary boring everyday work) is routine orchestrator for your application.
2018-12-05 00:48:33 +03:00
2019-01-17 07:54:39 +03:00
It seems like https://godoc.org/golang.org/x/sync/errgroup with some different:
1) propagates context to every routines. So routine can check if context stopped (`ctx.Done()`).
2) has flexible run/stop policy. i.e. one routine restarts when it fails (useful on daemons) but if fails another - all routines will be cancelled
2019-01-17 07:54:39 +03:00
3) already has optional signal handler `ListenOsSignals()`
2018-12-05 00:48:33 +03:00
## When it need?
2019-01-17 07:54:39 +03:00
Usually, when your program consists of several routines (i.e.: http server, metrics server and os signals subscriber) and you want to stop all routines when one of them ends (i.e.: by TERM os signal in signal subscriber).
## Usage
### New instance
```go
r := rutina.New()
```
2019-01-17 07:54:39 +03:00
or with optional mixins (see below):
2019-01-17 07:54:39 +03:00
```go
r := rutina.New(...Mixins)
```
or
```go
r.With(...Mixins)
```
2019-01-17 07:54:39 +03:00
### Start new routine
```go
2019-01-17 07:54:39 +03:00
r.Go(func (ctx context.Context) error {
...do something...
}, ...runOptions)
2019-01-17 07:54:39 +03:00
```
Available options of run policy:
* `ShutdownIfFail` - Shutdown all routines if this routine fails
* `RestartIfFail` - Restart this routine if it fail
* `DoNothingIfFail` - Do nothing just stop this routine if it fail
* `ShutdownIfDone` - Shutdown all routines if this routine done without errors
* `RestartIfDone` - Restart if this routine done without errors
* `DoNothingIfDone` - Do nothing if this routine done without errors
Default policy:
`ShutdownIfFail` && `ShutdownIfDone`
(just like [errgroup](https://godoc.org/golang.org/x/sync/errgroup))
#### Example of run policies
```go
2019-03-27 02:47:49 +03:00
r.Go(func(ctx context.Context) error {
// If this routine produce no error - it just restarts
// If it returns error - all other routines will shutdown (because context cancels)
}, rutina.RestartIfDone, rutina.ShutdownIfFail)
r.Go(func(ctx context.Context) error {
// If this routine produce no error - it just completes
// If it returns error - all other routines will shutdown (because context cancels)
}, rutina.DoNothingIfDone, rutina.ShutdownIfFail)
r.Go(func(ctx context.Context) error {
// If this routine produce no error - all other routines will shutdown (because context cancels)
// If it returns error - it will be restarted
}, rutina.RestartIfFail)
r.Go(func(ctx context.Context) error {
// If this routine stopped by any case - all other routines will shutdown (because context cancels)
}, rutina.ShutdownIfDone)
r.ListenOsSignals() // Shutdown all routines by OS signal
2019-01-17 07:54:39 +03:00
```
### Wait routines to complete
```go
2019-01-17 07:54:39 +03:00
err := r.Wait()
```
Here err = error that shutdowns all routines (may be will be changed at future)
2019-01-17 07:54:39 +03:00
## Mixins
2019-01-17 07:54:39 +03:00
### Usage
2019-01-17 07:54:39 +03:00
```go
r := rutina.New(mixin1, mixin2, ...)
2019-01-17 07:54:39 +03:00
```
or
```go
2019-01-17 07:54:39 +03:00
r := rutina.New()
r = r.With(mixin1, mixin2, ...) // Returns new instance of Rutina!
2019-01-17 07:54:39 +03:00
```
### Logger
```go
r = r.With(rutina.WithStdLogger())
```
or
```go
r = r.With(rutina.WithLogger(logger log.Logger))
```
2019-01-17 07:54:39 +03:00
Sets standard or custom logger. By default there is no logger.
2019-01-17 07:54:39 +03:00
### Custom context
2019-01-17 07:54:39 +03:00
```go
r = r.With(rutina.WithContext(ctx context.Context))
````
2019-01-17 07:54:39 +03:00
Propagates your own context to Rutina. By default it use own context.
2018-12-05 00:48:33 +03:00
### Errors channel
```go
errChan := make(chan error)
r = r.With(rutina.WithErrChan(errChan))
```
This channel will receive all errors from all routines with any `...Fail` run policy.
2018-12-05 00:48:33 +03:00
## Example
HTTP server with graceful shutdown [`example/http_server.go`](https://github.com/NeonXP/rutina/blob/master/example/http_server.go)
2018-12-05 00:48:33 +03:00
Different run policies [`example/policies.go`](https://github.com/NeonXP/rutina/blob/master/example/policies.go)