Skip to content

Commit 0f316b8

Browse files
authored
Shutdown gracefully (#29)
1 parent b6104fe commit 0f316b8

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

cmd/catp/catp/app.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import (
1010
"io"
1111
"log"
1212
"os"
13+
"os/signal"
1314
"path/filepath"
1415
"runtime/pprof"
1516
"sort"
1617
"strings"
1718
"sync/atomic"
19+
"syscall"
1820
"time"
1921

2022
"github.com/bool64/dev/version"
@@ -238,6 +240,16 @@ func Main(options ...func(o *Options)) error { //nolint:funlen,cyclop,gocognit,g
238240
r.sizes[fn] = st.Size()
239241
}
240242

243+
shutdown := make(chan os.Signal, 1)
244+
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
245+
246+
go func() {
247+
<-shutdown
248+
249+
println("received signal, shutting down...")
250+
atomic.StoreInt64(&r.closed, 1)
251+
}()
252+
241253
if r.parallel >= 2 {
242254
pr := r.pr
243255
pr.Start(func(t *progress.Task) {

cmd/catp/catp/catp.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ type runner struct {
2828
mu sync.Mutex
2929
output io.Writer
3030

31+
closed int64
32+
3133
pr *progress.Progress
3234
progressJSON string
3335

@@ -220,6 +222,10 @@ func (r *runner) scanFile(filename string, rd io.Reader, out io.Writer) {
220222
for s.Scan() {
221223
lines++
222224

225+
if atomic.LoadInt64(&r.closed) > 0 {
226+
break
227+
}
228+
223229
if r.limiter != nil {
224230
_ = r.limiter.Wait(context.Background()) //nolint:errcheck // No failure condition here.
225231
}

0 commit comments

Comments
 (0)