Skip to content

Commit

Permalink
feat: recreate Datastore instance on error in getById
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Jan 11, 2022
1 parent cb23649 commit 04b3442
Show file tree
Hide file tree
Showing 3 changed files with 590 additions and 582 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"devDependencies": {
"@google-cloud/datastore": "^6.0.0",
"@naturalcycles/dev-lib": "^12.0.1",
"@types/node": "^16.0.0",
"@types/node": "^17.0.8",
"jest": "^27.0.4"
},
"files": [
Expand Down
19 changes: 16 additions & 3 deletions src/datastore.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,24 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
): Promise<ROW[]> {
if (!ids.length) return []
const keys = ids.map(id => this.key(table, id))
const [entities] = await this.ds().get(keys)
let rows: any[]

try {
rows = (await this.ds().get(keys))[0]
} catch (err) {
this.cfg.logger.log('datastore recreated on error')

// This is to debug "GCP Datastore Timeout issue"
const datastoreLib = require('@google-cloud/datastore')
const DS = datastoreLib.Datastore as typeof Datastore
this.cachedDatastore = new DS(this.cfg)

throw err
}

return (
(entities as any[])
.map(e => this.mapId<ROW>(e))
rows
.map(r => this.mapId<ROW>(r))
// Seems like datastore .get() method doesn't return items properly sorted by input ids, so we gonna sort them here
// same ids are not expected here
.sort((a, b) => (a.id > b.id ? 1 : -1))
Expand Down
Loading

0 comments on commit 04b3442

Please sign in to comment.