Skip to content

Commit 5efef51

Browse files
committed
Use go-cli ExecuteContext to handle termination signals
Signed-off-by: Carlos Martín <carlos.martin.sanchez@gmail.com>
1 parent f800074 commit 5efef51

File tree

4 files changed

+47
-46
lines changed

4 files changed

+47
-46
lines changed

cmd/lookoutd/common.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"io/ioutil"
88
"net/http"
99
"os"
10-
"os/signal"
1110
"runtime"
1211
"time"
1312

@@ -24,7 +23,6 @@ import (
2423
"github.com/src-d/lookout/store/models"
2524
"github.com/src-d/lookout/util/cache"
2625
"github.com/src-d/lookout/util/cli"
27-
"github.com/src-d/lookout/util/ctxlog"
2826
"github.com/src-d/lookout/util/grpchelper"
2927

3028
"github.com/gregjones/httpcache/diskcache"
@@ -452,13 +450,3 @@ func (c *queueConsumerCommand) runEventDequeuer(ctx context.Context, qOpt cli.Qu
452450

453451
return queue_util.RunEventDequeuer(ctx, qOpt.Q, server.HandleEvent, c.Workers)
454452
}
455-
456-
func stopOnSignal(ctx context.Context) error {
457-
var stopSignalChan = make(chan os.Signal)
458-
signal.Notify(stopSignalChan, os.Interrupt)
459-
sig := <-stopSignalChan
460-
461-
ctxlog.Get(ctx).Infof("Signal %s received. Stopping server...", sig)
462-
463-
return nil
464-
}

cmd/lookoutd/serve.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ type ServeCommand struct {
2020
queueConsumerCommand
2121
}
2222

23-
func (c *ServeCommand) Execute(args []string) error {
24-
ctx, stopCtx := context.WithCancel(context.Background())
23+
func (c *ServeCommand) ExecuteContext(ctx context.Context, args []string) error {
24+
ctx, stopCtx := context.WithCancel(ctx)
2525
stopCh := make(chan error, 1)
2626

2727
cli.InitLog(&c.LogOptions, name)
@@ -88,32 +88,39 @@ func (c *ServeCommand) Execute(args []string) error {
8888
startDataServer, stopDataServer := c.initDataServer(dataHandler)
8989
go func() {
9090
err := startDataServer()
91-
ctxlog.Get(ctx).Errorf(err, "data server stopped")
91+
if err != context.Canceled {
92+
ctxlog.Get(ctx).Errorf(err, "data server stopped")
93+
}
9294
stopCh <- err
9395
}()
9496

9597
go func() {
9698
err := c.runEventDequeuer(ctx, qOpt, server)
97-
ctxlog.Get(ctx).Errorf(err, "event dequeuer stopped")
99+
if err != context.Canceled {
100+
ctxlog.Get(ctx).Errorf(err, "event dequeuer stopped")
101+
}
98102
stopCh <- err
99103
}()
100104

101105
go func() {
102106
err := c.runEventEnqueuer(ctx, qOpt, watcher)
103-
ctxlog.Get(ctx).Errorf(err, "event enqueuer stopped")
107+
if err != context.Canceled {
108+
ctxlog.Get(ctx).Errorf(err, "event enqueuer stopped")
109+
}
104110
stopCh <- err
105111
}()
106112

107-
go func() {
108-
stopCh <- stopOnSignal(ctx)
109-
}()
110-
111113
c.probeReadiness = true
112114

113-
err = <-stopCh
115+
select {
116+
case <-ctx.Done():
117+
err = ctx.Err()
118+
case err = <-stopCh:
119+
// stop the other servers that did not fail
120+
stopCtx()
121+
}
114122

115-
// stop servers gracefully
116-
stopCtx()
123+
// stop data server, it does not stop with context
117124
stopDataServer()
118125

119126
return err

cmd/lookoutd/watch.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ type WatchCommand struct {
1818
cli.QueueOptions
1919
}
2020

21-
func (c *WatchCommand) Execute(args []string) error {
22-
ctx, stopCtx := context.WithCancel(context.Background())
21+
func (c *WatchCommand) ExecuteContext(ctx context.Context, args []string) error {
22+
ctx, stopCtx := context.WithCancel(ctx)
2323
stopCh := make(chan error, 1)
2424

2525
cli.InitLog(&c.LogOptions, name)
@@ -53,20 +53,21 @@ func (c *WatchCommand) Execute(args []string) error {
5353

5454
go func() {
5555
err := c.runEventEnqueuer(ctx, c.QueueOptions, watcher)
56-
ctxlog.Get(ctx).Errorf(err, "event enqueuer stopped")
56+
if err != context.Canceled {
57+
ctxlog.Get(ctx).Errorf(err, "event enqueuer stopped")
58+
}
5759
stopCh <- err
5860
}()
5961

60-
go func() {
61-
stopCh <- stopOnSignal(ctx)
62-
}()
63-
6462
c.probeReadiness = true
6563

66-
err = <-stopCh
67-
68-
// stop servers gracefully
69-
stopCtx()
64+
select {
65+
case <-ctx.Done():
66+
err = ctx.Err()
67+
case err = <-stopCh:
68+
// stop the other servers that did not fail
69+
stopCtx()
70+
}
7071

7172
return err
7273
}

cmd/lookoutd/work.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ type WorkCommand struct {
2121
cli.QueueOptions
2222
}
2323

24-
func (c *WorkCommand) Execute(args []string) error {
25-
ctx, stopCtx := context.WithCancel(context.Background())
24+
func (c *WorkCommand) ExecuteContext(ctx context.Context, args []string) error {
25+
ctx, stopCtx := context.WithCancel(ctx)
2626
stopCh := make(chan error, 1)
2727

2828
cli.InitLog(&c.LogOptions, name)
@@ -81,28 +81,33 @@ func (c *WorkCommand) Execute(args []string) error {
8181
startDataServer, stopDataServer := c.initDataServer(dataHandler)
8282
go func() {
8383
err := startDataServer()
84-
ctxlog.Get(ctx).Errorf(err, "data server stopped")
84+
if err != context.Canceled {
85+
ctxlog.Get(ctx).Errorf(err, "data server stopped")
86+
}
8587
stopCh <- err
8688
}()
8789

8890
go func() {
8991
err := c.runEventDequeuer(ctx, c.QueueOptions, server)
90-
ctxlog.Get(ctx).Errorf(err, "event dequeuer stopped")
92+
if err != context.Canceled {
93+
ctxlog.Get(ctx).Errorf(err, "event dequeuer stopped")
94+
}
9195
stopCh <- err
9296
}()
9397

94-
go func() {
95-
stopCh <- stopOnSignal(ctx)
96-
}()
97-
9898
c.probeReadiness = true
9999

100100
ctxlog.Get(ctx).Infof("Worker started")
101101

102-
err = <-stopCh
102+
select {
103+
case <-ctx.Done():
104+
err = ctx.Err()
105+
case err = <-stopCh:
106+
// stop the other servers that did not fail
107+
stopCtx()
108+
}
103109

104-
// stop servers gracefully
105-
stopCtx()
110+
// stop data server, it does not stop with context
106111
stopDataServer()
107112

108113
return err

0 commit comments

Comments
 (0)