I found myself trying to process a vast amount of data recently and I had to do some (lets call it) creative coding to get the desired result.
I knew the processing would be a multi day task so I wrote it in a way that could be interrupted and resumed. But what stumped me was finding a nice way to process SIGINT/SIGTERM style events without wanting to break my loop.
btw these are obviously not the real names for my functions
let closed = false;
function* syncGenerator() {
yield* ~20-trillion-calculated-results;
}
function main() {
try {
for ( const value of syncGenerator() ) {
syncWork(value);
if (closed) {
syncGracefulCleanup();
break;
}
}
} catch ( err ) {
handleError(err);
} finally {
syncGracefulCleanup2();
}
}
process.on('SIGINT', () => closed = true);
I had failed to realise that SIGINT would never be processed while I was still in the for-loop and so would only ever finish after the entire dataset was processed (a very foolish oversight, I think I was deluded to believing it would work the same was as Arduino Hardware Interrupts. D'oh. Lesson learned.
Now that I realised this, my solution was to put half of my code at the end of the tick using the microtask async-await trick.
let closed = false;
function* syncGenerator() {
yield* ~20-trillion-calculated-results;
}
async function main() {
try {
for ( const value of syncGenerator() ) {
syncWork(value);
await new Promise(r => setImmediate(r)); // the new nasty
if (closed) {
syncGracefulCleanup();
break;
}
}
} catch ( err ) {
handleError(err);
} finally {
syncGracefulCleanup2();
}
}
process.on('SIGINT', () => closed = true);
Now, this works as I expected and the await allows the loop to be paused and allow SIGINT/SIGTERM to be processed and then picks up if closed is set to true. YAY.. But DAMN it looks nasty. I was hoping someone might have a better looking solution than this?
syncGeneratorpossibly be an asynchronous generator instead?for awaitloop to consume it?await new Promise((r) => setImmidate(r));into there. As I said, same problem, just different location.