Skip to content

Commit

Permalink
Keep logging message on connection loss
Browse files Browse the repository at this point in the history
Fixes #50
  • Loading branch information
mpapenbr committed Jun 2, 2024
1 parent b3784db commit 6839bce
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
20 changes: 13 additions & 7 deletions pkg/cmd/ping/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
}

Expand All @@ -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)
}
Expand Down
16 changes: 14 additions & 2 deletions pkg/grpc/dataprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6839bce

Please sign in to comment.