Skip to content

Commit b0732af

Browse files
authored
Merge pull request #225 from biothings/fix-896-bte
Add file locking while reading smartapi
2 parents 9eabfa1 + cb66625 commit b0732af

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@
5757
"@biothings-explorer/call-apis": "workspace:../call-apis",
5858
"@biothings-explorer/node-expansion": "workspace:../node-expansion",
5959
"@biothings-explorer/smartapi-kg": "workspace:../smartapi-kg",
60-
"@biothings-explorer/utils": "workspace:../utils",
6160
"@biothings-explorer/types": "workspace:../types",
62-
"biolink-model": "workspace:../biolink-model",
63-
"biomedical_id_resolver": "workspace:../biomedical_id_resolver",
61+
"@biothings-explorer/utils": "workspace:../utils",
6462
"@sentry/node": "^7.74.1",
6563
"async": "^3.2.4",
64+
"biolink-model": "workspace:../biolink-model",
65+
"biomedical_id_resolver": "workspace:../biomedical_id_resolver",
6666
"chi-square-p-value": "^1.0.5",
6767
"debug": "^4.3.4",
6868
"ioredis": "^5.3.2",

src/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Graph from './graph/graph';
1010
import EdgeManager from './edge_manager';
1111
import _ from 'lodash';
1212
import QEdge2APIEdgeHandler from './qedge2apiedge';
13-
import { LogEntry, StampedLog } from '@biothings-explorer/utils';
13+
import { lockWithActionAsync, LogEntry, StampedLog } from '@biothings-explorer/utils';
1414
import { promises as fs } from 'fs';
1515
import { getDescendants } from '@biothings-explorer/node-expansion';
1616
import { resolveSRI, SRINodeNormFailure } from 'biomedical_id_resolver';
@@ -81,8 +81,11 @@ export default class TRAPIQueryHandler {
8181
if (this.options.smartapi) {
8282
smartapiRegistry = this.options.smartapi;
8383
} else {
84-
const file = await fs.readFile(this.path, 'utf-8');
85-
smartapiRegistry = JSON.parse(file);
84+
smartapiRegistry = await lockWithActionAsync([this.path], async () => {
85+
const file = await fs.readFile(this.path, 'utf-8');
86+
const hits = JSON.parse(file);
87+
return hits;
88+
}, debug);
8689
}
8790

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

125128
const metaKG = new MetaKG(this.path, this.predicatePath);
126129
debug(`SmartAPI Specs read from path: ${this.path}`);
127-
metaKG.constructMetaKGSync(this.includeReasoner, this.options);
130+
await metaKG.constructMetaKGWithFileLock(this.includeReasoner, this.options);
128131
return metaKG;
129132
}
130133

@@ -722,7 +725,6 @@ export default class TRAPIQueryHandler {
722725
return;
723726
}
724727
debug('MetaKG successfully loaded!');
725-
726728
span1?.finish();
727729

728730
if (global.missingAPIs) {

0 commit comments

Comments
 (0)