Is it bad practice to use setInterval? #188
-
Hello, I currently have this code: const bree = new Bree({
doRootCheck: false,
root: false,
hasSeconds: true,
});
const client = new Client(bree);
await client.addAndStartAllJobs();
setInterval(async () => {
const hasUpdates = await client.checkUpdates();
if (hasUpdates) {
await client.removeAllJobs();
await client.addAndStartAllJobs();
}
}, 60 * 1000); As you can see, each minute I check if there are updates, and if yes, I remove, re-add and restart all jobs again. Every job there runs every minute, with a random timeout in seconds between 1 and 59(hence why I enabled hasSeconds flag). The jobs use puppeteer to capture screenshots. I noticed that this implementation quickly consumes a lot of memory, and sometimes cpu. How can I improve this? I saw that there's a library called https://github.com/ealmansi/set-interval-async Please let me know what I'm doing wrong here. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
We have no idea what |
Beta Was this translation helpful? Give feedback.
We have no idea what
Client
is in your code example and we don't know what your jobs are actually doing. Please provide more details. There is nothing wrong with usingsetInterval
. High CPU and memory is a result of CPU intensive and memory intensive operations.setInterval
is merely a timer and is not the culprit.