Skip to content

Commit

Permalink
fix: make sync kg construction support async file locks
Browse files Browse the repository at this point in the history
  • Loading branch information
NeuralFlux committed Nov 5, 2024
1 parent 0dd10e5 commit bc4e020
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 63 deletions.
13 changes: 13 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<SmartAPIKGOperationObject[]> {
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
Expand Down
24 changes: 9 additions & 15 deletions src/operations_builder/sync_operations_builder.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
}
80 changes: 32 additions & 48 deletions src/operations_builder/sync_operations_builder_with_reasoner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit bc4e020

Please sign in to comment.