Created
January 27, 2021 19:01
-
-
Save erkanzileli/391ef1ffee6cac804d3401c386f6c2c9 to your computer and use it in GitHub Desktop.
concurrent processing with chunks while holding a checkpoint as a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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