Skip to content

Commit 7604076

Browse files
authored
Merge pull request #70 from ekristen/beta-signals
refactor: bring signals package in house
2 parents 2c1b96e + 800593d commit 7604076

File tree

6 files changed

+122
-4
lines changed

6 files changed

+122
-4
lines changed

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ require (
1212
github.com/h2non/filetype v1.1.3
1313
github.com/krolaw/zipstream v0.0.0-20180621105154-0a2661891f94
1414
github.com/pelletier/go-toml/v2 v2.2.3
15-
github.com/rancher/wrangler v1.1.2
1615
github.com/sirupsen/logrus v1.9.3
1716
github.com/stretchr/testify v1.9.0
1817
github.com/ulikunitz/xz v0.5.12

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
8585
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
8686
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
8787
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
88-
github.com/rancher/wrangler v1.1.2 h1:oXbXo9k7y/H4drUpb4RM1c++vT9O3rpoNEfyusGykiU=
89-
github.com/rancher/wrangler v1.1.2/go.mod h1:2k9MyhlBdjcutcBGoOJSUAz0HgDAXnMjv81d3n/AaQc=
9088
github.com/rogpeppe/fastuuid v1.1.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
9189
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
9290
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import (
55
"path"
66

77
"github.com/apex/log"
8-
"github.com/rancher/wrangler/pkg/signals"
98
"github.com/sirupsen/logrus"
109
"github.com/urfave/cli/v2"
1110

1211
"github.com/ekristen/distillery/pkg/common"
12+
"github.com/ekristen/distillery/pkg/signals"
1313

1414
_ "github.com/ekristen/distillery/pkg/commands/clean"
1515
_ "github.com/ekristen/distillery/pkg/commands/completion"

pkg/signals/signals.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package signals
17+
18+
import (
19+
"context"
20+
"os"
21+
"os/signal"
22+
23+
"github.com/sirupsen/logrus"
24+
)
25+
26+
var onlyOneSignalHandler = make(chan struct{})
27+
var shutdownHandler chan os.Signal
28+
29+
// SetupSignalHandler registered for SIGTERM and SIGINT. A stop channel is returned
30+
// which is closed on one of these signals. If a second signal is caught, the program
31+
// is terminated with exit code 1.
32+
// Only one of SetupSignalContext and SetupSignalHandler should be called, and only can
33+
// be called once.
34+
func SetupSignalHandler() <-chan struct{} {
35+
return SetupSignalContext().Done()
36+
}
37+
38+
// SetupSignalContext is same as SetupSignalHandler, but a context.Context is returned.
39+
// Only one of SetupSignalContext and SetupSignalHandler should be called, and only can
40+
// be called once.
41+
func SetupSignalContext() context.Context {
42+
close(onlyOneSignalHandler) // panics when called twice
43+
44+
shutdownHandler = make(chan os.Signal, 2)
45+
46+
ctx, cancel := context.WithCancel(context.Background())
47+
signal.Notify(shutdownHandler, shutdownSignals...)
48+
go func() {
49+
s := <-shutdownHandler
50+
logrus.Warnf("signal received: %q, canceling context...", s)
51+
cancel()
52+
s = <-shutdownHandler
53+
logrus.Warnf("second signal received: %q, exiting...", s)
54+
os.Exit(1) // second signal. Exit directly.
55+
}()
56+
57+
return ctx
58+
}
59+
60+
// RequestShutdown emulates a received event that is considered as shutdown signal (SIGTERM/SIGINT)
61+
// This returns whether a handler was notified
62+
func RequestShutdown() bool {
63+
if shutdownHandler != nil {
64+
select {
65+
case shutdownHandler <- shutdownSignals[0]:
66+
return true
67+
default:
68+
}
69+
}
70+
71+
return false
72+
}

pkg/signals/signals_posix.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//go:build !windows
2+
3+
/*
4+
Copyright 2017 The Kubernetes Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package signals
20+
21+
import (
22+
"os"
23+
"syscall"
24+
)
25+
26+
var shutdownSignals = []os.Signal{os.Interrupt, syscall.SIGTERM}

pkg/signals/signals_windows.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package signals
18+
19+
import (
20+
"os"
21+
)
22+
23+
var shutdownSignals = []os.Signal{os.Interrupt}

0 commit comments

Comments
 (0)