graceful shutdown on SIGTERM

This commit is contained in:
Brad Rydzewski 2017-09-14 18:42:00 -07:00
parent 45357681a6
commit 8c7d48ebed
4 changed files with 67 additions and 2 deletions

View file

@ -17,10 +17,10 @@ import (
"github.com/cncd/pipeline/pipeline"
"github.com/cncd/pipeline/pipeline/backend"
"github.com/cncd/pipeline/pipeline/backend/docker"
"github.com/cncd/pipeline/pipeline/interrupt"
"github.com/cncd/pipeline/pipeline/multipart"
"github.com/cncd/pipeline/pipeline/rpc"
"github.com/drone/signal"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/tevino/abool"
@ -89,7 +89,7 @@ func loop(c *cli.Context) error {
context.Background(),
metadata.Pairs("hostname", hostname),
)
ctx = interrupt.WithContextFunc(ctx, func() {
ctx = signal.WithContextFunc(ctx, func() {
println("ctrl+c received, terminating process")
sigterm.Set()
})

21
vendor/github.com/drone/signal/LICENSE generated vendored Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 drone.io
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

38
vendor/github.com/drone/signal/signal.go generated vendored Normal file
View file

@ -0,0 +1,38 @@
// Package signal implements Context os/signal
package signal
import (
"context"
"os"
"os/signal"
"syscall"
)
// WithContext returns a copy of parent context whose Done channel is closed
// when an os interrupt signal is received.
func WithContext(ctx context.Context) context.Context {
return WithContextFunc(ctx, func() {
println("ctrl+c received, terminating process")
})
}
// WithContextFunc returns a copy of parent context that is cancelled when
// an os interrupt signal is received. The callback function f is invoked
// before cancellation.
func WithContextFunc(ctx context.Context, f func()) context.Context {
ctx, cancel := context.WithCancel(ctx)
go func() {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
defer signal.Stop(c)
select {
case <-ctx.Done():
case <-c:
f()
cancel()
}
}()
return ctx
}

6
vendor/vendor.json vendored
View file

@ -343,6 +343,12 @@
"revision": "72f4df4a266b7e1e15b75d4ab8e43e273fcbe1d7",
"revisionTime": "2017-09-09T01:06:28Z"
},
{
"checksumSHA1": "ZOASUqqEP4pfH5MNjImiyemkjOA=",
"path": "github.com/drone/signal",
"revision": "ac5d07ef13150b4db403b96152e1ea54335df400",
"revisionTime": "2017-09-15T01:38:02Z"
},
{
"checksumSHA1": "40Ns85VYa4smQPcewZ7SOdfLnKU=",
"path": "github.com/fatih/structs",