Skip to content

Commit

Permalink
Use async to manage concurrency on git modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Ndpnt committed Jun 11, 2020
1 parent d2362fb commit 172f5e9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -29,6 +29,7 @@
"nock": "^12.0.3"
},
"dependencies": {
"async": "^3.2.0",
"console-stamp": "^0.2.9",
"dotenv": "^8.2.0",
"isomorphic-git": "^1.4.0",
Expand Down
29 changes: 16 additions & 13 deletions src/history/persistor.js
Expand Up @@ -2,6 +2,13 @@ import path from 'path';
import fsApi from 'fs';
const fs = fsApi.promises;

import async from 'async';

const commitQueue = async.queue(_commit, 1);
commitQueue.error((err, { serviceProviderId, policyType, isSanitized, reject }) => {
reject(new Error(`_commit for ${serviceProviderId}${isSanitized ? ' sanitized ' : ' '}document ${policyType} experienced an error: ${err}`));
});

import * as git from './git.js';

const __dirname = path.dirname(new URL(import.meta.url).pathname);
Expand All @@ -28,7 +35,6 @@ export async function save({ serviceProviderId, policyType, fileContent, isSanit
});
}

let lock = Promise.resolve('Initial Promise');
export async function commit({ serviceProviderId, policyType, isSanitized }) {
const directory = `${isSanitized ? SANITIZED_DIRECTORY : RAW_DIRECTORY}/${serviceProviderId}`;
const fileExtension = isSanitized ? 'md' : 'html'
Expand All @@ -39,17 +45,14 @@ export async function commit({ serviceProviderId, policyType, isSanitized }) {
return;
}

// Ensure asynchronous functions `git.add` and `git.commit` will always be called in sequence…
// …and others caller of `persistor.commit` will wait
await lock;
lock = new Promise(resolveLock => {
git.add(filePath).then(() => {
git.commit(`${isSanitized ? 'Update sanitized' : 'Update'} ${serviceProviderId} ${policyType} document`).then((sha) => {
console.log(`Commit ID for document "${serviceProviderId}/${policyType}.${fileExtension}": ${sha}`);
resolveLock(sha);
});
});
});
return new Promise((resolve, reject) => {
commitQueue.push({ serviceProviderId, policyType, isSanitized, fileExtension, filePath, resolve, reject });
})
}

return lock;
async function _commit({ serviceProviderId, policyType, isSanitized, fileExtension, filePath, resolve }) {
await git.add(filePath)
const sha = await git.commit(`${isSanitized ? 'Update sanitized' : 'Update'} ${serviceProviderId} ${policyType} document`);
console.log(`Commit ID for document "${serviceProviderId}/${policyType}.${fileExtension}": ${sha}`);
resolve(sha);
}

0 comments on commit 172f5e9

Please sign in to comment.