Skip to content

Commit

Permalink
Merge pull request #225 from biothings/fix-896-bte
Browse files Browse the repository at this point in the history
Add file locking while reading smartapi
  • Loading branch information
tokebe authored Nov 13, 2024
2 parents 9eabfa1 + cb66625 commit b0732af
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@
"@biothings-explorer/call-apis": "workspace:../call-apis",
"@biothings-explorer/node-expansion": "workspace:../node-expansion",
"@biothings-explorer/smartapi-kg": "workspace:../smartapi-kg",
"@biothings-explorer/utils": "workspace:../utils",
"@biothings-explorer/types": "workspace:../types",
"biolink-model": "workspace:../biolink-model",
"biomedical_id_resolver": "workspace:../biomedical_id_resolver",
"@biothings-explorer/utils": "workspace:../utils",
"@sentry/node": "^7.74.1",
"async": "^3.2.4",
"biolink-model": "workspace:../biolink-model",
"biomedical_id_resolver": "workspace:../biomedical_id_resolver",
"chi-square-p-value": "^1.0.5",
"debug": "^4.3.4",
"ioredis": "^5.3.2",
Expand Down
12 changes: 7 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Graph from './graph/graph';
import EdgeManager from './edge_manager';
import _ from 'lodash';
import QEdge2APIEdgeHandler from './qedge2apiedge';
import { LogEntry, StampedLog } from '@biothings-explorer/utils';
import { lockWithActionAsync, LogEntry, StampedLog } from '@biothings-explorer/utils';
import { promises as fs } from 'fs';
import { getDescendants } from '@biothings-explorer/node-expansion';
import { resolveSRI, SRINodeNormFailure } from 'biomedical_id_resolver';
Expand Down Expand Up @@ -81,8 +81,11 @@ export default class TRAPIQueryHandler {
if (this.options.smartapi) {
smartapiRegistry = this.options.smartapi;
} else {
const file = await fs.readFile(this.path, 'utf-8');
smartapiRegistry = JSON.parse(file);
smartapiRegistry = await lockWithActionAsync([this.path], async () => {
const file = await fs.readFile(this.path, 'utf-8');
const hits = JSON.parse(file);
return hits;
}, debug);
}

const smartapiIds: string[] = [];
Expand Down Expand Up @@ -124,7 +127,7 @@ export default class TRAPIQueryHandler {

const metaKG = new MetaKG(this.path, this.predicatePath);
debug(`SmartAPI Specs read from path: ${this.path}`);
metaKG.constructMetaKGSync(this.includeReasoner, this.options);
await metaKG.constructMetaKGWithFileLock(this.includeReasoner, this.options);
return metaKG;
}

Expand Down Expand Up @@ -722,7 +725,6 @@ export default class TRAPIQueryHandler {
return;
}
debug('MetaKG successfully loaded!');

span1?.finish();

if (global.missingAPIs) {
Expand Down

0 comments on commit b0732af

Please sign in to comment.