Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions cmd/process.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package cmd

import (
"context"
"os"
"os/signal"
"syscall"

"golang.org/x/sync/errgroup"
)

// ProcessGroup is an errgroup that listens for OS process signals
type ProcessGroup struct {
ctx context.Context
cancel context.CancelFunc
group *errgroup.Group
}

// NewProcessGroup creates a new ProcessGroup
func NewProcessGroup(outerCtx context.Context) *ProcessGroup {
ctx, cancel := context.WithCancel(outerCtx)
group, ctx := errgroup.WithContext(ctx)
return &ProcessGroup{
ctx,
cancel,
group,
}
}

// Context returns the context used by the ProcessGroup
func (gw *ProcessGroup) Context() context.Context {
return gw.ctx
}

// Go calls the given function in a new goroutine.
//
// The first call to return a non-nil error cancels the group; its error will be
// returned by Wait.
func (gw *ProcessGroup) Go(f func() error) {
gw.group.Go(f)
}

// Wait blocks until all function calls from the Go method have returned, then
// returns the first non-nil error (if any) from them.
func (gw *ProcessGroup) Wait() error {
signals := make(chan os.Signal)
defer close(signals)

signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)

errs := make(chan error)
defer close(errs)
go func(group *errgroup.Group, ctx context.Context) {
err := group.Wait()
errs <- err
}(gw.group, gw.ctx)

for {
select {
case <-signals:
gw.cancel()
case <-gw.ctx.Done():
err := <-errs
return err
case err := <-errs:
return err
}
}
}
58 changes: 58 additions & 0 deletions cmd/process_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cmd_test

import (
"context"
"errors"
"testing"
"time"

"github.com/syncromatics/go-kit/cmd"

"github.com/stretchr/testify/assert"
)

func Test_ProcessGroup_Success(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
group := cmd.NewProcessGroup(ctx)

group.Go(func() error {
cancel()

select {
case <-group.Context().Done():
return nil
case <-time.After(3 * time.Second):
return errors.New("group context not derived from input context")
}
})

err := group.Wait()
assert.Nil(t, err)
}

func Test_ProcessGroup_SuccessWithoutContextCancellation(t *testing.T) {
group := cmd.NewProcessGroup(context.Background())

group.Go(func() error {
return nil
})

err := group.Wait()
assert.Nil(t, err)
}

func Test_ProcessGroup_Failure(t *testing.T) {
ctx := context.Background()
group := cmd.NewProcessGroup(ctx)

group.Go(func() error {
return nil
})
group.Go(func() error {
return errors.New("intentional failure")
})

err := group.Wait()
assert.Equal(t, "intentional failure", err.Error())

}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/Microsoft/go-winio v0.4.14 // indirect
github.com/Shopify/sarama v1.24.1
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
github.com/denisenkom/go-mssqldb v0.0.0-20190515213511-eb9f6a1743f3
github.com/go-redis/redis v6.15.7+incompatible
github.com/golang-migrate/migrate/v4 v4.8.0
github.com/golang/protobuf v1.3.2
Expand All @@ -20,12 +19,14 @@ require (
github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829
github.com/rakyll/statik v0.1.6
github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da // indirect
github.com/stretchr/testify v1.4.0
github.com/syncromatics/proto-schema-registry v0.7.2
github.com/uber/jaeger-client-go v2.22.1+incompatible
github.com/uber/jaeger-lib v2.2.0+incompatible // indirect
github.com/wvanbergen/kazoo-go v0.0.0-20180202103751-f72d8611297a
go.uber.org/zap v1.13.0
golang.org/x/net v0.0.0-20200513185701-a91f0712d120
golang.org/x/sync v0.0.0-20190423024810-112230192c58
golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9 // indirect
google.golang.org/grpc v1.25.1
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down