@cumulus/db 16.0.3-alpha.0 → 17.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.QuerySearchClient = void 0;
4
+ const retry_1 = require("./retry");
4
5
  /**
5
6
  * Class to handle fetching results for an arbitrary PostgreSQL query and
6
7
  * paging through them.
@@ -19,7 +20,7 @@ class QuerySearchClient {
19
20
  * @throws
20
21
  */
21
22
  async fetchRecords() {
22
- this.records = await (this.query
23
+ this.records = await (0, retry_1.RetryOnDbConnectionTerminateError)(this.query
23
24
  .offset(this.offset)
24
25
  .limit(this.limit));
25
26
  this.offset += this.limit;
@@ -6,5 +6,5 @@ import { Knex } from 'knex';
6
6
  * @param {Array<string>} granuleIds - Array of granule IDs
7
7
  * @returns {Promise<Array<Object>>} - An array of collection results
8
8
  */
9
- export declare const getCollectionsByGranuleIds: (knex: Knex, granuleIds: string[]) => Promise<any[]>;
9
+ export declare const getCollectionsByGranuleIds: (knex: Knex, granuleIds: string[]) => Promise<never>;
10
10
  //# sourceMappingURL=collection.d.ts.map
@@ -1,6 +1,11 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.getCollectionsByGranuleIds = void 0;
7
+ const logger_1 = __importDefault(require("@cumulus/logger"));
8
+ const retry_1 = require("./retry");
4
9
  const tables_1 = require("../tables");
5
10
  /**
6
11
  * Get collection results for a given set of granule IDs
@@ -11,11 +16,13 @@ const tables_1 = require("../tables");
11
16
  */
12
17
  const getCollectionsByGranuleIds = async (knex, granuleIds) => {
13
18
  const { collections: collectionsTable, granules: granulesTable, } = tables_1.TableNames;
14
- return await knex(collectionsTable)
19
+ const log = new logger_1.default({ sender: '@cumulus/db/models/collection' });
20
+ const query = knex(collectionsTable)
15
21
  .select(`${collectionsTable}.*`)
16
22
  .innerJoin(granulesTable, `${collectionsTable}.cumulus_id`, `${granulesTable}.collection_cumulus_id`)
17
23
  .whereIn(`${granulesTable}.granule_id`, granuleIds)
18
24
  .groupBy(`${collectionsTable}.cumulus_id`);
25
+ return await (0, retry_1.RetryOnDbConnectionTerminateError)(query, {}, log);
19
26
  };
20
27
  exports.getCollectionsByGranuleIds = getCollectionsByGranuleIds;
21
28
  //# sourceMappingURL=collection.js.map
@@ -0,0 +1,7 @@
1
+ import { Knex } from 'knex';
2
+ import pRetry from 'p-retry';
3
+ import Logger from '@cumulus/logger';
4
+ declare type PromiseReturnType<T> = T extends (method: Promise<any> | Knex.QueryBuilder) => infer R ? Promise<R> : never;
5
+ export declare const RetryOnDbConnectionTerminateError: <T>(method: Promise<any> | Knex.QueryBuilder, retryConfig?: pRetry.Options, log?: Logger) => Promise<PromiseReturnType<T>>;
6
+ export {};
7
+ //# sourceMappingURL=retry.d.ts.map
@@ -0,0 +1,32 @@
1
+ 'use strict';
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.RetryOnDbConnectionTerminateError = void 0;
7
+ const p_retry_1 = __importDefault(require("p-retry"));
8
+ const logger_1 = __importDefault(require("@cumulus/logger"));
9
+ const RetryOnDbConnectionTerminateError = async (method, retryConfig, log) => {
10
+ const logger = log || new logger_1.default({ sender: '@cumulus/db/retry' });
11
+ return await (0, p_retry_1.default)(async () => {
12
+ try {
13
+ const result = await method;
14
+ return result;
15
+ }
16
+ catch (error) {
17
+ if (error.message.includes('Connection terminated unexpectedly')) {
18
+ logger.error(`${error}. Retrying...`);
19
+ throw error;
20
+ }
21
+ throw new p_retry_1.default.AbortError(error);
22
+ }
23
+ }, {
24
+ retries: 3,
25
+ onFailedAttempt: (e) => {
26
+ logger.error(`Error ${e.message}. Attempt ${e.attemptNumber} failed.`);
27
+ },
28
+ ...retryConfig,
29
+ });
30
+ };
31
+ exports.RetryOnDbConnectionTerminateError = RetryOnDbConnectionTerminateError;
32
+ //# sourceMappingURL=retry.js.map
@@ -1,8 +1,13 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.BasePgModel = void 0;
7
+ const logger_1 = __importDefault(require("@cumulus/logger"));
4
8
  const errors_1 = require("@cumulus/errors");
5
9
  const database_1 = require("../database");
10
+ const retry_1 = require("../lib/retry");
6
11
  class BasePgModel {
7
12
  constructor({ tableName }) {
8
13
  this.tableName = tableName;
@@ -16,7 +21,9 @@ class BasePgModel {
16
21
  * @returns {Promise<PostgresCollectionRecord[]>} List of returned records
17
22
  */
18
23
  async searchWithUpdatedAtRange(knexOrTransaction, params, updatedAtParams) {
19
- const records = await knexOrTransaction(this.tableName).where((builder) => {
24
+ const log = new logger_1.default({ sender: '@cumulus/db/models/base' });
25
+ const query = knexOrTransaction(this.tableName)
26
+ .where((builder) => {
20
27
  builder.where(params);
21
28
  if (updatedAtParams.updatedAtFrom || updatedAtParams.updatedAtTo) {
22
29
  builder.whereBetween('updated_at', [
@@ -25,6 +32,7 @@ class BasePgModel {
25
32
  ]);
26
33
  }
27
34
  });
35
+ const records = await (0, retry_1.RetryOnDbConnectionTerminateError)(query, {}, log);
28
36
  return records;
29
37
  }
30
38
  async count(knexOrTransaction, params) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cumulus/db",
3
- "version": "16.0.3-alpha.0",
3
+ "version": "17.0.0",
4
4
  "description": "Utilities for working with the Cumulus DB",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./dist/index.js",
@@ -16,7 +16,8 @@
16
16
  "tsc:listEmittedFiles": "../../node_modules/.bin/tsc --listEmittedFiles",
17
17
  "test": "../../node_modules/.bin/ava",
18
18
  "test:coverage": "../../node_modules/.bin/nyc npm test",
19
- "watch": "../../node_modules/.bin/tsc -w"
19
+ "watch": "../../node_modules/.bin/tsc -w",
20
+ "coverage": "python ../../scripts/coverage_handler/coverage.py"
20
21
  },
21
22
  "ava": {
22
23
  "files": [
@@ -29,12 +30,12 @@
29
30
  "node": ">=16.19.0"
30
31
  },
31
32
  "dependencies": {
32
- "@cumulus/aws-client": "16.0.3-alpha.0",
33
- "@cumulus/common": "16.0.3-alpha.0",
34
- "@cumulus/errors": "16.0.3-alpha.0",
35
- "@cumulus/logger": "16.0.3-alpha.0",
36
- "@cumulus/message": "16.0.3-alpha.0",
37
- "@cumulus/types": "16.0.3-alpha.0",
33
+ "@cumulus/aws-client": "17.0.0",
34
+ "@cumulus/common": "17.0.0",
35
+ "@cumulus/errors": "17.0.0",
36
+ "@cumulus/logger": "17.0.0",
37
+ "@cumulus/message": "17.0.0",
38
+ "@cumulus/types": "17.0.0",
38
39
  "crypto-random-string": "^3.2.0",
39
40
  "is-valid-hostname": "1.0.2",
40
41
  "knex": "2.4.1",
@@ -46,5 +47,5 @@
46
47
  "devDependencies": {
47
48
  "@types/uuid": "^8.0.0"
48
49
  },
49
- "gitHead": "81be18bf42487473dce42432781672da91fbecd0"
50
+ "gitHead": "a5acb591242a334a730cebd6159f3a88a466432b"
50
51
  }