Skip to content

Commit

Permalink
add stop function to ContextWServer
Browse files Browse the repository at this point in the history
  • Loading branch information
Vamsi-Mundra committed Dec 6, 2024
1 parent f1f76a3 commit 3e303bc
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions cmdutil/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,17 @@ func (sf ServerFuncs) Stop(err error) {

// NewContextServer that when Run(), calls the given function with a context
// that is canceled when Stop() is called.
func NewContextServer(fn func(context.Context) error) Server {
func NewContextServer(fn func(context.Context) error, stopFunc ...func(error)) Server {
ctx, cancel := context.WithCancel(context.Background())

return &ServerFuncs{
RunFunc: func() error {
return fn(ctx)
},
StopFunc: func(error) {
StopFunc: func(err error) {
for _, stop := range stopFunc {
stop(err)
}
cancel()
},
}
Expand Down

0 comments on commit 3e303bc

Please sign in to comment.