From 6839bce89819ed461d5d38e916cd635d398d7828 Mon Sep 17 00:00:00 2001 From: mpapenbr Date: Sun, 2 Jun 2024 09:37:22 +0200 Subject: [PATCH] Keep logging message on connection loss Fixes #50 --- README.md | 22 ++++++++++++---------- pkg/cmd/ping/ping.go | 20 +++++++++++++------- pkg/grpc/dataprovider.go | 16 ++++++++++++++-- 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index a787a19..a458d2f 100644 --- a/README.md +++ b/README.md @@ -54,13 +54,13 @@ log-format: json #log-file: racelogger.log ``` -| Key | Value | Info | -| ---------- | --------- | ----------------------------------------------------- | -| addr | host:port | This is the address of the backend server | -| token | | A secret credential to identify valid racelogger user | -| log-level | info | The level used for logging | -| log-format | json | Logs are written in JSON format. May also use `text` | -| log-file | | if present logs are written to this file | +| Key | Value | Info | +| ---------- | ----------- | ----------------------------------------------------- | +| addr | `host:port` | This is the address of the backend server | +| token | | A secret credential to identify valid racelogger user | +| log-level | `info` | The level used for logging | +| log-format | `json` | Logs are written in JSON format. May also use `text` | +| log-file | | if present logs are written to this file | ## Check @@ -106,7 +106,7 @@ _Tip:_ Use double quotes (") around values containing blanks and/or other specia ### Log messages while recording -You may want to log the messages that are sent to server. This may be useful if the connection to the server is lost. You may import the logged messages later. +You may want to log the messages that are sent to the server. This may be useful if the connection to the server is lost. You may import the logged messages later. ```console racelogger.exe record -n "Sebring 12h" -d "Split #2" --msg-log-file grpc-data.bin @@ -124,8 +124,10 @@ racelogger.exe ping -n 10 -d 1s ## Import -Let's assume the connection to the backend server was lost during recording. Luckily we enabled to message logging during recording via the `--msg-log-file grpc-data.bin` option (see above). -After the race has finished we want to import the data to the backend. Best practise is to replace the (partial) data on the server with the import file. +Let's assume the connection to the backend server was lost during recording. Luckily we enabled the message logging during recording via the `--msg-log-file grpc-data.bin` option (see above). +After the race has finished we want to import the data to the backend. + +Best practise is to replace the (partial) data on the server with the import file. ```console racelogger.exe import --replace-data grpc-data.bin diff --git a/pkg/cmd/ping/ping.go b/pkg/cmd/ping/ping.go index d21903e..1120019 100644 --- a/pkg/cmd/ping/ping.go +++ b/pkg/cmd/ping/ping.go @@ -15,10 +15,12 @@ import ( ) var ( - numPings int - delayArg string + numPings int + delayArg string + ignoreErrors bool ) +//nolint:lll // readability func NewPingCmd() *cobra.Command { cmd := &cobra.Command{ Use: "ping", @@ -29,6 +31,7 @@ func NewPingCmd() *cobra.Command { } cmd.Flags().IntVarP(&numPings, "num", "n", 10, "number of pings to send") cmd.Flags().StringVarP(&delayArg, "delay", "d", "1s", "time to wait between pings") + cmd.Flags().BoolVarP(&ignoreErrors, "ignore", "i", false, "Ignore errors and continue pinging") return cmd } @@ -53,12 +56,15 @@ func pingBackend() { req := providerv1.PingRequest{Num: int32(i)} r, err := c.Ping(context.Background(), &req) if err != nil { - log.Error("error pinging server", log.ErrorField(err)) - return + log.Error("error pinging server", log.Int("i", i), log.ErrorField(err)) + if !ignoreErrors { + return + } + } else { + log.Info("Response", + log.Int32("num", r.Num), + log.String("time-utc", r.Timestamp.AsTime().Format(time.RFC3339))) } - log.Info("Response", - log.Int32("num", r.Num), - log.String("time-utc", r.Timestamp.AsTime().Format(time.RFC3339))) time.Sleep(delay) } diff --git a/pkg/grpc/dataprovider.go b/pkg/grpc/dataprovider.go index c478d43..3812e86 100644 --- a/pkg/grpc/dataprovider.go +++ b/pkg/grpc/dataprovider.go @@ -111,18 +111,30 @@ func (dpc *DataProviderClient) DeleteEvent(eventKey string) error { return err } -//nolint:whitespace // by design +//nolint:whitespace,nestif,gocognit // by design func (dpc *DataProviderClient) PublishStateFromChannel( eventKey string, rcv chan *racestatev1.PublishStateRequest, ) { go func() { + errorCounter := 0 for { s, more := <-rcv if s != nil { err := dpc.PublishState(s) if err != nil { - log.Error("Error publishing state data", log.ErrorField(err)) + if errorCounter%30 == 0 { + log.Error("Error publishing state data", + log.Int("errorCounter", errorCounter+1), + log.ErrorField(err)) + } + errorCounter++ + } else { + if errorCounter > 0 { + log.Info("Published state data successful again", + log.Int("errorCounter", errorCounter)) + } + errorCounter = 0 } } if !more {