Skip to content

Instantly share code, notes, and snippets.

@erkanzileli
Created January 27, 2021 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erkanzileli/391ef1ffee6cac804d3401c386f6c2c9 to your computer and use it in GitHub Desktop.
Save erkanzileli/391ef1ffee6cac804d3401c386f6c2c9 to your computer and use it in GitHub Desktop.
concurrent processing with chunks while holding a checkpoint as a file
const R = require("ramda");
// checkpoint, only processed data
const processed = require("./updated.json");
// processed and unprocessed data together, whole data
const toBeProcessed = require("./to_be_updated.json");
// data which will be processed on this execution
const willBeProcessed = R.difference(toBeUpdated, updated);
const chunks = R.splitEvery(10, willBeUpdated);
// function that does something
async function doSomething(param) {}
// write progress to the checkpoint file
function syncProgress() {
fs.writeFileSync("updated.json", Buffer.from(JSON.stringify(updated)));
}
// concurrent execution with chunks
(async function () {
for (const arr of chunks) {
await Promise.all(
arr.map((param) =>
doSomething(param).then(() => {
updated.push(param);
})
)
).catch(syncProgress);
syncProgress();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment