-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* basic retry mechanism * WIP integration tests for retry handler * integration tests for retry working with basic functionality 💪🏼 * async all the namings * add documentation * implement retry sql methods WIP * query retry logic WIP * express example * tests passing on new retry implementation * update sql methods to include retry * update docs and remove comment
- Loading branch information
1 parent
b8eb44f
commit 931d3cf
Showing
11 changed files
with
576 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,45 @@ | ||
# less-tedious | ||
|
||
Helper package for the node.js tedious SQL Server connectivity module. | ||
Helper package for the node.js tedious SQL Server library, and accompanying mssql client library. | ||
|
||
This package provides the following... | ||
|
||
- easy to use `query` and `modify` methods that simplifies common SQL calls. | ||
- easy to use `query` and `modify` methods that simplifies common SQL calls | ||
- automatic connection management and pool initialisation | ||
- promise support. No more wrapping requests in callbacks | ||
- verbose debugging info, enabled by `winston` | ||
- retry logic which tolerates timeouts and other custom conditions of your choosing | ||
|
||
|
||
## Async Retry mechanism | ||
|
||
The async retry handler was created to support the retry of database calls under certain conditions. | ||
For example, when all database connections are saturated, you may want to wait 5 seconds before re-attempting. | ||
|
||
### Example | ||
|
||
```javascript | ||
const retry = require('./retry-async') | ||
// actual call to database wrapped in an async function... | ||
const callDatabase = async () => { | ||
const data = await sql.query('SELECT * FROM [SomeTable]') | ||
return data | ||
} | ||
// we only want to retry timeout errors... | ||
const onlyRetryTimeoutsPredicate = (error) => { | ||
return error.message.contains('timeout') | ||
} | ||
|
||
const retryPolicy = { | ||
attempts: 3, | ||
pauseTimeMs: 5000, | ||
pauseMultiplier: 1.5 | ||
} | ||
|
||
const attemptCount = 3 | ||
try { | ||
const data = await retry(callDatabase, retryPolicy, onlyRetryTimeoutsPredicate) | ||
console.log(`we got some data:${data}`) | ||
} catch (error) { | ||
console.error(`attempted to query database ${retryPolicy.attempts} times, but all calls were unsuccessful`) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
'use strict' | ||
|
||
require('dotenv').config() | ||
const oneMinuteInMilliseconds = 60000 | ||
|
||
module.exports = { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict' | ||
|
||
require('dotenv').config() | ||
const express = require('express') | ||
const sqlService = require('./index') | ||
const sqlConfig = require('./example-config') | ||
|
||
sqlService.initPool(sqlConfig) | ||
|
||
const app = express() | ||
const port = 3000 | ||
|
||
const callDatabase = async (res) => { | ||
const sql = 'SELECT TOP 1 * FROM [mtc_admin].[settings]' | ||
await sqlService.queryWithRetry(sql) | ||
res.sendStatus(200) | ||
} | ||
|
||
app.get('/', (req, res) => callDatabase(res)) | ||
app.listen(port, () => console.log(`express listening on port ${port}`)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.