Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Got retry hook to log when retries happen #354

Merged
merged 6 commits into from
Aug 15, 2023

Conversation

Cruikshanks
Copy link
Member

https://eaflood.atlassian.net/browse/WATER-4083

We had an issue with the Midlands SROC supplementary bill run. It would error and when checking the logs we found it was because the charging-module-api was rejecting a POST /transaction request.

{
  "result": {
    "succeeded": false,
    "response": {
      "statusCode": 409,
      "body": {
        "statusCode": 409,
        "error": "Conflict",
        "message": "A transaction with Client ID 'ebee5d48-3191-4615-b86a-5bebb9026bfe' for Regime 'wrls' already exists.",
        "clientId": "ebee5d48-3191-4615-b86a-5bebb9026bfe"
      }
    }
  }
}

But this should be impossible. We generate the ID's during the bill run. If the bill run errors all transaction records get deleted. We then found a second attempt to run the bill run succeeded.

We've been unable to replicate the failed scenario but have a working theory. We believe a POST /transaction request is failing due to a timeout. That doesn't mean the CHA didn't receive the request, just that Got timed out waiting for it. We have it configured to automatically retry these. So, in this scenario, the CHA receives the first request and creates the transaction but Got times out. It then sends the same request a second time, by which point the record already exists in the CHA so it rejects it.

To confirm this, and see when Got is retrying this change adds a new beforeRetry hook to the Got instance we create. In it, we can log that a retry has taken place and details about it.

https://eaflood.atlassian.net/browse/WATER-4083

We had an issue with the Midlands SROC supplementary bill run. It would error and when checking the logs we found it was because the [charging-module-api](https://github.com/DEFRA/sroc-charging-module-api) was rejecting a `POST /transaction` request.

```json
{
  "result": {
    "succeeded": false,
    "response": {
      "statusCode": 409,
      "body": {
        "statusCode": 409,
        "error": "Conflict",
        "message": "A transaction with Client ID 'ebee5d48-3191-4615-b86a-5bebb9026bfe' for Regime 'wrls' already exists.",
        "clientId": "ebee5d48-3191-4615-b86a-5bebb9026bfe"
      }
    }
  }
}
```

But this should be impossible. We generate the ID's during the bill run. If the bill run error's all transaction records get deleted. We then found a second attempt to run the bill run succeeded.

We've been unable to replicate the failed scenario but have a working theory. We believe a `POST /transaction` request is failing due to a timeout. That doesn't mean the CHA didn't receive the request, just that [Got](https://github.com/sindresorhus/got) timed out waiting for it. We have it configured to automatically retry these. So, in this scenario the CHA receives the first request and creates the transaction but **Got** times out. It then sends the same request a second time, by which point the record already exists in the CHA so it rejects it.

To confirm this, and see when **Got** is retrying this change adds a new [beforeRetry](https://github.com/sindresorhus/got/blob/main/documentation/9-hooks.md#beforeretry) hook to the **Got** instance we create. In it we can log that a retry has taken place and details about it.
@Cruikshanks Cruikshanks added the enhancement New feature or request label Aug 15, 2023
@Cruikshanks Cruikshanks self-assigned this Aug 15, 2023
We did try extending the Got instance we create `_importGot()`. But that never seemed to trigger the hook. But when we added to options it then started firing.
In our opinion its clearer to see what is happening, add documentation etc if we use named functions rather than anonymous ones.
@Cruikshanks Cruikshanks marked this pull request as ready for review August 15, 2023 12:25
Copy link
Contributor

@Jozzey Jozzey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼

@Cruikshanks Cruikshanks merged commit 96e4ab3 into main Aug 15, 2023
@Cruikshanks Cruikshanks deleted the add-logging-to-got-retries branch August 15, 2023 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants