diff --git a/src/index.ts b/src/index.ts index 999ebeb..62815e8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,6 +6,7 @@ import { ft } from "./filter"; import path from "path"; import Debug from "debug"; import QueryOperationObject from "./parser/query_operation"; +import { lockWithActionAsync } from "@biothings-explorer/utils"; const debug = Debug("bte:smartapi-kg:MetaKG"); export * from "./types"; @@ -66,6 +67,18 @@ export default class MetaKG { return this.ops; } + /* Async wrapper for using constructMetaKGSync to enable using async file locking */ + async constructMetaKGWithFileLock(includeReasoner = false, options: BuilderOptions = {}): Promise { + this._ops = await lockWithActionAsync( + [this._file_path, this._predicates_path], + async () => { + return syncBuilderFactory(options, includeReasoner, this._file_path, this._predicates_path); + }, + debug + ); + return this.ops; + } + /** * Filters the whole meta kg based on apiList, teamName, tag or component * @param {Object} options - filtering options diff --git a/src/operations_builder/sync_operations_builder.ts b/src/operations_builder/sync_operations_builder.ts index 240ccea..bd31788 100644 --- a/src/operations_builder/sync_operations_builder.ts +++ b/src/operations_builder/sync_operations_builder.ts @@ -1,9 +1,6 @@ import BaseOperationsBuilder from "./base_operations_builder"; import { syncLoaderFactory } from "../load/sync_loader_factory"; import { BuilderOptions } from "../types"; -import { lockWithActionSync } from "@biothings-explorer/utils"; -import { SmartAPISpec } from "../parser/types"; -const debug = require("debug")("bte:smartapi-kg:SyncOperationsBuilder"); export default class SyncOperationsBuilder extends BaseOperationsBuilder { private _file_path: string; @@ -14,18 +11,15 @@ export default class SyncOperationsBuilder extends BaseOperationsBuilder { } build() { - let specs: SmartAPISpec[] = lockWithActionSync(this._file_path, () => { - return syncLoaderFactory( - this._options.smartAPIID, - this._options.teamName, - this._options.tag, - this._options.component, - this._options.apiList, - this._file_path, - this._options.smartapiSpecs - ); - }, debug); - + const specs = syncLoaderFactory( + this._options.smartAPIID, + this._options.teamName, + this._options.tag, + this._options.component, + this._options.apiList, + this._file_path, + this._options.smartapiSpecs + ); return this.loadOpsFromSpecs(specs); } } diff --git a/src/operations_builder/sync_operations_builder_with_reasoner.ts b/src/operations_builder/sync_operations_builder_with_reasoner.ts index 8b51b08..586115a 100644 --- a/src/operations_builder/sync_operations_builder_with_reasoner.ts +++ b/src/operations_builder/sync_operations_builder_with_reasoner.ts @@ -7,7 +7,7 @@ import { PredicatesMetadata } from "../types"; import Debug from "debug"; const debug = Debug("bte:smartapi-kg:SyncOperationsBuilderWithReasoner"); import { SmartAPISpec } from "../parser/types"; -import { biolink, lockWithActionSync } from "@biothings-explorer/utils"; +import { biolink } from "@biothings-explorer/utils"; declare global { var missingAPIs: SmartAPISpec[]; @@ -220,55 +220,39 @@ export default class SyncOperationsBuilderWithReasoner extends BaseOperationsBui } build() { - const predicatesMetadata: PredicatesMetadata[] = lockWithActionSync( - this._predicates_file_path, - () => this.fetch(), - debug - ) - - let specs: SmartAPISpec[]; - let nonTRAPIOps: SmartAPIKGOperationObject[]; - - lockWithActionSync( + const specs = syncLoaderFactory( + this._options.smartAPIID, + this._options.teamName, + this._options.tag, + this._options.component, + this._options.apiList, this._file_path, - () => { - specs = syncLoaderFactory( - this._options.smartAPIID, - this._options.teamName, - this._options.tag, - this._options.component, - this._options.apiList, - this._file_path, - this._options.smartapiSpecs, - ); - - nonTRAPIOps = this.loadOpsFromSpecs(specs); - global.missingAPIs = syncLoaderFactory( - undefined, - undefined, - undefined, - undefined, - undefined, - this._file_path, - this._options.smartapiSpecs, - ).filter( - spec => - "info" in spec && - "x-translator" in spec.info && - spec.info["x-translator"].component === "KP" && - "paths" in spec && - "/query" in spec.paths && - "x-trapi" in spec.info && - spec.servers.length && - "/meta_knowledge_graph" in spec.paths && - !predicatesMetadata - .map(m => m.association.smartapi.id) - .includes(spec._id), - ); - }, - debug + this._options.smartapiSpecs, + ); + const nonTRAPIOps = this.loadOpsFromSpecs(specs); + const predicatesMetadata = this.fetch(); + global.missingAPIs = syncLoaderFactory( + undefined, + undefined, + undefined, + undefined, + undefined, + this._file_path, + this._options.smartapiSpecs, + ).filter( + spec => + "info" in spec && + "x-translator" in spec.info && + spec.info["x-translator"].component === "KP" && + "paths" in spec && + "/query" in spec.paths && + "x-trapi" in spec.info && + spec.servers.length && + "/meta_knowledge_graph" in spec.paths && + !predicatesMetadata + .map(m => m.association.smartapi.id) + .includes(spec._id), ); - let TRAPIOps = [] as SmartAPIKGOperationObject[]; predicatesMetadata.map(metadata => { TRAPIOps.push.apply(TRAPIOps, this.parsePredicateEndpoint(metadata));