mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-12 08:23:48 +02:00
116c310820
* Add linter misspell * Fix spelling Co-authored-by: Anbraten <anton@ju60.de>
37 lines
803 B
Go
37 lines
803 B
Go
package interrupt
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/signal"
|
|
)
|
|
|
|
// 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 canceled 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, 1)
|
|
signal.Notify(c, os.Interrupt)
|
|
defer signal.Stop(c)
|
|
|
|
select {
|
|
case <-ctx.Done():
|
|
case <-c:
|
|
f()
|
|
cancel()
|
|
}
|
|
}()
|
|
|
|
return ctx
|
|
}
|