-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Tech Debt] Use knex pooling correctly and provide DB connection to P…
…gBoss - Initialize the knex pool instance in the top level of publisher and consumer and pass the knex instance to the classes that make DB calls. - Update knex initialization to use the full knexfile config instead of just the connection field. - Add PgBossKnexAdapter class to convert PgBoss SQL statements into knex raw commands. - Pass PgBossKnexAdapter to queue client setup. This removes the separate database for queue messages. - Remove knex pool destroy statements from the message queue consumer and from postgres publisher. - Keep knex pool destroy statements in message queue publisher to close the pool after each interval script. - Update CI to remove the message queue database parameter from deploy jobs - Remove map statement from CSV formatter to avoid making a copy of the report dataset during formatting in order to reduce consumer memory usage. - Offset timed publisher script runs so they don't ever happen in parallel to reduce publisher memory usage. - Disable unused hourly publisher runs.
- Loading branch information
Showing
19 changed files
with
184 additions
and
113 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
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
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
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
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
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
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,39 @@ | ||
/** | ||
* Handles providing a database client for the Pg-Boss library using knex. | ||
*/ | ||
class PgBossKnexAdapter { | ||
#knex; | ||
|
||
/** | ||
* @param {import('knex')} knexInstance an initialized instance of the knex | ||
* library which provides a database connection. | ||
*/ | ||
constructor(knexInstance) { | ||
this.#knex = knexInstance; | ||
} | ||
|
||
/** | ||
* Execute PgBoss SQL using the knex library interface | ||
* | ||
* @param {string} sql the SQL string to execute. | ||
* @param {string[]} parameters the parameters to insert into the SQL string. | ||
* @returns {Promise} which resolves with the result of the SQL query. | ||
*/ | ||
executeSql(sql, parameters = []) { | ||
// This is needed to replace pg-boss' $1, $2 arguments | ||
// into knex's :val, :val2 style. | ||
const replacedSql = sql.replace( | ||
/\$(\d+)\b/g, | ||
(_, number) => `:param_${number}`, | ||
); | ||
|
||
const parametersObject = {}; | ||
parameters.forEach( | ||
(value, index) => (parametersObject[`param_${index + 1}`] = value), | ||
); | ||
|
||
return this.#knex.raw(replacedSql, parametersObject); | ||
} | ||
} | ||
|
||
module.exports = PgBossKnexAdapter; |
Oops, something went wrong.