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 f2f6f54 commit 7745a7a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/controllers/cron/update_local_smartapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,18 +326,18 @@ async function updateSmartAPISpecs() {
delete obj._score;
});

await lockWithActionAsync(localFilePath, async () => {
await lockWithActionAsync([localFilePath], async () => {
await fs.writeFile(localFilePath, JSON.stringify({ hits: hits }));
}, debug)

const predicatesInfo = await getOpsFromPredicatesEndpoints(res.data.hits);
await lockWithActionAsync(predicatesFilePath, async () => {
await lockWithActionAsync([predicatesFilePath], async () => {
await fs.writeFile(predicatesFilePath, JSON.stringify(predicatesInfo));
}, debug);

// Create a new metakg
const metakg = new MetaKG();
metakg.constructMetaKGSync(true, { predicates: predicatesInfo, smartapiSpecs: { hits: hits as any }, apiList });
await metakg.constructMetaKGWithFileLock(true, { predicates: predicatesInfo, smartapiSpecs: { hits: hits as any }, apiList });
global.metakg = metakg;
global.smartapi = { hits }; // hits is an array, but smartapi must be a dict
};
Expand All @@ -348,11 +348,11 @@ async function loadGlobalMetaKGReadOnly() {
const predicatesFilePath = path.resolve(__dirname, "../../../data/predicates.json");

const metakg = new MetaKG(localFilePath, predicatesFilePath);
metakg.constructMetaKGSync(true, { apiList });
await metakg.constructMetaKGWithFileLock(true, { apiList });
global.metakg = metakg;

global.smartapi = await lockWithActionAsync(
localFilePath,
[localFilePath],
async () => {
const file = await fs.readFile(localFilePath, 'utf-8');
const hits = JSON.parse(file);
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/meta_knowledge_graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ export default class MetaKnowledgeGraphHandler {
try {
if (smartAPIID !== undefined) {
debug(`Constructing with SmartAPI ID ${smartAPIID}`);
kg.constructMetaKGSync(false, { apiList, smartAPIID: smartAPIID });
await kg.constructMetaKGWithFileLock(false, { apiList, smartAPIID: smartAPIID });
} else if (teamName !== undefined) {
debug(`Constructing with team ${teamName}`);
kg.constructMetaKGSync(false, { apiList, teamName: teamName });
await kg.constructMetaKGWithFileLock(false, { apiList, teamName: teamName });
} else {
debug(`Constructing with default`);
kg.constructMetaKGSync(true, { apiList });
await kg.constructMetaKGWithFileLock(true, { apiList });
}
if (kg.ops.length === 0) {
debug(`Found 0 operations`);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/metakg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class MetaKG {
if (req.query.provided_by !== undefined) {
source = utils.removeQuotesFromQuery(req.query.provided_by as string);
}
const assocs = assoc(
const assocs = await assoc(
req.query.subject as string,
req.query.object as string,
req.query.predicate as string,
Expand Down

0 comments on commit 7745a7a

Please sign in to comment.