Skip to content

Commit

Permalink
libs/common: TrapSignal accepts logger as a first parameter
Browse files Browse the repository at this point in the history
 and does not block anymore
* previously it was dumping "captured ..." msg to os.Stdout
* TrapSignal should not be responsible for blocking thread of execution

Refs #3238
  • Loading branch information
melekes committed Feb 14, 2019
1 parent d32f7d2 commit ea978d3
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 69 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Special thanks to external contributors on this release:
* Apps

* Go API
- [libs/common] TrapSignal accepts logger as a first parameter and does not block anymore
* previously it was dumping "captured ..." msg to os.Stdout
* TrapSignal should not be responsible for blocking thread of execution

* Blockchain Protocol

Expand Down
6 changes: 5 additions & 1 deletion cmd/priv_val_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ func main() {
panic(err)
}

cmn.TrapSignal(func() {
// Stop upon receiving SIGTERM or CTRL-C.
cmn.TrapSignal(logger, func() {
err := rs.Stop()
if err != nil {
panic(err)
}
})

// Run forever.
select {}
}
10 changes: 7 additions & 3 deletions cmd/tendermint/commands/lite.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func ensureAddrHasSchemeOrDefaultToTCP(addr string) (string, error) {
}

func runProxy(cmd *cobra.Command, args []string) error {
// Stop upon receiving SIGTERM or CTRL-C.
cmn.TrapSignal(logger, func() {
// TODO: close up shop
})

nodeAddr, err := ensureAddrHasSchemeOrDefaultToTCP(nodeAddr)
if err != nil {
return err
Expand Down Expand Up @@ -86,9 +91,8 @@ func runProxy(cmd *cobra.Command, args []string) error {
return cmn.ErrorWrap(err, "starting proxy")
}

cmn.TrapSignal(func() {
// TODO: close up shop
})
// Run forever
select {}

return nil
}
22 changes: 7 additions & 15 deletions cmd/tendermint/commands/run_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package commands

import (
"fmt"
"os"
"os/signal"
"syscall"

"github.com/spf13/cobra"

cmn "github.com/tendermint/tendermint/libs/common"
nm "github.com/tendermint/tendermint/node"
)

Expand Down Expand Up @@ -57,25 +55,19 @@ func NewRunNodeCmd(nodeProvider nm.NodeProvider) *cobra.Command {
return fmt.Errorf("Failed to create node: %v", err)
}

// Stop upon receiving SIGTERM or CTRL-C
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
for sig := range c {
logger.Error(fmt.Sprintf("captured %v, exiting...", sig))
if n.IsRunning() {
n.Stop()
}
os.Exit(1)
// Stop upon receiving SIGTERM or CTRL-C.
cmn.TrapSignal(logger, func() {
if n.IsRunning() {
n.Stop()
}
}()
})

if err := n.Start(); err != nil {
return fmt.Errorf("Failed to start node: %v", err)
}
logger.Info("Started node", "nodeInfo", n.Switch().NodeInfo())

// Run forever
// Run forever.
select {}
},
}
Expand Down
16 changes: 12 additions & 4 deletions docs/app-dev/abci-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,15 @@ func cmdKVStore(cmd *cobra.Command, args []string) error {
return err
}
// Wait forever
cmn.TrapSignal(func() {
// Stop upon receiving SIGTERM or CTRL-C.
cmn.TrapSignal(logger, func() {
// Cleanup
srv.Stop()
})
// Run forever.
select {}
return nil
}
```
Expand Down Expand Up @@ -238,11 +242,15 @@ func cmdCounter(cmd *cobra.Command, args []string) error {
return err
}
// Wait forever
cmn.TrapSignal(func() {
// Stop upon receiving SIGTERM or CTRL-C.
cmn.TrapSignal(logger, func() {
// Cleanup
srv.Stop()
})
// Run forever.
select {}
return nil
}
```
Expand Down
49 changes: 28 additions & 21 deletions libs/autofile/cmd/logjack.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,21 @@ func parseFlags() (headPath string, chopSize int64, limitSize int64, version boo
return
}

type fmtLogger struct{}

func (fmtLogger) Info(msg string, keyvals ...interface{}) {
strs := make([]string, len(keyvals))
for i, kv := range keyvals {
strs[i] = fmt.Sprintf("%v", kv)
}
fmt.Printf("%s %s\n", msg, strings.Join(strs, ","))
}

func main() {
// Stop upon receiving SIGTERM or CTRL-C.
cmn.TrapSignal(fmtLogger{}, func() {
fmt.Println("logjack shutting down")
})

// Read options
headPath, chopSize, limitSize, version := parseFlags()
Expand All @@ -51,29 +65,22 @@ func main() {
os.Exit(1)
}

go func() {
// Forever, read from stdin and write to AutoFile.
buf := make([]byte, readBufferSize)
for {
n, err := os.Stdin.Read(buf)
group.Write(buf[:n])
group.Flush()
if err != nil {
group.Stop()
if err == io.EOF {
os.Exit(0)
} else {
fmt.Println("logjack errored")
os.Exit(1)
}
// Forever read from stdin and write to AutoFile.
buf := make([]byte, readBufferSize)
for {
n, err := os.Stdin.Read(buf)
group.Write(buf[:n])
group.Flush()
if err != nil {
group.Stop()
if err == io.EOF {
os.Exit(0)
} else {
fmt.Println("logjack errored")
os.Exit(1)
}
}
}()

// Trap signal
cmn.TrapSignal(func() {
fmt.Println("logjack shutting down")
})
}
}

func parseBytesize(chopSize string) int64 {
Expand Down
13 changes: 8 additions & 5 deletions libs/common/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,24 @@ func GoPath() string {
return path
}

// TrapSignal catches the SIGTERM and executes cb function. After that it exits
// with code 1.
func TrapSignal(cb func()) {
type logger interface {
Info(msg string, keyvals ...interface{})
}

// TrapSignal catches the SIGTERM/SIGINT and executes cb function. After that it exits
// with code 0.
func TrapSignal(logger logger, cb func()) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
for sig := range c {
fmt.Printf("captured %v, exiting...\n", sig)
logger.Info(fmt.Sprintf("captured %v, exiting...", sig))
if cb != nil {
cb()
}
os.Exit(1)
}
}()
select {}
}

// Kill the running process by sending itself SIGTERM.
Expand Down
18 changes: 10 additions & 8 deletions rpc/lib/test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@ type Result struct {
}

func main() {
mux := http.NewServeMux()
cdc := amino.NewCodec()
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
// Stop upon receiving SIGTERM or CTRL-C.
cmn.TrapSignal(logger, func() {})

var (
mux = http.NewServeMux()
cdc = amino.NewCodec()
logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout))
)

rpcserver.RegisterRPCFuncs(mux, routes, cdc, logger)
listener, err := rpcserver.Listen("0.0.0.0:8008", rpcserver.Config{})
if err != nil {
cmn.Exit(err.Error())
}
go rpcserver.StartHTTPServer(listener, mux, logger)
// Wait forever
cmn.TrapSignal(func() {
})

rpcserver.StartHTTPServer(listener, mux, logger)
}
16 changes: 5 additions & 11 deletions tools/tm-bench/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,12 @@ Examples:
"broadcast_tx_"+broadcastTxMethod,
)

// Quit when interrupted or received SIGTERM.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
for sig := range c {
fmt.Printf("captured %v, exiting...\n", sig)
for _, t := range transacters {
t.Stop()
}
os.Exit(1)
// Stop upon receiving SIGTERM or CTRL-C.
cmn.TrapSignal(logger, func() {
for _, t := range transacters {
t.Stop()
}
}()
})

// Wait until transacters have begun until we get the start time.
timeStart := time.Now()
Expand Down
6 changes: 5 additions & 1 deletion tools/tm-monitor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,17 @@ Examples:
ton.Start()
}

cmn.TrapSignal(func() {
// Stop upon receiving SIGTERM or CTRL-C.
cmn.TrapSignal(logger, func() {
if !noton {
ton.Stop()
}
monitor.Stop()
listener.Close()
})

// Run forever.
select {}
}

func startMonitor(endpoints string) *monitor.Monitor {
Expand Down

0 comments on commit ea978d3

Please sign in to comment.