diff options
author | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2021-03-18 17:58:00 +0300 |
---|---|---|
committer | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2021-03-18 17:58:00 +0300 |
commit | 14fcf184ae40729e270821115e2ba39c475badb9 (patch) | |
tree | 81f6d915e6ed6de5ead922b9bdb1ab1be41a5c01 /README.md | |
parent | cb67839e3c2acb257d8b67bd28ca3dcea258b66a (diff) |
v3v3.0.0
Diffstat (limited to 'README.md')
-rwxr-xr-x | README.md | 45 |
1 files changed, 24 insertions, 21 deletions
@@ -7,7 +7,7 @@ Package Rutina (russian "рутина" - ordinary boring everyday work) is routi 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 errors (useful on daemons) but if errors another - all routines will be cancelled +2) has flexible run/stop policy. i.e. one routine restarts when it errors (useful on daemons) but if errors another - all routines will be cancelled 3) already has optional signal handler `ListenOsSignals()` ## When it need? @@ -21,18 +21,17 @@ Usually, when your program consists of several routines (i.e.: http server, metr With default options: ```go -r := rutina.New(nil) +r := rutina.New() ``` or with custom options: ```go r := rutina.New( - rutina.Opt. - SetParentContext(ctx context.Context). // Pass parent context to Rutina (otherwise it uses own new context) - SetListenOsSignals(listenOsSignals bool). // Auto listen OS signals and close context on Kill, Term signal - SetLogger(l logger). // Pass logger for debug, i.e. `log.Printf` - SetErrors(errCh chan error) // Set errors channel for errors from routines in Restart/DoNothing errors policy + ParentContext(ctx context.Context), // Pass parent context to Rutina (otherwise it uses own new context) + ListenOsSignals(listenOsSignals ...os.Signal), // Auto listen OS signals and close context on Kill, Term signal + Logger(l logger), // Pass logger for debug, i.e. `log.Printf` + Errors(errCh chan error), // Set errors channel for errors from routines in Restart/DoNothing errors policy ) ``` @@ -41,17 +40,21 @@ r := rutina.New( ```go r.Go(func (ctx context.Context) error { ...do something... -}, *runOptions) +}) ``` #### Run Options ```go -RunOpt. - SetOnDone(policy Policy). // Run policy if returns no error - SetOnError(policy Policy). // Run policy if returns error - SetTimeout(timeout time.Duration). // Timeout to routine (after it context will be closed) - SetMaxCount(maxCount int) // Max tries on Restart policy +r.Go( + func (ctx context.Context) error { + ...do something... + }, + SetOnDone(policy Policy), // Run policy if returns no error (default: Shutdown) + SetOnError(policy Policy), // Run policy if returns error (default: Shutdown) + SetTimeout(timeout time.Duration), // Timeout to routine (after it context will be closed) + SetMaxCount(maxCount int), // Max tries on Restart policy +) ``` #### Run policies @@ -64,23 +67,23 @@ RunOpt. ```go r.Go(func(ctx context.Context) error { - // If this routine produce no error - it just completes, other routines not affected + // If this routine produce no error - all other routines will shutdown (because context cancels) // If it returns error - all other routines will shutdown (because context cancels) -}, nil) +},) r.Go(func(ctx context.Context) error { // If this routine produce no error - it restarts // If it returns error - all other routines will shutdown (because context cancels) -}, rutina.RunOpt.SetOnDone(rutina.Restart)) +}, SetOnDone(rutina.Restart)) 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.RunOpt.SetOnDone(rutina.Shutdown).SetOnError(rutina.Restart)) + // If it returns error - it will be restarted (maximum 10 times) +}, SetOnError(rutina.Restart), SetMaxCount(10)) r.Go(func(ctx context.Context) error { - // If this routine stopped by any case - all other routines will shutdown (because context cancels) -}, rutina.RunOpt.SetOnDone(rutina.Shutdown)) + // If this routine stopped by any case other routines will work as before. +}, SetOnDone(rutina.DoNothing)) r.ListenOsSignals() // Shutdown all routines by OS signal ``` @@ -104,7 +107,7 @@ r.Kill(id) // Closes individual context for #id routine that must shutdown it ### List of routines ```go -list := r.Processes() +list := r.Processes() ``` Returns ids of working routines |