Skip to content

Commit 6d1c4ee

Browse files
committed
polish(ctx): setup ctx cancel in main
- dedups cancel logic
1 parent 625a7cc commit 6d1c4ee

File tree

3 files changed

+21
-38
lines changed

3 files changed

+21
-38
lines changed

commands/walk.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ package commands
33
import (
44
"context"
55
"errors"
6-
"os"
7-
"os/signal"
86
"strings"
9-
"syscall"
107
"time"
118

129
"github.com/filecoin-project/sentinel-visor/schedule"
@@ -100,20 +97,6 @@ func walk(cctx *cli.Context) error {
10097
strg = db
10198
}
10299
}
103-
// Set up a context that is canceled when the command is interrupted
104-
ctx, cancel := context.WithCancel(cctx.Context)
105-
defer cancel()
106-
107-
// Set up a signal handler to cancel the context
108-
go func() {
109-
interrupt := make(chan os.Signal, 1)
110-
signal.Notify(interrupt, syscall.SIGTERM, syscall.SIGINT)
111-
select {
112-
case <-interrupt:
113-
cancel()
114-
case <-ctx.Done():
115-
}
116-
}()
117100

118101
scheduler := schedule.NewScheduler(cctx.Duration("task-delay"))
119102

@@ -136,7 +119,7 @@ func walk(cctx *cli.Context) error {
136119
})
137120

138121
// Start the scheduler and wait for it to complete or to be cancelled.
139-
err = scheduler.Run(ctx)
122+
err = scheduler.Run(cctx.Context)
140123
if !errors.Is(err, context.Canceled) {
141124
return err
142125
}

commands/watch.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ package commands
33
import (
44
"context"
55
"errors"
6-
"os"
7-
"os/signal"
86
"strings"
9-
"syscall"
107
"time"
118

129
"github.com/filecoin-project/sentinel-visor/schedule"
@@ -75,21 +72,6 @@ func watch(cctx *cli.Context) error {
7572
storage = db
7673
}
7774

78-
// Set up a context that is canceled when the command is interrupted
79-
ctx, cancel := context.WithCancel(cctx.Context)
80-
defer cancel()
81-
82-
// Set up a signal handler to cancel the context
83-
go func() {
84-
interrupt := make(chan os.Signal, 1)
85-
signal.Notify(interrupt, syscall.SIGTERM, syscall.SIGINT)
86-
select {
87-
case <-interrupt:
88-
cancel()
89-
case <-ctx.Done():
90-
}
91-
}()
92-
9375
tsIndexer, err := chain.NewTipSetIndexer(lensOpener, storage, builtin.EpochDurationSeconds*time.Second, cctx.String("name"), tasks)
9476
if err != nil {
9577
return xerrors.Errorf("setup indexer: %w", err)
@@ -107,7 +89,7 @@ func watch(cctx *cli.Context) error {
10789
})
10890

10991
// Start the scheduler and wait for it to complete or to be cancelled.
110-
err = scheduler.Run(ctx)
92+
err = scheduler.Run(cctx.Context)
11193
if !errors.Is(err, context.Canceled) {
11294
return err
11395
}

main.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56
"os"
7+
"os/signal"
8+
"syscall"
69

710
logging "github.com/ipfs/go-log/v2"
811
"github.com/urfave/cli/v2"
@@ -14,6 +17,21 @@ import (
1417
var log = logging.Logger("visor")
1518

1619
func main() {
20+
// Set up a context that is canceled when the command is interrupted
21+
ctx, cancel := context.WithCancel(context.Background())
22+
defer cancel()
23+
24+
// Set up a signal handler to cancel the context
25+
go func() {
26+
interrupt := make(chan os.Signal, 1)
27+
signal.Notify(interrupt, syscall.SIGTERM, syscall.SIGINT)
28+
select {
29+
case <-interrupt:
30+
cancel()
31+
case <-ctx.Done():
32+
}
33+
}()
34+
1735
if err := logging.SetLogLevel("*", "info"); err != nil {
1836
log.Fatal(err)
1937
}
@@ -137,7 +155,7 @@ func main() {
137155
},
138156
}
139157

140-
if err := app.Run(os.Args); err != nil {
158+
if err := app.RunContext(ctx, os.Args); err != nil {
141159
log.Fatal(err)
142160
}
143161
}

0 commit comments

Comments
 (0)