diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 4837f9f..0000000 --- a/.travis.yml +++ /dev/null @@ -1,10 +0,0 @@ -language: go - -go: - - 1.10.x - -install: - - go get go.uber.org/dig - -script: - - go test -v ./... diff --git a/README.md b/README.md index d72119c..257e67f 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,5 @@ # Grace pkg -[![Build Status](https://travis-ci.org/chapsuk/grace.svg?branch=master)](https://travis-ci.org/chapsuk/grace) - Package with single function for create base context which will be canceled on signals: `SIGINT`, `SIGTERM`, `SIGHUP`. @@ -12,39 +10,13 @@ package main import ( "context" + "github.com/chapsuk/grace" ) func main() { ctx := grace.ShutdownContext(context.Background()) - // context can be used for long time tasks or for what else <-ctx.Done() // do graceful shutdown after context was canceled } ``` - -## Example with dig - -```go -package main - -import ( - "github.com/chapsuk/grace" - "go.uber.org/dig" -) - -func main() { - c := dig.New() - - c.Provide(grace.NewShutdownContext) - c.Invoke(func(p grace.ContextParams) { - // nodes: { - // context.Context[name="grace_context"] -> deps: [], ctor: func() grace.ContextResult - // } - // values: { - // context.Context[name="grace_context"] => context.Background.WithCancel - // } - <-p.Context.Done() - }) -} -``` diff --git a/sahutdown_test.go b/sahutdown_test.go deleted file mode 100644 index ee5c58c..0000000 --- a/sahutdown_test.go +++ /dev/null @@ -1,30 +0,0 @@ -package grace_test - -import ( - "testing" - "time" - - "github.com/chapsuk/grace" - "go.uber.org/dig" -) - -func TestDigUsage(t *testing.T) { - - c := dig.New() - - if err := c.Provide(grace.NewShutdownContext); err != nil { - t.Fatalf("provide grace context error: %v", err) - } - - err := c.Invoke(func(cparam grace.ContextParams) { - select { - case <-cparam.Context.Done(): - t.Fatalf("context canceled without signal") - case <-time.Tick(100 * time.Millisecond): - } - }) - - if err != nil { - t.Fatalf("invoke grace context error: %v", err) - } -} diff --git a/shutdown.go b/shutdown.go index c4dc0b3..1a956d8 100644 --- a/shutdown.go +++ b/shutdown.go @@ -5,8 +5,6 @@ import ( "os" "os/signal" "syscall" - - "go.uber.org/dig" ) // ShutdownContext returns child context from passed context which will be canceled @@ -21,22 +19,3 @@ func ShutdownContext(c context.Context) context.Context { }() return ctx } - -// ContextResult wrap grace context for dig usage -type ContextResult struct { - dig.Out - Context context.Context `name:"grace_context"` -} - -// NewShutdownContext returns wrapped ShutdownContext func result for dig usage -func NewShutdownContext() ContextResult { - return ContextResult{ - Context: ShutdownContext(context.Background()), - } -} - -// ContextParams contains shutdown context for dig usage -type ContextParams struct { - dig.In - Context context.Context `name:"grace_context"` -}